Linked Lists C/C++ Easy Method 2020 | Singly Linked List

Published by Lonz on

Linked List C/C++ Learn Step by Step

Introduction of Linked Lists C/C++:

The Linked Lists is a linear data structure so forms a sequence or pattern. It stores the homogenous data types and the nodes are linked by pointers. 

The linked list is a data structure or a Node which has two parts like
data part and the second part is a pointer to another node, it is commonly called a Node.

Linked List or Singly Linked list is a linear data structure like Arrays.
But in Arrays the data is stored in contiguous locations and the size is
fixed.

But in linked lists the data is stored at different locations and is linked or connected through Pointers thus making it a dynamic data structure that grows and shrinks at the run time with our needs.

Since a good hold on pointers in C++ is mandatory. So keep in mind that Pointers are variables which holds the address of another variable that can be any primary data type like Int, Char, Float and can be any user-defined data types like Classes, Structures, Unions and much more in a broader sense.

Following are the Simple and Easy Steps to Learn actually how we
create and modify linked lists data elements .

        step 1: Create a structure or Node with two fields.

 a. like int data which stores the actual data elements.
 b. Node *Next is a pointer which holds memory address of another node.
c. now just make a class where we call functions like adding or displaying elements of a linked list.

Create a Node with data and a next pointer part C/C++

step 2. Define a function which adds data to the Node.

add nodes

add-node to the linked list

display function to show the node elements

Linked List C++ is used when we want to dynamically allocate memory at the run time because it can grow and shrink as the data increases or decreases.

	//Linked List C++ Program
#include<iostream>
using namespace std;
 struct node
 {
 	int data;
 	node *next;
 };
 class linked_list
 {
 	node *head;
 	public:
 		linked_list()
 		{
 			head=NULL;
 		}
	void add_node();
	void display();
	void add_node_begining();
	void add_at_pos();
 };
 
 void linked_list::add_node()
 {
 	int n;
 	node *p;
 	node *temp=new node;
 	cout<<"Enter data: \n";
 	cin>>n;
 	temp->data=n;
 	temp->next=NULL;
 if(head==NULL)
 {
 	head=temp;
 	head->next=NULL;
 }
 else
 {
 	p=head;
 	while(p->next!=NULL)
 	{
 		p=p->next;
 	}
	 	p->next=temp;
	 
 }
 }
  void linked_list::display()
  	{
  		node *temp;
  		temp=head;
  	cout<<"values in linked list are:\n";
  	cout<<endl;
  	while(temp!=NULL)
  	{
  		cout<<" "<<temp->data<<" -->";
  		temp=temp->next;
	  }
	  cout<<endl;
	  cout<<endl;
	  }
	  
 void linked_list::add_node_begining()
 {
 	int n;
 	node *p;
 	node *temp=new node;
 	cout<<"enter value: ";
 	cin>>n;
 	temp->data=n;
 	temp->next=NULL;
 if(head==NULL)
 {
 	head=temp;
 	head->next=NULL;
 }
 else
 {
 	p=head;
 	temp->next=p;
 	head=temp;
 }
}
 
 void linked_list::add_at_pos()
 {
 	int n, i, pos, counter=0;
 	node *temp=new node;
 	node *p,*ptr;
 	cout<<"enter postion at which to insert: ";
 	cin>>pos;
 	cout<<"enter value: ";
 	cin>>n;
 	temp->data=n;
 	temp->next=NULL;
 	p=head;
 while(p!=NULL)
 {
 	p=p->next;
 	counter ++;
 }
 if(pos==1)
 {
 	if(head==NULL)
 	{
 		head=temp;
 		head->next=NULL;
	 }
	 else
	 {
	 	ptr=head;
	 	head=temp;
	 	head->next=ptr;
	 }
	 
 }else if(pos>1 || pos<=counter)
 {
 	p=head;
  for(i=1; i<pos; i++)
   {
   	ptr=p;
   	p=p->next;
   }
   ptr->next=temp;
   temp->next=p;
 }else
  cout<<"position out of order: \n";
}
 

 int main()
 {
 	linked_list object;
 	while(1){
	 cout<<"enter 1 for add: 2 for display:3 for add-begining";
	 cout<<" 4 for add-pos: 5 for exit: \n";
 	int ch;
 	cin>>ch;
 	
	 switch(ch)
	 {
	 case 1:
 	   object.add_node();
     break;
    case 2:
 	object.display();
 	break;
 	    case 3:
 		object.add_node_begining();
 		break;
 	case 4:
 			object.add_at_pos();
 	break;
 	  case 5:
 		return 0;
 	default:
 		cout<<"wrong choice:\n";
 }
	 }
	 return 0;
 }
  1. Do You Want To Learn These Related Data Structures?
  2. Doubly Linked List C++
  3. Circular Singly Linked Lists C/C++
  4. Doubly Circular Linked Lists C/C++
  5. Stack Using Arrays


0 Comments

Leave a Reply

%d bloggers like this: