SlideShare a Scribd company logo
Linked list
• Linked lists are a type of linear data structure. They consist of nodes
containing the data and a reference (link) needed to move on to the
next item.
Node
The head pointer contains the address of the first node. Null means the end of the linked list.
head
100 200 500
Linked lists can be of multiple types: singly, doubly, and circular linked list
100
12 200 14 500 67 NULL
Data Next
Creating a single node of a single linked list
in c
• #include <stdio.h>
• #include<stdlib.h>
• struct node
• {
• int data;
• struct node* next;
• };
• int main()
• {
• struct node *head= NULL;
• head = (struct node*)malloc(sizeof(struct
node));
• head->data= 45;
• head->next=NULL;
• printf("%d", head->data);
• return 0;
• }
Creating a single node of a single linked
list in c++
• #include<iostream>
• using namespace std;
• struct node
• {
• int data;
• node* next;
• };
• int main()
• {
• node *head= NULL;
• head = new node();
• head->data= 45;
• head->next=NULL;
• cout<< head->data;
• return 0;
• }
Creating a single linked list in c
• #include <stdio.h>
• #include<stdlib.h>
• struct node
• {
• int data;
• struct node* next;
• };
• int main()
• {
• struct node *head= NULL;
• head = (struct node*)malloc(sizeof(struct node));
• head->data= 45;
• head->next=NULL;
• printf("%dn", head->data);
• struct node *second= NULL;
• second = (struct node*)malloc(sizeof(struct node));
• second->data= 98;
• second->next=NULL;
• head->next=second;
• printf("%d", second->data);
• return 0;
• }
Creating a single linked list in c++
• #include<iostream>
• using namespace std;
• struct node
• {
• int data;
• node* next;
• };
• int main()
• {
• node *head= NULL;
• head = new node();
• head->data= 45;
• head->next=NULL;
• cout<< head->data<<endl;
• node *second= NULL;
• second = new node();
• second->data= 98;
• second->next=NULL;
• head->next= second;
• cout<< second->data;
• return 0;
• }
Single Linked List
• #include<iostream>
• using namespace std;
• struct node {
• int data;
• node* next; };
• int main()
• { node *head= NULL;
• head = new node();
• head->data= 45;
• head->next=NULL;
• cout<< head->next<<endl;
•
• node *second= NULL;
• second = new node();
• second->data= 98;
• second->next=NULL;
• head->next= second;
• cout<< head->next<<endl;
• node*third =NULL;
• third= new node();
• third->data= 198;
• third->next=NULL;
• second->next= third;
• cout<< second->next<<endl;
•
• //Traversing of linked list
• while (head != NULL) {
• cout << head->data<<endl;
• head = head->next;
• }
• return 0; }
Algorithm for Traversing of linked list
Traversing means to process each node exactly once.
• Step 1: [INITIALIZE] SET PTR = HEAD
• Step 2: Repeat Steps 3 and 4 while PTR != NULL
• Step 3: Apply process to PTR -> DATA
• Step 4: SET PTR = PTR->NEXT
• [END OF LOOP]
• Step 5: EXIT
Second method (not moving a head pointer)
Creating a single linked list in c
• #include <stdio.h>
• #include<stdlib.h>
• struct node {
• int data;
• struct node* next; };
• int main()
• { struct node *head= NULL;
• head = (struct node*)malloc(sizeof(struct node));
• head->data= 45;
• head->next=NULL;
• printf("%dn", head->data);
• struct node *second= NULL;
• second = (struct node*)malloc(sizeof(struct node));
• second->data= 98;
• second->next=NULL;
• head->next=second;
• printf("%dn", second->data);
• second = (struct node*)malloc(sizeof(struct node));
• second->data= 158;
• second->next=NULL;
• head->next->next=second;
• printf("%d", second->data);
• return 0; }
Creating a single linked list in c++
• #include<iostream>
• using namespace std;
• struct node {
• int data;
• node* next; };
• int main()
• { node *head= NULL;
• head = new node();
• head->data= 45;
• head->next=NULL;
• cout<< head->data<<endl;
• node *second= NULL;
• second = new node();
• second->data= 98;
• second->next=NULL;
• head->next= second;
• cout<< second->data<<endl;
• second->data= 158;
• second->next=NULL;
• head->next->next= second;
• cout<< second->data;
• return 0; }
ALGORITHM OF OFINSERTION NODE AT THE BEGINNING LINKED LIST
Lets START is the pointer having the initial position in linked list.
Let data be the element to be inserted in the new node.
POS is the mark where the new node is to be inserted.
TEMP is a temporary pointer to hold the node address.
1. Input data
2. Develop a New Node
3. New Node → DATA = data
4. New Node → Next = NULL
5. If (START equal to NULL)
then START = New Node.
Inserting the node at the BEGINNING of Single Linked List
• #include<iostream>
• using namespace std;
• struct node {
• int data;
• node* next; };
• void insertion(node**head,int ND) //double pointer
• {
• node *newnode= NULL;
• newnode = new node();
• newnode->data= ND;
• newnode-> next = *head; //Link address part
• *head= newnode; //Make newNode as first node
• }
• int main()
• { node *head= NULL;
• head = new node();
• head->data= 15;
• head->next=NULL;
• node *second= NULL;
• second = new node();
• second->data= 18;
• second->next=NULL;
• head->next= second;
•
• node*third =NULL;
• third= new node();
• third->data= 198;
• third->next=NULL;
• second->next= third;
•
• insertion(&head,1); //call by value
• //Traversing of linked list
• while (head != NULL) {
• cout << head->data<<endl;
• head = head->next;
• }
• return 0; }
Inserting the node at a CERTAIN position (or after node)of Single Linked List
• #include<iostream>
• using namespace std;
• struct node {
• int data;
• node* next; };
• void insertatcertain(node*head,int ND,int p)
• {
• node *nn= head;
• node *newnode= NULL;
• newnode = new node();
• newnode->data = ND;
• p--;
• while(p!=1)
• {
• nn=nn->next;
• p--;
• }
• newnode->next = nn->next;
• nn->next = newnode;
• }
• int main()
• { node *head= NULL;
• head = new node();
• head->data= 45;
• head->next=NULL;
• node *second= NULL;
• second = new node();
• second->data= 98;
• second->next=NULL;
• head->next= second;
• node*third =NULL ;
• third= new node();
• third->data= 198;
• third->next=NULL;
• second->next= third;
• int p;
• cout<<"at which position u want to add node = ";
• cin>>p;
• insertatcertain(head,100,p);
• //Traversing of linked list
• while (head != NULL) {
• cout << head->data<<endl;
• head = head->next;
• }
• return 0; }
Algorithm to Inserting the node at the END of Single Linked List
Begin
1. Input DATA
2. develop a New Node
3. New Node → DATA = DATA
4. New Node → Next = NULL
5. If (START equal to NULL)
(a) START = New Node
6. Else
(a) TEMP = START
(b) While (TEMP → Next not equal to NULL)
(i) TEMP = TEMP → Next
7. TEMP → Next = New Node
Exit
Inserting the node at the END of Single Linked List
• #include <iostream>
• using namespace std;
• struct node
• {
• int data;
• node*next; };
• void insertatend(node*temp,int val)
• {
• node*end= NULL;
• end= new node();
• end->data = val;
• end->next=NULL;
• while(temp->next != NULL) {
• temp = temp->next;
• }
• temp->next = end;
• }
• int main()
• {
• node*head= NULL;
• head= new node();
• head->data = 12;
• head->next =NULL;
•
• node*one = NULL;
• one= new node();
• one->data = 13;
• one->next =NULL;
• head->next =one;
• node*two = NULL;
• two= new node();
• two->data = 14;
• two->next =NULL;
• one->next =two;
• node*three = NULL;
• three= new node();
• three->data = 15;
• three->next =NULL;
• two->next =three;
• insertatend(head,99);
• while(head!=NULL)
• {
• cout<<head->data<<endl;
• head = head->next;
• }
• return 0;
• }
Algorithm to Deleting a node from the beginning
• Step 1: IF HEAD = NULL
• Write UNDERFLOW
• Go to Step 5
• else
• Step 2: SET PTR = HEAD
• Step 3: SET HEAD = HEAD -> NEXT
• Step 4: delete PTR
• Step 5: EXIT
Deleting a node from the beginning
• #include <iostream>
• using namespace std;
• struct node
• {
• int data;
• node*next;
• };
• void del(node**head)
• {
• if (*head==NULL)
• { cout<<"list is empty";
• }
• else
• { node*temp=*head;
• *head=(*head)->next;
• delete temp;
• } }
• int main()
• { node*head= NULL;
• head= new node();
• head->data = 12;
• head->next =NULL;
• node*one = NULL;
• one= new node();
• one->data = 13;
• one->next =NULL;
• head->next =one;
• node*two = NULL;
• two= new node();
• two->data = 14;
• two->next =NULL;
• one->next =two;
• del(&head);
• while(head!=NULL)
• { cout<<head->data<<endl;
• head = head->next; }
• return 0; }
Algorithm to Deleting a node from the end
• IF HEAD = NULL
• Write UNDERFLOW
• Go to Step 8
• else
• Step 2: SET PTR = HEAD
• Step 3: Repeat Steps 4 and 5
• while PTR -> NEXT != NULL
• Step 4: SET PREPTR = PTR
• Step 5: SET PTR = PTR -> NEXT
• [END OF while LOOP]
• Step 6: SET PREPTR -> NEXT = NULL
• Step 7: delete PTR
• Step 8: EXIT
Deleting a node from the END
• #include <iostream>
• using namespace std;
• struct node
• {
• int data;
• node*next;
• };
• void del(node**head)
• { if (*head==NULL)
• { cout<<"list is empty";
• }
• else if ((*head)->next==NULL)
• { delete head;
• *head=NULL;
• }
• else
• { node*temp=*head;
• node*pnode=NULL;
• while(temp->next!=NULL)
• { pnode=temp;
• temp=temp->next;
• }
• pnode->next=NULL;
• delete temp; } }
• int main()
• { node*head= NULL;
• head= new node();
• head->data = 12;
• head->next =NULL;
• node*one = NULL;
• one= new node();
• one->data = 13;
• one->next =NULL;
• head->next =one;
• node*two = NULL;
• two= new node();
• two->data = 14;
• two->next =NULL;
• one->next =two;
• del(&head);
• while(head!=NULL)
• { cout<<head->data<<endl;
• head = head->next; }
• return 0; }
Algorithm to Deletion after a given node in Linked List
• Step 1: IF HEAD = NULL
• Write UNDERFLOW
• Go to Step 10
• else
• Step 2: SET PTR = HEAD
• Step 3: SET PREPTR = PTR
• Step 4: Repeat Steps 5 and 6
• while PREPTR -> DATA != NUM
• Step 5: SET PREPTR = PTR
• Step 6: SET PTR = PTR -> NEXT
• [END OF while LOOP]
• Step 7: SET TEMP = PTR
• Step 8: SET PREPTR -> NEXT = PTR -> NEXT
• Step 9: delete TEMP
• Step 10 : EXIT
Deletion after a given node in Linked List
• #include <iostream>
• using namespace std;
• struct node
• {
• int data;
• node*next;
• };
• void delafter(node**head)
• {
• node*temp=(*head)->next;
• (*head)->next = temp->next;
• delete temp;
• }
• int main()
• { node*head= NULL;
• head= new node();
• head->data = 12;
• head->next =NULL;
• node*one = NULL;
• one= new node();
• one->data = 13;
• one->next =NULL;
• head->next =one;
• node*two = NULL;
• two= new node();
• two->data = 14;
• two->next =NULL;
• one->next =two;
• delafter(&head);
• while(head!=NULL)
• { cout<<head->data<<endl;
• head = head->next; }
• return 0; }
Searching an element in Linked List
• #include <iostream>
• using namespace std;
• struct node
• {
• int data;
• node*next;
• };
• void search(node*head, int val )
• { while(head!=NULL) {
• if(head->data == val) {
• cout << "element found"<<endl;
• return; }
• head = head->next; }
• cout << "No"<<endl;
• }
• int main()
• {
• node*head= NULL;
• head= new node();
• head->data = 12;
• head->next =NULL;
• node*one = NULL;
• one= new node();
• one->data = 13;
• one->next =NULL;
• head->next =one;
• node*two = NULL;
• two= new node();
• two->data = 14;
• two->next =NULL;
• one->next =two;
• search(head,56);
• while(head!=NULL)
• { cout<<head->data<<endl;
• head = head->next; }
• return 0; }

