01 Linked Lists
01 Linked Lists
1. Linked Lists
Lists:
In general lists are like arrays that are used to store ordered data.
A List is a linear sequence of data objects of same type.
Or
List is a collection of multiple elements arranged one after another in linear order.
Linked Lists :
(Using Dynamic memory allocation i.e. Node implementation) :
Thus linked list is a linear collection of Nodes where each node holds link for next
Node in the list. The first node holds link for the second node, second holds link
for third and so on. The last Node holds Null link to indicate its a last element in
the list.
Each node consists of two main fields: Information i.e. Data and Link i.e. Reference for next element.
Data
Link
Node
Fig. 1
1.Linke d Lis ts
1 of 24
www.santoshkabirsir.com
For example, a linked list created to store student information may have students roll no, name and height as
an information field in a node.
Link is nothing but a reference i.e. memory address of a node. In C programs link is a Pointer for a node.
Singly linked list consists of one or more nodes with each node holding
reference for a next element in the list.
The last node holds a null reference to indicate end of a list.
The reference for the first node is kept in a reference variable called root.
New nodes can be added to the list and any existing nodes can be removed.
One can traverse the list from in forward direction in the list.
Following figure shows a Singly linked list with four elements i.e. nodes with data
A, B, C and D. A is first node and D is a last node.
Black filled circles indicate links to next nodes and hollow circle indicates null link.
A
Circular linked list consists of one or more nodes with each node holding
reference (pointer) for a next element in the list.
The last node holds a reference to the root i.e. first node of a list.
The reference for the first node is kept in a reference variable called root.
New nodes can be added to the list and any existing nodes can be removed.
One can traverse the list in forward direction but circularly.
Following figure shows a Circular linked list with four elements i.e. nodes with
data A, B, C and D. A is first node and D is a last node.
Black filled circles indicate links to next nodes.
A
1.Linke d Lis ts
2 of 24
www.santoshkabirsir.com
Doubly linked list consists of one or more nodes with each node holding
references (pointers) for next and previous elements in the list.
Each node has two link fields ( i.e. two pointers). The pointers are called as
forward (next) pointer and backward (previous) pointer.
The forward pointer of last node and backward pointer of root hold Null.
The reference for the first node is kept in a reference variable called root.
New nodes can be added to the list and any existing nodes can be removed.
One can traverse the list in forward direction as well as backward direction.
Thus, from any nth node in a list we can move towards root or towards end
of list. Thus, the list is more flexible.
Following figure shows a Doubly linked list with four elements i.e. nodes with data
A, B, C and D. A is first node and D is a last node.
Black filled circles indicate links to next/previous nodes.
A
1.Linke d Lis ts
3 of 24
www.santoshkabirsir.com
1.Linke d Lis ts
4 of 24
www.santoshkabirsir.com
Note :
Complete program for Singly Linked List will be Discussed and Written in class.
1.Linke d Lis ts
5 of 24
www.santoshkabirsir.com
1.Linke d Lis ts
6 of 24
www.santoshkabirsir.com
Adding a node:
An algorithm for adding a node to the circular linked list is same as that for
simple linked list (See algorithm A-1 for linear linked list). One difference is that
the link in a newly allocated node will hold reference for the first node in the list.
Thus, in the algorithm add new step at the end as follows,
Put root pointer in new nodes link.
1.Linke d Lis ts
7 of 24
www.santoshkabirsir.com
*root=NULL, *last=NULL;
1.Linke d Lis ts
8 of 24
www.santoshkabirsir.com
Display( void );
InsertBefore( int p, int v);
InsertAfter( int p, int V);
DeleteNode(int p);
1.Linke d Lis ts
9 of 24
www.santoshkabirsir.com
1.Linke d Lis ts
10 of 24
www.santoshkabirsir.com
1.Linke d Lis ts
11 of 24
www.santoshkabirsir.com
Removing a node:
Algorithm to remove an item from the list,
A- 9
Notes prepared by: Santosh Kabir
Mobile : 98336 29398
1.Linke d Lis ts
12 of 24
www.santoshkabirsir.com
1.Linke d Lis ts
13 of 24
www.santoshkabirsir.com
*root=NULL, *last=NULL;
CreateList( int n );
Display( void );
InsertBefore( int p, int v);
InsertAfter( int p, int V);
DeleteNode(int p);
void Display()
{
struct Node *r;
if( root == NULL )
printf("List is empty\n");
else
{
r = root;
printf("Nodes...\n");
while( r != NULL )
{
printf("%d\t", r->data);
Notes prepared by: Santosh Kabir
Mobile : 98336 29398
1.Linke d Lis ts
14 of 24
www.santoshkabirsir.com
}
}
void InsertBefore( int p, int v)
{
struct Node *cur, *pr, *nw;
int found = 0;
cur = root;
while( cur != NULL )
{
if( cur -> data == p )
{
found = 1;
break;
}
else
{
pr = cur;
cur = cur->next;
}
}
if( found )
{
nw = (struct Node*) malloc( sizeof( struct Node) );
nw->data = v;
if( cur == root )
{
nw->next = root;
nw->prev = NULL;
root = nw;
}
else
{
pr->next = nw;
nw->prev = pr;
nw->next = cur;
cur->prev = nw;
}
}
}
void InsertAfter( int p, int v)
{
struct Node *cur,*nxt, *nw;
int found = 0;
cur = root;
while( cur != NULL )
{
if( cur -> data == p )
{
found = 1;
break;
Notes prepared by: Santosh Kabir
Mobile : 98336 29398
1.Linke d Lis ts
15 of 24
www.santoshkabirsir.com
}
else
cur = cur->next;
if( found )
{
nw = (struct Node*) malloc( sizeof( struct Node) );
nw->data = v;
if( cur->next == NULL )
{
cur->next = nw;
nw->prev = cur;
nw->next = NULL;
last = nw;
}
else
{
nxt = cur->next;
cur->next = nw;
nw->prev = cur;
nw->next = nxt;
}
}
}
void DeleteNode( int p )
{
struct Node *cur, *pr, *nxt;
int found =0;
cur = root;
while( cur != NULL )
{
if( cur -> data == p )
{
found =1;
break;
}
else
{
pr = cur;
cur = cur->next;
}
}
if( found )
{
if( cur == root )
{
root = root->next;
root->prev= NULL;
}
else if( cur->next == NULL )
{
pr->next = NULL;
Notes prepared by: Santosh Kabir
Mobile : 98336 29398
1.Linke d Lis ts
16 of 24
www.santoshkabirsir.com
last = pr;
}
else
{
nxt = cur->next;
pr->next = nxt;
nxt->prev = pr;
}
free( cur );
next
10
20
30
40
-1
1.Linke d Lis ts
17 of 24
www.santoshkabirsir.com
if(first== -1)
first=0;
return p;
void AddNode(int v)
{
int p;
p = GetNode();
A[p].data = v;
last = p;
}
void InsertAfter(int pos, int val)
{
int p;
if(pos == -1 || pos == avail)
printf("\n Invaliid position ...\n");
else if(pos == last)
AddNode(val);
else
{
p = GetNode();
A[p].data = val;
A[p].next = A[pos].next;
Notes prepared by: Santosh Kabir
Mobile : 98336 29398
1.Linke d Lis ts
18 of 24
www.santoshkabirsir.com
void FreeNode(int p)
{
if( p == first )
{
first = A[p].next;
}
A[p].next =avail;
avail = p;
}
int DeleteNode(int pos)
{
int cur=first, prev=-1;
int v; // to store value at given pos.
while(cur != pos)
{
prev = cur;
cur = A[cur].next;
}
v = A[pos].data;
A[prev].next = A[pos].next;
FreeNode(pos);
A[last].next = avail;
return(v);
}
void Display()
{
int r=first;
if( r != -1)
{
printf("\nList elements...\n");
while( r != avail)
{
printf("%d\t", A[r].data );
r = A[r].next;
}
}
else
{
printf("\nList is empty ...\n");
}
}
void main()
{
int d;
Notes prepared by: Santosh Kabir
Mobile : 98336 29398
1.Linke d Lis ts
19 of 24
www.santoshkabirsir.com
1.Linke d Lis ts
20 of 24
www.santoshkabirsir.com
}
}
getch();
1.Linke d Lis ts
21 of 24
www.santoshkabirsir.com
1.Linke d Lis ts
22 of 24
www.santoshkabirsir.com
}
if( r1 != NULL || r2 != NULL )
v= 0;
}
return v;
}
4. Creating a Sorted Linked list:
The sorted linked stores nodes in the ascending order of values of the nodes. i.e.
Node with the smallest value will be the first element of the list and that with the
largest value will be the last node of the list.
Following function inserts a new node at the proper position in the list so that the
list nodes are in the ascending order. If the new node has smallest value it will be
inserted as a root node and a node value is larger than the other nodes the new
node is added at the end of list. ( Assume singly linked list)
void createSortedList(int val)
{
struct Node *cur=NULL, *prev=NULL, *nw;
nw = malloc(sizeof(struct Node));
nw->data = val;
nw->next = NULL;
if(root==NULL) // if list is empty
{
root=nw;
last=nw;
}
else if( val > last->data) // if value is greater than last node value
{
last->next =nw;
last = nw;
}
else
{
cur=root;
while(cur->data < val) // move in the list to get proper position
{
prev=cur;
cur=cur->next;
}
if(cur==root) // to insert node at beginning
{
nw->next = root;
root=nw;
}
1.Linke d Lis ts
23 of 24
www.santoshkabirsir.com
Sem II
Sem IV
* With practicals *
By Santosh Kabir sir.
1.Linke d Lis ts
24 of 24
www.santoshkabirsir.com