SlideShare a Scribd company logo
1 of 17
Download to read offline
Linked List
 It is a list or collection of data items that can be stored in scattered locations (positions) in
memory.
 To store data in scattered locations in memory we have to make link between one data item
to another. So, each data item or element must have two parts: one is data part another is
link (pointer) part.
 Each data item of a linked list is called a node.
 Data part contains (holds) actual data (information) and the link part points to the next node
of the list.
 To locate the list an external pointer is used that pints the first node of the list. The link part
of the last node will not point to any node. So, it will be null. This type of list is called
linear (one way) linked list or simply linked list.
Figure 1: A single node
Figure 2: Graphical representation of a linear linked list
Node declaration and store data in a node
1. Node declaration
struct node
{
int data;
node *next;
};
2. Data store:
node *nptr;
nptr -> data = 20;
nptr -> next = NULL;
Create a new node
1. Node declaration
struct node
{
int data;
node *next;
};
2. Declare variables (pointer type) that point to the node:
node *nptr;
3. Allocate memory for new node:
nptr = new(node);
4. Insert node value:
nptr -> data = 55;
nptr -> next = NULL;
Algorithm (pseudo code) to create a linked list
1. Declare node and pointers (list, tptr, nptr):
i. struct node
{
int data;
node *next;
};
ii. node *list, *tptr, *nptr;
2. Create an empty list:
list = NULL;
3. Create a new node:
nptr = new(node);
nptr -> data = item;
nptr -> next = NULL;
4. Make link between the linked list and the new node:
if (list==NULL)
{
list = nptr;
tptr = nptr;
}
else
{
tptr -> next = nptr;
tptr = nptr;
}
4. Output linked list.
Note: Step 3 and 4 will be repeated again and again if two or more nodes are to be added in the
list.
Comments: Here, nptr is a pointer that points to a new node, The tptr is the pointer that points
the last node, which has been already added. item is a variable using which we shall enter data to
the new node.
Figure 3: Addition of nodes to a linked list (pictorial view)
Sample Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
struct node
{
int data;
node *next;
}*nptr,*tptr,*list;
int main()
{
list=NULL;
printf("/*Type negative number(n<0) when finished.*/nEnter the
datan");
do
{
nptr=(node *)malloc(sizeof(node));
scanf("%d",&nptr->data);
nptr->next=NULL;
if(nptr->data<0)
{
nptr=NULL;
}
else
{
if(list==NULL)
{
list=nptr;
tptr=nptr;
}
else
{
tptr->next=nptr;
tptr=nptr;
}
}
}while(nptr!=NULL);
printf("Linked List: ");
while(list!=NULL)
{
printf("%d=>",list->data);
list=list->next;
}
printf("NULLn");
getch();
}
Sample Output
/*Type negative number(n<0) when finished.*/
Enter the data
2 3 4 7 1 -9
Linked List: 2=>3=>4=>7=>1=>NULL
Algorithm (pseudo code) to search a node from a linked list
1. Declare node and tptr
2. Input the values to be located:
item = 51;
3. Search the item:
tptr = list;
while (tptr->data != item and tptr->next != NULL)
{
tptr = tptr->next;
}
4. Output:
if (tptr -> data = item)
print “FOUND”
else print “NOT FOUND”
Sample Code
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
struct node
{
int data;
node *next;
}*nptr,*tptr,*list;
int main()
{
list=NULL;
printf("/*Type (n<0) when finished.*/nEnter the datan");
do
{
nptr=(node *)malloc(sizeof(node));
scanf("%d",&nptr->data);
nptr->next=NULL;
if(nptr->data<0)
{
nptr=NULL;
}
else
{
if(list==NULL)
{
list=nptr;
tptr=nptr;
}
else
{
tptr->next=nptr;
tptr=nptr;
}
}
}while(nptr!=NULL);
printf("Enter an item: ");
nptr=(node *)malloc(sizeof(node));
scanf("%d",&nptr->data);
nptr->next=NULL;
tptr=list;
while((tptr->data!=nptr->data)&&(tptr->next!=NULL))
{
tptr=tptr->next;
}
if(tptr->data==nptr->data)
{
printf("Found");
}
else
{
printf("Not Found");
}
getch();
}
Sample Output
Case 1:
/*Type (n<0) when finished.*/
Enter the data
2 3 6 9 1 -22
Enter an item: 6
Found
Case 2:
/*Type (n<0) when finished.*/
Enter the data
3 4 8 1 -9
Enter an item: 10
Not Found
Algorithm (pseudo code) to insert a node into a linked list
[Here, we shall consider insertion at the beginning of the first node]
1. Declare node and pointers (list, tptr, nptr)
2. Input linked list (we have to use an existing list)
3. Create a new node
nptr = new(node);
nptr -> data = item;
nptr -> next = NULL;
4. Insert the new node at the beginning of the first node
tptr=list;
list=nptr;
nptr->next=tptr;
Sample Code
/*insert a node as a first node in a list*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
struct node
{
int data;
node *next;
}*nptr,*tptr,*list;
int main()
{
list=NULL;
printf("/*Type (n<0) when finished.*/nEnter the datan");
do
{
nptr=(node *)malloc(sizeof(node));
scanf("%d",&nptr->data);
nptr->next=NULL;
if(nptr->data<0)
{
nptr=NULL;
}
else
{
if(list==NULL)
tptr
list
{
list=nptr;
tptr=nptr;
}
else
{
tptr->next=nptr;
tptr=nptr;
}
}
}while(nptr!=NULL);
printf("Enter an item: ");
nptr=(node *)malloc(sizeof(node));
scanf("%d",&nptr->data);
nptr->next=NULL;
tptr=list;
list=nptr;
nptr->next=tptr;
printf("Updated Linked List: ");
while(list!=NULL)
{
printf("%d=>",list->data);
list=list->next;
}
printf("NULLn");
getch();
}
Sample Output
/*Type (n<0) when finished.*/
Enter the data
2 3 6 9 -2
Enter an item: 22
Updated Linked List: 22=>2=>3=>6=>9=>NULL
Algorithm (pseudo code) to insert a node into a linked list
[Here, we shall consider insertion after the last node]
1. Declare node and pointers (list, tptr, nptr)
2. Input linked list (we have to use an existing list)
3. Create a new node
nptr = new(node);
nptr -> data = item;
nptr -> next = NULL;
4. Insert the new node after the last node:
tptr->next=nptr;
Sample Code
/*insert a node as a last in a list*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
struct node
{
int data;
node *next;
}*nptr,*tptr,*list,*n;
int main()
{
list=NULL;
printf("/*Type negative number(n<0) when finished.*/nEnter the
datan");
do
{
nptr=(node *)malloc(sizeof(node));
scanf("%d",&nptr->data);
nptr->next=NULL;
if(nptr->data<0)
{
nptr=NULL;
}
else
{
if(list==NULL)
{
list=nptr;
tptr=nptr;
}
else
{
tptr->next=nptr;
tptr=nptr;
}
}
tptr
}while(nptr!=NULL);
printf("Enter an item: ");
nptr=(node *)malloc(sizeof(node));
scanf("%d",&nptr->data);
nptr->next=NULL;
tptr->next=nptr;
printf("Updated List: ");
while(list!=NULL)
{
printf("%d=>",list->data);
list=list->next;
}
printf("NULLn");
getch();
}
Sample Output
/*Type negative number(n<0) when finished.*/
Enter the data
1 3 7 0 4 -44
Enter an item: 22
Updated List: 1=>3=>7=>0=>4=>22=>NULL
Algorithm (pseudo code) to insert a node into a linked list
[Here, we shall consider insertion after the first node or before the last node in an ascending list]
1. Declare node and pointers (list, tptr, nptr)
2. Input linked list (we have to use an existing list)
3. Create a new node
nptr = new(node);
nptr -> data = item;
nptr -> next = NULL;
4. Locate the appropriate position for the new node:
tptr = list;
while (tptr -> next -> data < nptr -> data)
{
tptr = tptr -> next;
}
5. Insert new node at appropriate position
nptr -> next = tptr -> next;
tptr -> next = nptr;
6. Updated linked list.
Sample Code
/*insert a node in the list as a middle point*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
struct node
{
int data;
node *next;
}*nptr,*tptr,*list;
int main()
{
list=NULL;
printf("/*Type (n<0) when finished.*/nEnter the datan");
do
{
nptr=(node *)malloc(sizeof(node));
scanf("%d",&nptr->data);
nptr->next=NULL;
if(nptr->data<0)
{
nptr=NULL;
}
else
{
if(list==NULL)
{
list=nptr;
tptr=nptr;
}
else
{
tptr->next=nptr;
tptr=nptr;
}
}
}while(nptr!=NULL);
printf("nEnter an item: ");
nptr=(node *)malloc(sizeof(node));
scanf("%d",&nptr->data);
nptr->next=NULL;
tptr=list;
while(tptr->next->data<nptr->data)
tptr=tptr->next;
nptr->next=tptr->next;
tptr->next=nptr;
printf("Updated Linked List: ");
while(list!=NULL)
{
printf("%d=>",list->data);
list=list->next;
}
printf("NULLn");
getch();
}
Sample Output
/*Type (n<0) when finished.*/
Enter the data
2 3 7 8 -66
Enter an item: 4
Updated Linked List: 2=>3=>4=>7=>8=>NULL
Algorithm (pseudo code) to delete a node from a linked list
[Here, we shall not consider deletion process of the first node and the last node in the list]
1. Declare node and pointers (list, tptr, pptr)
2. Input linked list and the item (that is to be deleted)
3. Search the item to be deleted in the list:
tptr = list;
while (tptr -> data !=item)
{
pptr =tptr;
tptr = tptr -> next;
}
4. Delete the node
pptr -> next = tptr -> next;
delete (tptr);
5. Output: Updated linked lists.
Sample Code
/*deleting a node in a list*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
struct node
{
int data;
node *next;
}*nptr,*tptr,*list,*pptr;
int main()
{
list=NULL;
printf("/*Type negative number(n<0) when finished.*/nEnter the
datan");
do
{
nptr=(node *)malloc(sizeof(node));
scanf("%d",&nptr->data);
nptr->next=NULL;
if(nptr->data<0)
{
nptr=NULL;
}
else
{
if(list==NULL)
{
list=nptr;
tptr=nptr;
}
else
{
tptr->next=nptr;
tptr=nptr;
}
}
}while(nptr!=NULL);
printf("Enter deleting item: ");
nptr=(node *)malloc(sizeof(node));
scanf("%d",&nptr->data);
nptr->next=NULL;
tptr=list;
while(tptr->data!=nptr->data)
{
pptr=tptr;
tptr=tptr->next;
}
pptr->next=tptr->next;
printf("Updated Linked List: ");
while(list!=NULL)
{
printf("%d=>",list->data);
list=list->next;
}
printf("NULLn");
getch();
}
Sample Output
Case 1:
/*Type negative number(n<0) when finished.*/
Enter the data
2 6 9 1 3 5 -55
Enter deleting item: 1
Updated Linked List: 2=>6=>9=>3=>5=>NULL
Case 2:
/*Type negative number(n<0) when finished.*/
Enter the data
2 5 7 9 22 6 -99
Enter deleting item: 6
Updated Linked List: 2=>5=>7=>9=>22=>NULL