More Related Content

PPTX
Linked List - Insertion & Deletion
DOCX
Lab Week 2 Game Programming.docx
PPTX
DSA(1).pptx
PPTX
DS-3asdfghjklxmmcnaefiuhavbifuhablc.pptx
DOCX
Implement of c &amp; its coding programming by sarmad baloch
PPT
Doublylinklist
PPTX
Doubly & Circular Linked Lists
PPTX
linked list in DSA using C++ explained.pptx
Linked List - Insertion & Deletion
Lab Week 2 Game Programming.docx
DSA(1).pptx
DS-3asdfghjklxmmcnaefiuhavbifuhablc.pptx
Implement of c &amp; its coding programming by sarmad baloch
Doublylinklist
Doubly & Circular Linked Lists
linked list in DSA using C++ explained.pptx

Similar to How to sort linked list using sorthing method.pptx (20)

PPT
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
PPTX
Unit II Data Structure 2hr topic - List - Operations.pptx
PDF
Rewrite this code so it can use a generic type instead of integers. .pdf
PPTX
DoublyLinkedList.pptx
PPT
Linked list introduction and different operation
PPTX
Revisiting a data structures in detail with linked list stack and queue
PDF
Singly linked list
PPTX
Implemention of Linked list concept in Data Structures
PPT
DOCX
#include stdafx.h #include iostream using namespace std;vo.docx
PPTX
Singly linked list.pptx
PPTX
DS group binary tree all information M.pptx
DOC
Ds 2 cycle
PPTX
class 2.pptxdsafe fnbwfl;d,qgyewbjdkqwd;wd
PPTX
class 2.pptxdsdhwdhwdbgkjbdqwnddbqsddbbd
PDF
program on string in java Lab file 2 (3-year)
PPT
LinkedDoublyLists.ppt
PDF
c++ Computational Complexity filling in the following three .pdf
PDF
Lab-2.2 717822E504.pdf
PPT
LinkedDoublyLists (1).ppt dll is node to node.
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
Unit II Data Structure 2hr topic - List - Operations.pptx
Rewrite this code so it can use a generic type instead of integers. .pdf
DoublyLinkedList.pptx
Linked list introduction and different operation
Revisiting a data structures in detail with linked list stack and queue
Singly linked list
Implemention of Linked list concept in Data Structures
#include stdafx.h #include iostream using namespace std;vo.docx
Singly linked list.pptx
DS group binary tree all information M.pptx
Ds 2 cycle
class 2.pptxdsafe fnbwfl;d,qgyewbjdkqwd;wd
class 2.pptxdsdhwdhwdbgkjbdqwnddbqsddbbd
program on string in java Lab file 2 (3-year)
LinkedDoublyLists.ppt
c++ Computational Complexity filling in the following three .pdf
Lab-2.2 717822E504.pdf
LinkedDoublyLists (1).ppt dll is node to node.
Ad

