CH 2 Linear Data Structure - Linked List
CH 2 Linear Data Structure - Linked List
Prepared by:
Mihir Mishra
Assistant Professor
Computer Engineering Department
1
Outline
• Introduction
• Singly linked list
• Create
• Insert an element
• Delete an element
• Circular linked list
• Create
• Insert an element
• Delete an element
• Doubly linked list
• Create
• Insert an element
• Delete an element
2
Introduction to Linked List
• A list is a data structure/type, which contains elements of the same type arranged in a set of
continuous memory location.
• With lists it is possible for us to perform certain operations such as searching the list, sorting it,
printing it and so on.
• For example, array, which is a sequential structure.
• Operations such as searching can be very efficiently carried out using an array representation. But
insertion and deletion cannot be easily performed with an array representation.
• To insert a new item into a location in list, we must shift the array down to make room for new
item.
• Similarly, deleting an item from the list required that we shift up all the array elements following
the one to be deleted.
• Another drawback with array is that they are very adaptable since the size of the array must be
fixed in advanced. The size of the array cannot be increased or decreased during the execution of
the program.
3
Introduction to Linked List
void main() int a [100] 2 * 100 = 200 bytes
{ int n = 5 5 * 2 = 10 bytes
int a[100],n,i; 200 – 10 = 190 bytes (occupied by compiler but
not used by the program and no one can use it)
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i])
}
.
.
.
}
4
Introduction to Linked List
• Here total 100 bytes
0
• 0 to 25 (25 bytes) and 60 to 75 (15 bytes) total
40 bytes are free.
25
• Remaining 60 bytes are occupied.
• Now if you want to store int a[15]; So total 15
60
* 2 = 30 bytes are required to store this array.
• Though 40 bytes are free, this array can not be
75 stored in these memory blocks. Because an
array requires contagious memory location
but here it is in the blocks i.e. 25 bytes and 15
bytes.
99
5
Introduction to Linked List
• These problems can be overcome by considering another data structure called the linked list.
• A linked list represents a linear collection of item, called node. Nodes of a linked list can be
scattered about in the memory, they need not necessarily represent a set of consecutive memory
location.
• Each node in a linked list has two parts:
1. Information Part, which contains the data for the node
2. Linked Part, which gives the memory address of the next node in the list.
INFO LINK
node
6
Introduction to Linked List
• Figure shows an abstract representation of a linked list. Information part may consist of
one or more fields.
• Thus a linked list consists of a series of structures, which are not necessarily contiguous.
Each structure contains an information field and a linked, which is a pointer to the next
structure.
• An arrow is used to the linked list is member of each node to indicate the location of the
next node. The NULL in the link member of the last node signifies the end of the list.
head
3 5 7
7
Introduction to Linked List
head
3 5 7
• A singly linked list looks like that shown in the above figure.
• Head is a pointer, which gives the address of the first node in the linked list. Link
field of the last node contains the NULL, which indicates it does not contain any
address.
• Moving from one node to another by following a next reference is known as link
hopping or pointer hoping.
8
Introduction to Linked List
• We have seen that a linked list is a collection of nodes of the same type, so it can
be defined as follow:
struct node struct node
{ {
data_type info;
data_type info; struct node *link;
__________ *link; };
};
struct node
9
Introduction to Linked List
• A c program to create a linked list which N1 link = N2;
contains three nodes. N2 info = 7;
#include<stdio.h>
N2 link = N3;
#include<conio.h>
N3 info =9;
struct node N3 link = NULL;
{
printf(“\nLinked List\n”);
int info;
printf(“%d\t%d\t%d”,
struct node *link; N1info, N2 info, N3 info);
};
getch();
void main()
}
{
struct node *N1,*N2,*N3;
N1 info = 5;
10
Algorithm To Create a Singly Linked List
CREATE_LIST(HEAD, NewNode, element): • Step 4: [Set the content of the link part as NULL]
⁻ HEAD is a pointer used to hold the address of the first LINK[NewNode]=NULL
node of the linked list.
• Step 5: [Is the new node is the first node?]
⁻ NewNode is a pointer used to hold the address of the
new node. If the ans is YES then
⁻ CurrPtr is a pointer, which is updated always to the last [Connect the NewNode to the HEAD]
node in the linked list. HEAD=NewNode
⁻ Element is an element that will be inserted in a new Else
node.
a) [Take the CurrPtr to the last
⁻ This algorithm will generate a new node. Node of the linked list]
• Step 1: [Initialize the Head] CurrPtr=Address of the last Node
HEAD=NULL b) [Connect the NewNode to the
• Step 2: [Create a new node and store its address in link of the last node]
the pointer NewNode] LINK[CurrPtr]=NewNode
NewNode=Address of the new node c) Exit
• Step 3: [Copy the informationfor the new node to [End If]
the information part]
• Step 6: Return
INFO[NewNode]=element
11
Algorithm To Traverse a Singly Linked List
TRAVERSE_LIST(HEAD): • Step 2: [Perform the traversing
⁻ This algorithm traverses a linked list operation]
by applying a process to each While LINK[CurrPtr]!=NULL
element of the list. do
⁻ The variable CurrPtr points to the [Move pointer to next
node currently being processed. node]
⁻ HEAD is a pointer used to hold the CurrPtr=LINK[CurrPtr]
address of the first node of the
linked list. done
• Step 1: [Initialize a pointer to the [End of while operation]
HEAD] • Step 4: Exit
CurrPtr=HEAD
12
C Function to Create a Singly Linked List
void create_list(int element) else
{ {
struct node *NewNode,*CurrPtr; CurrPtr=HEAD;
NewNode=(struct node *) while(CurrPtr-> link!=NULL)
malloc(sizeof(struct node)); {
NewNodeinfo=element; CurrPtr=CurrPtrlink;
NewNodelink=NULL; }
if(HEAD==NULL) CurrPtrlink=NewNode;
{ }
HEAD=NewNode; }
}
13
Insert an Element In a Singly Linked List
• There are three different types of the insertion
1. Insert a node at the beginning of the linked list
2. Insert a node at the end of the linked list
3. Insert a node at the given position in the list
14
Insert A Node At The Beginning Of The Singly Linked List
HEAD
3 5 7
9
HEAD
3 5 7
9
HEAD
9 3 5 7
15
Insert A Node At The Beginning Of The Singly Linked List
INSERT_AT_BEG(HEAD,element): • Step 2: [Copy the information for the
⁻ Let the HEAD pointer hold the address new node to the info part]
of the first node of the linked list. INFO[NewNode]=element
⁻ The following algorithm will create a • Step 3:[Set the content of the link pat
NewNode and insert this node at the to contain the address of the
beginning of the linked list. first node]
⁻ This algorithm inserts element into the LINK[NewNode]=HEAD
list as the first node. • Step 4:[Make the NewNode as the first
• Step 1: [Create a new node and store node of the list]
its address in the pointer HEAD=NewNode
NewNode]
• Step 5: Exit/Return
NewNode=Address of the first
node
16
Insert A Node At The Beginning Of The Singly Linked List
INSERT_AT_BEG(struct node *HEAD,int element)
{
struct node *NewNode;
NewNode=(struct node *)malloc(sizeof(struct node)) ;
NewNode->info=element;
NewNode->link=HEAD;
HEAD=NewNode;
}
17
Insert A Node At The End Of The Singly Linked List
HEAD
3 5 7 9
HEAD
3 5 7 9
HEAD
3 5 7 9
18
Insert A Node At The End Of The Singly Linked List
INSERT_AT_END(HEAD,element): • Step 4: [Initialize HEAD value to CurrPtr]
⁻ Let list be store in memory. CurrPtr = HEAD
⁻ HEAD gives the address of the first node. • Step 5:[Find the last node of the list]
⁻ This algorithm insert element into the list as the While LINK[CurrPtr] != NULL
last node of the list. Do
⁻ CurrPtr is a pointer, which is pointing to the [Move CurrPtr to next node]
current node of the list.
CurrPtr = LINK[CurrPtr]
• Step 1: [Create a new node and store its address
in a pointer NewNode] Done
NewNode = Address of the new [While End]
node • Step 6: [Connect the new node to the link of the
• Step 2: [Copy the information for the new node in last node]
the information part of NewNode] LINK[CurrPtr] = NewNode
INFO[NewNode] = element • Step 7: Exit/Return
• Step 3: [Set the contents of link part to NULL]
LINK[NewNode] = NULL
19
Insert A Node At The End Of The Singly Linked List
void insert_at_end(struct node *HEAD, int element)
{
struct node *NewNode,*CurrPtr;
NewNode=(struct node *)malloc(sizeof(struct node));
NewNode info=element;
NewNode link=NULL;
CurrPtr=HEAD;
while(CurrPtr link != NULL)
{
CurrPtr = CurrPtr link;
}
CurrPtr link = NewNode;
}
20
Insert A Node At The Given Position In The Singly Linked List
HEAD
3 5 7
HEAD
9
3 5 7
Pos = 2
9
HEAD
3 9 5 7
21
Insert A Node At The Given Position In The Singly Linked List
INSERT_AT_POS(HEAD,ELEMENT,POS): Do
⁻ Let list be a list store in memory. [Move CurrPtr to next node]
⁻ HEAD gives the address of the first node. CurrPtr=LINK[CurrPtr
⁻ This algorithm insert element into the list as the given [End For]
position POS.
• Step 6: [Check whether the position is found or not?]
⁻ CurrPtr is a pointer which is pointing to the current
node of the list. If CurrPtr=NULL then
• Step 1:[Create a new node and store its address in the Print “Position out of range”
pointer NewNode] Exit
NewNode=Address of the new node Else
• Step 2: [Copy the information for the new node to the [Insert the NewNode at the required position]
info part]
LINK[NewNode]=LINK[CurrPtr]
INFO[NewNode]=element
LINK[CurrPtr]=NewNode
• Step 3: [Initialize HEAD value to CurrPtr]
[End If]
CurrPtr=HEAD
• Step 7: Exit/Return
• Step 4: [Move to required position]
For I=1 to POS-1 and CurrPtr!=NULL
22
Insert A Node At The Given Position In The Singly Linked List
INSERT_AT_POS(struct node *HEAD, int if(CurrPtr = = NULL)
element, int pos) {
{ printf(“Position out of range” );
struct node *NewNode,*CurrPtr; }
NewNode=(struct node *) else
malloc(sizeof(struct node)) ;
{
NewNode info=element;
NewNode link =
CurrPtr = HEAD;
CurrPtr link;
for(i=1; i < pos-1 && CurrPtr!=NULL ;
i++) CurrPtr link=NewNode;
{ }
CurrPtr=CurrPtr link; }
}
23