More Related Content

What's hot

linked list
linked listlinked list
linked listAbbott
 
Data structure lecture 5
Data structure lecture 5Data structure lecture 5
Data structure lecture 5Kumar
 
Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationProf Ansari
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structureTushar Aneyrao
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queueRajkiran Nadar
 
Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Adam Mukharil Bachtiar
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Muhammad Hammad Waseem
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked listsstudent
 
Linked list
Linked listLinked list
Linked listVONI
 

What's hot (20)

linked list
linked listlinked list
linked list
 
Linked list
Linked listLinked list
Linked list
 
Data structure lecture 5
Data structure lecture 5Data structure lecture 5
Data structure lecture 5
 
linked list
linked listlinked list
linked list
 
Link List
Link ListLink List
Link List
 
Linked lists
Linked listsLinked lists
Linked lists
 
Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory Allocation
 
Single linked list
Single linked listSingle linked list
Single linked list
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structure
 
Team 10
Team 10Team 10
Team 10
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Linked list
Linked listLinked list
Linked list
 
CSE240 Doubly Linked Lists
CSE240 Doubly Linked ListsCSE240 Doubly Linked Lists
CSE240 Doubly Linked Lists
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked lists
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 

Similar to Linked list

Data structures linked list introduction.pptx
Data structures linked list introduction.pptxData structures linked list introduction.pptx
Data structures linked list introduction.pptxKalpana Mohan
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked ListReazul Islam
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfEricvtJFraserr
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptxchin463670
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfKylaMaeGarcia1
 