Recently uploaded (20)

PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Welding lecture in detail for understanding
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
DOCX
573137875-Attendance-Management-System-original
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Geodesy 1.pptx...............................................
PDF
Well-logging-methods_new................
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Sustainable Sites - Green Building Construction
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Welding lecture in detail for understanding
Automation-in-Manufacturing-Chapter-Introduction.pdf
Model Code of Practice - Construction Work - 21102022 .pdf
573137875-Attendance-Management-System-original
OOP with Java - Java Introduction (Basics)
Geodesy 1.pptx...............................................
Well-logging-methods_new................
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Foundation to blockchain - A guide to Blockchain Tech
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Sustainable Sites - Green Building Construction
Lecture Notes Electrical Wiring System Components
Operating System & Kernel Study Guide-1 - converted.pdf
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Ad

How to sort linked list using sorthing method.pptx

  • 1. Linked list • Linked lists are a type of linear data structure. They consist of nodes containing the data and a reference (link) needed to move on to the next item. Node The head pointer contains the address of the first node. Null means the end of the linked list. head 100 200 500 Linked lists can be of multiple types: singly, doubly, and circular linked list 100 12 200 14 500 67 NULL Data Next
  • 2. Creating a single node of a single linked list in c • #include <stdio.h> • #include<stdlib.h> • struct node • { • int data; • struct node* next; • }; • int main() • { • struct node *head= NULL; • head = (struct node*)malloc(sizeof(struct node)); • head->data= 45; • head->next=NULL; • printf("%d", head->data); • return 0; • } Creating a single node of a single linked list in c++ • #include<iostream> • using namespace std; • struct node • { • int data; • node* next; • }; • int main() • { • node *head= NULL; • head = new node(); • head->data= 45; • head->next=NULL; • cout<< head->data; • return 0; • }
  • 3. Creating a single linked list in c • #include <stdio.h> • #include<stdlib.h> • struct node • { • int data; • struct node* next; • }; • int main() • { • struct node *head= NULL; • head = (struct node*)malloc(sizeof(struct node)); • head->data= 45; • head->next=NULL; • printf("%dn", head->data); • struct node *second= NULL; • second = (struct node*)malloc(sizeof(struct node)); • second->data= 98; • second->next=NULL; • head->next=second; • printf("%d", second->data); • return 0; • } Creating a single linked list in c++ • #include<iostream> • using namespace std; • struct node • { • int data; • node* next; • }; • int main() • { • node *head= NULL; • head = new node(); • head->data= 45; • head->next=NULL; • cout<< head->data<<endl; • node *second= NULL; • second = new node(); • second->data= 98; • second->next=NULL; • head->next= second; • cout<< second->data; • return 0; • }
  • 4. Single Linked List • #include<iostream> • using namespace std; • struct node { • int data; • node* next; }; • int main() • { node *head= NULL; • head = new node(); • head->data= 45; • head->next=NULL; • cout<< head->next<<endl; • • node *second= NULL; • second = new node(); • second->data= 98; • second->next=NULL; • head->next= second; • cout<< head->next<<endl; • node*third =NULL; • third= new node(); • third->data= 198; • third->next=NULL; • second->next= third; • cout<< second->next<<endl; • • //Traversing of linked list • while (head != NULL) { • cout << head->data<<endl; • head = head->next; • } • return 0; }
  • 5. Algorithm for Traversing of linked list Traversing means to process each node exactly once. • Step 1: [INITIALIZE] SET PTR = HEAD • Step 2: Repeat Steps 3 and 4 while PTR != NULL • Step 3: Apply process to PTR -> DATA • Step 4: SET PTR = PTR->NEXT • [END OF LOOP] • Step 5: EXIT
  • 6. Second method (not moving a head pointer) Creating a single linked list in c • #include <stdio.h> • #include<stdlib.h> • struct node { • int data; • struct node* next; }; • int main() • { struct node *head= NULL; • head = (struct node*)malloc(sizeof(struct node)); • head->data= 45; • head->next=NULL; • printf("%dn", head->data); • struct node *second= NULL; • second = (struct node*)malloc(sizeof(struct node)); • second->data= 98; • second->next=NULL; • head->next=second; • printf("%dn", second->data); • second = (struct node*)malloc(sizeof(struct node)); • second->data= 158; • second->next=NULL; • head->next->next=second; • printf("%d", second->data); • return 0; } Creating a single linked list in c++ • #include<iostream> • using namespace std; • struct node { • int data; • node* next; }; • int main() • { node *head= NULL; • head = new node(); • head->data= 45; • head->next=NULL; • cout<< head->data<<endl; • node *second= NULL; • second = new node(); • second->data= 98; • second->next=NULL; • head->next= second; • cout<< second->data<<endl; • second->data= 158; • second->next=NULL; • head->next->next= second; • cout<< second->data; • return 0; }
  • 7. ALGORITHM OF OFINSERTION NODE AT THE BEGINNING LINKED LIST Lets START is the pointer having the initial position in linked list. Let data be the element to be inserted in the new node. POS is the mark where the new node is to be inserted. TEMP is a temporary pointer to hold the node address. 1. Input data 2. Develop a New Node 3. New Node → DATA = data 4. New Node → Next = NULL 5. If (START equal to NULL) then START = New Node.
  • 8. Inserting the node at the BEGINNING of Single Linked List • #include<iostream> • using namespace std; • struct node { • int data; • node* next; }; • void insertion(node**head,int ND) //double pointer • { • node *newnode= NULL; • newnode = new node(); • newnode->data= ND; • newnode-> next = *head; //Link address part • *head= newnode; //Make newNode as first node • } • int main() • { node *head= NULL; • head = new node(); • head->data= 15; • head->next=NULL; • node *second= NULL; • second = new node(); • second->data= 18; • second->next=NULL; • head->next= second; • • node*third =NULL; • third= new node(); • third->data= 198; • third->next=NULL; • second->next= third; • • insertion(&head,1); //call by value • //Traversing of linked list • while (head != NULL) { • cout << head->data<<endl; • head = head->next; • } • return 0; }
  • 9. Inserting the node at a CERTAIN position (or after node)of Single Linked List • #include<iostream> • using namespace std; • struct node { • int data; • node* next; }; • void insertatcertain(node*head,int ND,int p) • { • node *nn= head; • node *newnode= NULL; • newnode = new node(); • newnode->data = ND; • p--; • while(p!=1) • { • nn=nn->next; • p--; • } • newnode->next = nn->next; • nn->next = newnode; • } • int main() • { node *head= NULL; • head = new node(); • head->data= 45; • head->next=NULL; • node *second= NULL; • second = new node(); • second->data= 98; • second->next=NULL; • head->next= second; • node*third =NULL ; • third= new node(); • third->data= 198; • third->next=NULL; • second->next= third; • int p; • cout<<"at which position u want to add node = "; • cin>>p; • insertatcertain(head,100,p); • //Traversing of linked list • while (head != NULL) { • cout << head->data<<endl; • head = head->next; • } • return 0; }
  • 10. Algorithm to Inserting the node at the END of Single Linked List Begin 1. Input DATA 2. develop a New Node 3. New Node → DATA = DATA 4. New Node → Next = NULL 5. If (START equal to NULL) (a) START = New Node 6. Else (a) TEMP = START (b) While (TEMP → Next not equal to NULL) (i) TEMP = TEMP → Next 7. TEMP → Next = New Node Exit
  • 11. Inserting the node at the END of Single Linked List • #include <iostream> • using namespace std; • struct node • { • int data; • node*next; }; • void insertatend(node*temp,int val) • { • node*end= NULL; • end= new node(); • end->data = val; • end->next=NULL; • while(temp->next != NULL) { • temp = temp->next; • } • temp->next = end; • } • int main() • { • node*head= NULL; • head= new node(); • head->data = 12; • head->next =NULL; • • node*one = NULL; • one= new node(); • one->data = 13; • one->next =NULL; • head->next =one; • node*two = NULL; • two= new node(); • two->data = 14; • two->next =NULL; • one->next =two; • node*three = NULL; • three= new node(); • three->data = 15; • three->next =NULL; • two->next =three; • insertatend(head,99); • while(head!=NULL) • { • cout<<head->data<<endl; • head = head->next; • } • return 0; • }
  • 12. Algorithm to Deleting a node from the beginning • Step 1: IF HEAD = NULL • Write UNDERFLOW • Go to Step 5 • else • Step 2: SET PTR = HEAD • Step 3: SET HEAD = HEAD -> NEXT • Step 4: delete PTR • Step 5: EXIT
  • 13. Deleting a node from the beginning • #include <iostream> • using namespace std; • struct node • { • int data; • node*next; • }; • void del(node**head) • { • if (*head==NULL) • { cout<<"list is empty"; • } • else • { node*temp=*head; • *head=(*head)->next; • delete temp; • } } • int main() • { node*head= NULL; • head= new node(); • head->data = 12; • head->next =NULL; • node*one = NULL; • one= new node(); • one->data = 13; • one->next =NULL; • head->next =one; • node*two = NULL; • two= new node(); • two->data = 14; • two->next =NULL; • one->next =two; • del(&head); • while(head!=NULL) • { cout<<head->data<<endl; • head = head->next; } • return 0; }
  • 14. Algorithm to Deleting a node from the end • IF HEAD = NULL • Write UNDERFLOW • Go to Step 8 • else • Step 2: SET PTR = HEAD • Step 3: Repeat Steps 4 and 5 • while PTR -> NEXT != NULL • Step 4: SET PREPTR = PTR • Step 5: SET PTR = PTR -> NEXT • [END OF while LOOP] • Step 6: SET PREPTR -> NEXT = NULL • Step 7: delete PTR • Step 8: EXIT
  • 15. Deleting a node from the END • #include <iostream> • using namespace std; • struct node • { • int data; • node*next; • }; • void del(node**head) • { if (*head==NULL) • { cout<<"list is empty"; • } • else if ((*head)->next==NULL) • { delete head; • *head=NULL; • } • else • { node*temp=*head; • node*pnode=NULL; • while(temp->next!=NULL) • { pnode=temp; • temp=temp->next; • } • pnode->next=NULL; • delete temp; } } • int main() • { node*head= NULL; • head= new node(); • head->data = 12; • head->next =NULL; • node*one = NULL; • one= new node(); • one->data = 13; • one->next =NULL; • head->next =one; • node*two = NULL; • two= new node(); • two->data = 14; • two->next =NULL; • one->next =two; • del(&head); • while(head!=NULL) • { cout<<head->data<<endl; • head = head->next; } • return 0; }
  • 16. Algorithm to Deletion after a given node in Linked List • Step 1: IF HEAD = NULL • Write UNDERFLOW • Go to Step 10 • else • Step 2: SET PTR = HEAD • Step 3: SET PREPTR = PTR • Step 4: Repeat Steps 5 and 6 • while PREPTR -> DATA != NUM • Step 5: SET PREPTR = PTR • Step 6: SET PTR = PTR -> NEXT • [END OF while LOOP] • Step 7: SET TEMP = PTR • Step 8: SET PREPTR -> NEXT = PTR -> NEXT • Step 9: delete TEMP • Step 10 : EXIT
  • 17. Deletion after a given node in Linked List • #include <iostream> • using namespace std; • struct node • { • int data; • node*next; • }; • void delafter(node**head) • { • node*temp=(*head)->next; • (*head)->next = temp->next; • delete temp; • } • int main() • { node*head= NULL; • head= new node(); • head->data = 12; • head->next =NULL; • node*one = NULL; • one= new node(); • one->data = 13; • one->next =NULL; • head->next =one; • node*two = NULL; • two= new node(); • two->data = 14; • two->next =NULL; • one->next =two; • delafter(&head); • while(head!=NULL) • { cout<<head->data<<endl; • head = head->next; } • return 0; }
  • 18. Searching an element in Linked List • #include <iostream> • using namespace std; • struct node • { • int data; • node*next; • }; • void search(node*head, int val ) • { while(head!=NULL) { • if(head->data == val) { • cout << "element found"<<endl; • return; } • head = head->next; } • cout << "No"<<endl; • } • int main() • { • node*head= NULL; • head= new node(); • head->data = 12; • head->next =NULL; • node*one = NULL; • one= new node(); • one->data = 13; • one->next =NULL; • head->next =one; • node*two = NULL; • two= new node(); • two->data = 14; • two->next =NULL; • one->next =two; • search(head,56); • while(head!=NULL) • { cout<<head->data<<endl; • head = head->next; } • return 0; }