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

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

PPTX
Linked list
RahulGandhi110
 
PDF
Linked Lists.pdf
Kaynattariq1
 
PPTX
Data structures linked list introduction.pptx
Kalpana Mohan
 
PPTX
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
ssusere1e8b7
 
PPTX
singlelinkedlistasdfghzxcvbnmqwertyuiopa
rabailasghar3
 
PPTX
Linked list and its operations - Traversal
kasthurimukila
 
PPTX
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
shesnasuneer
 
PPT
Unit ii(dsc++)
Durga Devi
 
PPTX
Doubly & Circular Linked Lists
Afaq Mansoor Khan
 
PPTX
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
beshahashenafe20
 
DOCX
Bsf23006565 dsa 3rd assignment.docx............
XEON14
 
PPT
Operations on linked list
Sumathi Kv
 
DOCX
Singlelinked list
Lakshmi Sarvani Videla
 
PPT
lecture four of data structures :Linked List-ds.ppt
donemoremaregere376
 
PPTX
VCE Unit 02 (1).pptx
skilljiolms
 
PDF
Create a link list. Add some nodes to it, search and delete nodes fro.pdf
hadpadrrajeshh
 
PPT
Data Structures with C Linked List
Reazul Islam
 
PPTX
DSModule2.pptx
ChrisSosaJacob
 
PPTX
data structures lists operation of lists
muskans14
 
PPTX
Linked lists a
Khuram Shahzad
 
Linked list
RahulGandhi110
 
Linked Lists.pdf
Kaynattariq1
 
Data structures linked list introduction.pptx
Kalpana Mohan
 
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
ssusere1e8b7
 
singlelinkedlistasdfghzxcvbnmqwertyuiopa
rabailasghar3
 
Linked list and its operations - Traversal
kasthurimukila
 
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
shesnasuneer
 
Unit ii(dsc++)
Durga Devi
 
Doubly & Circular Linked Lists
Afaq Mansoor Khan
 
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
beshahashenafe20
 
Bsf23006565 dsa 3rd assignment.docx............
XEON14
 
Operations on linked list
Sumathi Kv
 
Singlelinked list
Lakshmi Sarvani Videla
 
lecture four of data structures :Linked List-ds.ppt
donemoremaregere376
 
VCE Unit 02 (1).pptx
skilljiolms
 
Create a link list. Add some nodes to it, search and delete nodes fro.pdf
hadpadrrajeshh
 
Data Structures with C Linked List
Reazul Islam
 
DSModule2.pptx
ChrisSosaJacob
 
data structures lists operation of lists
muskans14
 
Linked lists a
Khuram Shahzad
 

Recently uploaded (20)

PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PPTX
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
MRRS Strength and Durability of Concrete
CivilMythili
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
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; }