1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdfsudhinjv
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfvishalateen
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfrohit219406
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked listAmit Vats
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdffathimahardwareelect
 
10- Consider the following data structure used for implementing a link.docx
10- Consider the following data structure used for implementing a link.docx10- Consider the following data structure used for implementing a link.docx
10- Consider the following data structure used for implementing a link.docxtodd991
 

Similar to Linked list (20)

Data structures linked list introduction.pptx
Data structures linked list introduction.pptxData structures linked list introduction.pptx
Data structures linked list introduction.pptx
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptx
 
Ll.pptx
Ll.pptxLl.pptx
Ll.pptx
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 
1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
 
linked-list.ppt
linked-list.pptlinked-list.ppt
linked-list.ppt
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked list
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
10- Consider the following data structure used for implementing a link.docx
10- Consider the following data structure used for implementing a link.docx10- Consider the following data structure used for implementing a link.docx
10- Consider the following data structure used for implementing a link.docx
 

More from A. S. M. Shafi (20)

2D Transformation in Computer Graphics
2D Transformation in Computer Graphics2D Transformation in Computer Graphics
2D Transformation in Computer Graphics
 
3D Transformation in Computer Graphics
3D Transformation in Computer Graphics3D Transformation in Computer Graphics
3D Transformation in Computer Graphics
 
