Home » Data Structures C++ » Linked Lists C++ Program Insertion Deletion & Example

Linked Lists C++ Program Insertion Deletion & Example

Linked Lists C++

Introduction of Linked Lists C++:

The Linked List is a Linear data structure that forms a sequence or pattern. It stores the homogenous or similar type of data just like Arrays and Queues.

 The linked list is a data structure or a Node which has two parts like,
1. Data Part stores the actual data e.g. Roll No., Name, etc.

2. Link Part the 2nd part is a pointer to another node, i.e, it stores the address of the next 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 that 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.

Linked List C++ Explanation

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

Step 1: Create a structure ( 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. Insert Nodes.

Define a function which adds data to the Node.

Step 3: Display Data

Linked Lists C++ Program

//Linked List C++ Program //wikkihut.com #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; } } //function to display elements 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; //wikkihut.com }

Some Related Articles