Linked List : A linked list is a sequence of data structures, which are connected together via links.Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list is the second most-used data structure after array. Following are the important terms to understand the concept of Linked List.
Link − Each link of a linked list can store a data called an element.
Next − Each link of a linked list contains a link to the next link called Next.
LinkedList − A Linked List contains the connection link to the first link called First.
Basic Operations
Following are the basic operations supported by a list.
Insertion − Adds an element at the beginning of the list.
Deletion − Deletes an element at the beginning of the list.
Display − Displays the complete list.
Search − Searches an element using the given key.
Delete − Deletes an element using the given key.
Advantages of Linked Lists
They are a dynamic in nature which allocates the memory when required.
Insertion and deletion operations can be easily implemented.
Stacks and queues can be easily executed.
Linked List reduces the access time.
Disadvantages of Linked Lists
The memory is wasted as pointers require extra memory for storage.
No element can be accessed randomly; it has to access each node sequentially.
Reverse Traversing is difficult in linked list.
Add in start function in Linked List
void add_in_start(){ temp=new list; cout<<"Enter Value: "; cin>>temp->data; temp->ptr=start; start=temp;}
Add in End function in Linked List
void add_in_end(){ if(start==NULL) { temp= new list; cout<<"Enter Value: "; cin>>temp->data; temp->ptr=NULL; start=temp; cout<<"Value successfully Added"<<endl; } else{ temp= new list; cout<<"Enter Value: "; cin>>temp->data; temp->ptr=NULL; temp1=start; while(temp1->ptr!=NULL) { temp1=temp1->ptr; } temp1->ptr=temp; }}
Add a number after a specific number in Linked List
void add_after_no(){ if(start==NULL) { cout<<"Error!List is Empty"<<endl; } else{ int n; cout<<"Enter the Number: "; cin>>n; temp=start; while(temp->ptr!=NULL) { if(temp->data==n) list *temp2; temp2=temp->ptr; temp1=new list; cout<<"Enter Value: "; cin>>temp1->data; temp->ptr=temp1; temp1->ptr=temp2; cout<<"Value Successfully Added"<<endl; break; } temp=temp->ptr;}}}
Add a number Before a specific number in Linked List
void add_before_no(){ if(start==NULL) { cout<<"Error!List is Empty"<<endl; } else{ int n; cout<<"Enter the Number: "; cin>>n; temp=start; list *temp2; list *temp3; while(temp->ptr!=NULL) { temp1=temp; temp=temp->ptr; if(temp->data==n) { temp2=new list; cout<<"Enter Value: "; cin>>temp2->data; temp3=temp1->ptr; temp1->ptr=temp2; temp2->ptr=temp; cout<<"Value Successfully Added"<<endl; break;}}}}
Remove number from end in Linked List
void remove_frm_end(){ if(start==NULL) { cout<<"Error!List is Empty"<<endl; } else{ temp=start; if(start->ptr==NULL) { delete start; start=NULL; cout<<"Successfully Removed "<<endl; } else{ while(temp->ptr!=NULL) { temp1=temp; temp=temp->ptr; } delete temp; temp1->ptr=NULL; cout<<"Successfully Removed "<<endl; }}}
Remove from start in Linked List
void remove_frm_start(){ if(start==NULL) { cout<<"Error!List is Empty"<<endl; } else{ temp=start->ptr; delete start; start=temp; cout<<"Value Successfully Removed From Start"<<endl; }}
Remove by number in Linked List
void remove_by_no(){ if(start==NULL) { cout<<"Error!List is Empty"<<endl;} else{ int v; cout<<"Enter value: "; cin>>v; if(start->data==v) { start=start->ptr; cout<<"Sucessfully Removed"<<endl; } else { temp=start; while(temp->ptr!=NULL) { temp1=temp; temp=temp->ptr; if(temp->data==v) { temp1->ptr=temp->ptr; delete temp; cout<<"Sucessfully Removed"<<endl; break;}}}}}
Display function of Linked List
void show(){ if(start==NULL) cout<<"List is Empty"<<endl; else { temp=start; while(temp!=NULL) { cout<<temp->data<<endl; temp=temp->ptr;}}}
Display Even Numbers of Linked List
void even(){ cout<<"Even Numbers in List:"<<endl; temp=start; while(temp!=NULL) { if(temp->data%2==0) cout<<temp->data<<endl; temp=temp->ptr;}}
Display Odd Numbers of Linked List
void odd(){ cout<<"Even Numbers in List:"<<endl; temp=start; while(temp!=NULL) { if(temp->data%2==1) cout<<temp->data<<endl; temp=temp->ptr; }}