Projection
ProjectionProjection
Projection
 
2D Transformation
2D Transformation2D Transformation
2D Transformation
 
Line drawing algorithm
Line drawing algorithmLine drawing algorithm
Line drawing algorithm
 
Fragmentation
FragmentationFragmentation
Fragmentation
 
File organization
File organizationFile organization
File organization
 
Bankers algorithm
Bankers algorithmBankers algorithm
Bankers algorithm
 
RR and priority scheduling
RR and priority schedulingRR and priority scheduling
RR and priority scheduling
 
Fcfs and sjf
Fcfs and sjfFcfs and sjf
Fcfs and sjf
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
1D Array
1D Array1D Array
1D Array
 
2D array
2D array2D array
2D array
 
Stack push pop
Stack push popStack push pop
Stack push pop
 
Queue
QueueQueue
Queue
 
Searching
SearchingSearching
Searching
 
Sorting
SortingSorting
Sorting
 
Sum of subset problem
Sum of subset problemSum of subset problem
Sum of subset problem
 
Quick sort
Quick sortQuick sort
Quick sort
 
N queens problem
N queens problemN queens problem
N queens problem
 

Recently uploaded

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 

Recently uploaded (20)

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 

Linked list

  • 1. Linked List  It is a list or collection of data items that can be stored in scattered locations (positions) in memory.  To store data in scattered locations in memory we have to make link between one data item to another. So, each data item or element must have two parts: one is data part another is link (pointer) part.  Each data item of a linked list is called a node.  Data part contains (holds) actual data (information) and the link part points to the next node of the list.  To locate the list an external pointer is used that pints the first node of the list. The link part of the last node will not point to any node. So, it will be null. This type of list is called linear (one way) linked list or simply linked list. Figure 1: A single node Figure 2: Graphical representation of a linear linked list Node declaration and store data in a node 1. Node declaration struct node { int data; node *next; };
  • 2. 2. Data store: node *nptr; nptr -> data = 20; nptr -> next = NULL; Create a new node 1. Node declaration struct node { int data; node *next; }; 2. Declare variables (pointer type) that point to the node: node *nptr; 3. Allocate memory for new node: nptr = new(node); 4. Insert node value: nptr -> data = 55; nptr -> next = NULL; Algorithm (pseudo code) to create a linked list 1. Declare node and pointers (list, tptr, nptr): i. struct node { int data; node *next; }; ii. node *list, *tptr, *nptr; 2. Create an empty list: list = NULL; 3. Create a new node: nptr = new(node); nptr -> data = item; nptr -> next = NULL;
  • 3. 4. Make link between the linked list and the new node: if (list==NULL) { list = nptr; tptr = nptr; } else { tptr -> next = nptr; tptr = nptr; } 4. Output linked list. Note: Step 3 and 4 will be repeated again and again if two or more nodes are to be added in the list. Comments: Here, nptr is a pointer that points to a new node, The tptr is the pointer that points the last node, which has been already added. item is a variable using which we shall enter data to the new node. Figure 3: Addition of nodes to a linked list (pictorial view) Sample Code: #include<stdio.h> #include<conio.h>
  • 4. #include<stdlib.h> #define NULL 0 struct node { int data; node *next; }*nptr,*tptr,*list; int main() { list=NULL; printf("/*Type negative number(n<0) when finished.*/nEnter the datan"); do { nptr=(node *)malloc(sizeof(node)); scanf("%d",&nptr->data); nptr->next=NULL; if(nptr->data<0) { nptr=NULL; } else { if(list==NULL) { list=nptr; tptr=nptr; } else
  • 5. { tptr->next=nptr; tptr=nptr; } } }while(nptr!=NULL); printf("Linked List: "); while(list!=NULL) { printf("%d=>",list->data); list=list->next; } printf("NULLn"); getch(); } Sample Output /*Type negative number(n<0) when finished.*/ Enter the data 2 3 4 7 1 -9 Linked List: 2=>3=>4=>7=>1=>NULL Algorithm (pseudo code) to search a node from a linked list 1. Declare node and tptr 2. Input the values to be located: item = 51; 3. Search the item: tptr = list; while (tptr->data != item and tptr->next != NULL) { tptr = tptr->next; }
  • 6. 4. Output: if (tptr -> data = item) print “FOUND” else print “NOT FOUND” Sample Code #include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct node { int data; node *next; }*nptr,*tptr,*list; int main() { list=NULL; printf("/*Type (n<0) when finished.*/nEnter the datan"); do { nptr=(node *)malloc(sizeof(node)); scanf("%d",&nptr->data); nptr->next=NULL; if(nptr->data<0) { nptr=NULL; } else { if(list==NULL) { list=nptr; tptr=nptr; } else
  • 7. { tptr->next=nptr; tptr=nptr; } } }while(nptr!=NULL); printf("Enter an item: "); nptr=(node *)malloc(sizeof(node)); scanf("%d",&nptr->data); nptr->next=NULL; tptr=list; while((tptr->data!=nptr->data)&&(tptr->next!=NULL)) { tptr=tptr->next; } if(tptr->data==nptr->data) { printf("Found"); } else { printf("Not Found"); } getch(); } Sample Output Case 1: /*Type (n<0) when finished.*/ Enter the data 2 3 6 9 1 -22 Enter an item: 6 Found Case 2: /*Type (n<0) when finished.*/ Enter the data 3 4 8 1 -9 Enter an item: 10 Not Found Algorithm (pseudo code) to insert a node into a linked list [Here, we shall consider insertion at the beginning of the first node] 1. Declare node and pointers (list, tptr, nptr)
  • 8. 2. Input linked list (we have to use an existing list) 3. Create a new node nptr = new(node); nptr -> data = item; nptr -> next = NULL; 4. Insert the new node at the beginning of the first node tptr=list; list=nptr; nptr->next=tptr; Sample Code /*insert a node as a first node in a list*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct node { int data; node *next; }*nptr,*tptr,*list; int main() { list=NULL; printf("/*Type (n<0) when finished.*/nEnter the datan"); do { nptr=(node *)malloc(sizeof(node)); scanf("%d",&nptr->data); nptr->next=NULL; if(nptr->data<0) { nptr=NULL; } else { if(list==NULL) tptr list
  • 9. { list=nptr; tptr=nptr; } else { tptr->next=nptr; tptr=nptr; } } }while(nptr!=NULL); printf("Enter an item: "); nptr=(node *)malloc(sizeof(node)); scanf("%d",&nptr->data); nptr->next=NULL; tptr=list; list=nptr; nptr->next=tptr; printf("Updated Linked List: "); while(list!=NULL) { printf("%d=>",list->data); list=list->next; } printf("NULLn"); getch(); } Sample Output /*Type (n<0) when finished.*/ Enter the data 2 3 6 9 -2 Enter an item: 22 Updated Linked List: 22=>2=>3=>6=>9=>NULL Algorithm (pseudo code) to insert a node into a linked list [Here, we shall consider insertion after the last node] 1. Declare node and pointers (list, tptr, nptr) 2. Input linked list (we have to use an existing list) 3. Create a new node nptr = new(node); nptr -> data = item; nptr -> next = NULL;
  • 10. 4. Insert the new node after the last node: tptr->next=nptr; Sample Code /*insert a node as a last in a list*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct node { int data; node *next; }*nptr,*tptr,*list,*n; int main() { list=NULL; printf("/*Type negative number(n<0) when finished.*/nEnter the datan"); do { nptr=(node *)malloc(sizeof(node)); scanf("%d",&nptr->data); nptr->next=NULL; if(nptr->data<0) { nptr=NULL; } else { if(list==NULL) { list=nptr; tptr=nptr; } else { tptr->next=nptr; tptr=nptr; } } tptr
  • 11. }while(nptr!=NULL); printf("Enter an item: "); nptr=(node *)malloc(sizeof(node)); scanf("%d",&nptr->data); nptr->next=NULL; tptr->next=nptr; printf("Updated List: "); while(list!=NULL) { printf("%d=>",list->data); list=list->next; } printf("NULLn"); getch(); } Sample Output /*Type negative number(n<0) when finished.*/ Enter the data 1 3 7 0 4 -44 Enter an item: 22 Updated List: 1=>3=>7=>0=>4=>22=>NULL Algorithm (pseudo code) to insert a node into a linked list [Here, we shall consider insertion after the first node or before the last node in an ascending list] 1. Declare node and pointers (list, tptr, nptr) 2. Input linked list (we have to use an existing list) 3. Create a new node nptr = new(node); nptr -> data = item; nptr -> next = NULL; 4. Locate the appropriate position for the new node: tptr = list; while (tptr -> next -> data < nptr -> data) { tptr = tptr -> next; }
  • 12. 5. Insert new node at appropriate position nptr -> next = tptr -> next; tptr -> next = nptr; 6. Updated linked list. Sample Code /*insert a node in the list as a middle point*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct node { int data; node *next; }*nptr,*tptr,*list; int main() { list=NULL; printf("/*Type (n<0) when finished.*/nEnter the datan"); do { nptr=(node *)malloc(sizeof(node)); scanf("%d",&nptr->data); nptr->next=NULL; if(nptr->data<0) {
  • 13. nptr=NULL; } else { if(list==NULL) { list=nptr; tptr=nptr; } else { tptr->next=nptr; tptr=nptr; } } }while(nptr!=NULL); printf("nEnter an item: "); nptr=(node *)malloc(sizeof(node)); scanf("%d",&nptr->data); nptr->next=NULL; tptr=list; while(tptr->next->data<nptr->data) tptr=tptr->next; nptr->next=tptr->next; tptr->next=nptr; printf("Updated Linked List: "); while(list!=NULL)
  • 14. { printf("%d=>",list->data); list=list->next; } printf("NULLn"); getch(); } Sample Output /*Type (n<0) when finished.*/ Enter the data 2 3 7 8 -66 Enter an item: 4 Updated Linked List: 2=>3=>4=>7=>8=>NULL Algorithm (pseudo code) to delete a node from a linked list [Here, we shall not consider deletion process of the first node and the last node in the list] 1. Declare node and pointers (list, tptr, pptr) 2. Input linked list and the item (that is to be deleted) 3. Search the item to be deleted in the list: tptr = list; while (tptr -> data !=item) { pptr =tptr; tptr = tptr -> next; } 4. Delete the node pptr -> next = tptr -> next; delete (tptr); 5. Output: Updated linked lists.
  • 15. Sample Code /*deleting a node in a list*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct node { int data; node *next; }*nptr,*tptr,*list,*pptr; int main() { list=NULL; printf("/*Type negative number(n<0) when finished.*/nEnter the datan"); do { nptr=(node *)malloc(sizeof(node)); scanf("%d",&nptr->data); nptr->next=NULL; if(nptr->data<0) { nptr=NULL; }
  • 16. else { if(list==NULL) { list=nptr; tptr=nptr; } else { tptr->next=nptr; tptr=nptr; } } }while(nptr!=NULL); printf("Enter deleting item: "); nptr=(node *)malloc(sizeof(node)); scanf("%d",&nptr->data); nptr->next=NULL; tptr=list; while(tptr->data!=nptr->data) { pptr=tptr; tptr=tptr->next; }
  • 17. pptr->next=tptr->next; printf("Updated Linked List: "); while(list!=NULL) { printf("%d=>",list->data); list=list->next; } printf("NULLn"); getch(); } Sample Output Case 1: /*Type negative number(n<0) when finished.*/ Enter the data 2 6 9 1 3 5 -55 Enter deleting item: 1 Updated Linked List: 2=>6=>9=>3=>5=>NULL Case 2: /*Type negative number(n<0) when finished.*/ Enter the data 2 5 7 9 22 6 -99 Enter deleting item: 6 Updated Linked List: 2=>5=>7=>9=>22=>NULL