You are on page 1of 72

Linked List

By:
Dabal Singh Mahara
2017

1
Unit – 5
Contents Hours Marks
a. Concept and Definition
b. Inserting and deleting nodes
c. Linked List implementation of Stack (Push/Pop)
d. Linked list implementation of Queues
(Insert/Remove) 6 8
e. Circular List
• Stack as a Circular List (Push/Pop)
• Queue as a Circular List (Insert/Remove)
f. Doubly Linked List (Insert/Remove)

2
Concept and Definition
of Linked List
What is a linked list?
• If the memory is allocated at compile time of a program, then it is fixed and
cannot be changed.
• There is an alternative strategy to allocate memory only when it is required.
This special data structure is called linked list that provides a more flexible
storage system.
• Linked list is a linear collection of data elements called nodes, linked to one
another by means of the pointers.
• Each node is divided into two parts:
• The first part is data or info of the element
• The second part is link or pointer to next node in the list.

10 NULL

data next

4
Example Linked List

start 10 15 20 NULL

data next

• Start is a special pointer that points to the first node of the list and next field
of last node contains a special pointer known as NULL pointer.
• NULL indicates the end of linked list.
• If start == NULL then the list is empty.
Representation of Node
• The node is represented by a self-referential structure.
• That is, a structure that contains a reference to itself is known as self-
referential structure.
• node structure is given below:
struct node
{
int data;
struct node * next;
};
Advantages and Disadvantages

• Advantages:
o Linked list is dynamic data-structure that can grow and shrink during the
execution of a program. This feature helps in efficient memory utilization.
o Insertion and deletion are more easier and efficient.
• Disadvantages:
o More memory is needed to store a data item as an extra field for address is
allocated.
o Access to arbitrary element is a bit more cumbersome and time consuming.
Operations on Linked List
The basic operations performed on the linked list are as follows:
• Create: To create a linked list
• Insert: To insert a node at any specified position
• Delete: To delete a node from linked list
• Traversing: Going through all the nodes in the linked list
• Searching: This operation is used to find an element in the linked list.
• Concatenation: Process of appending second list to the end of the
first list.
Types of Linked List
• Basically, there are three types of linked lists:
• Singly Linked List
• Doubly Linked List
• Circular Linked List
• Singly Linked List: A singly linked list is a dynamic data structure in which each node has
two fields:
• One for data and another is link field.
• The data field stores the data value and link field stores address of next node in the
list.
• The first node is pointed by external pointer 'start' and node at last has a NULL
pointer.

10 15 20 NULL
start
data next
Fig. the singly linked list with three elements 10, 15 and 20
Representation of Singly Linked List
• Create a structure for a node with two fields: info and
next field as given below:

struct node
{ void createEmptyList()
int info; {
struct node * next;
}; start = NULL;
}
struct node *start;
Creating a node
• Creation of a node consists:
struct node *newnode;
newnode = (struct node *) malloc(sizeof(struct node));
newnode -> info = x;
newnode ->next = NULL;
• We can define a function to create a node that allocates memory for a
node dynamically and returns a pointer to newly created node.
• struct node *getnode()
{
struct node *newnode;
newnode = (struct node *) malloc(sizeof(struct node));
return (newnode);
}
Inserting a node in Linked List
• To insert a node into linked list, following three steps are performed:
 Allocating a node
 Assigning data to info field of node
 Adjusting a pointer
• The new node can be inserted :
• At the beginning of linked list
• At the end of the linked list
• At any specified position of the lined list
• Insertion requires obtaining a new node and changing tow links
Inserting at the beginning of List
• Algorithm: Let *start be the pointer to first node in the current list.
1. Create a new node using malloc function
i.e. newnode = (struct node *) malloc(sizeof(struct node));
2. Assign data to the info field of new node
i.e. newnode->info = x
3. Set next field of newnode to start.
i.e. newnode->next = start;
4. Set the start pointer to the newnode.
i.e. start = newnode;
5. end
C-function to insert a node at beginning
void insertBeg( int item)
{
struct node *newnode;
newnode = (struct node *) malloc(sizeof(struct node));
newnode->info = item;
newnode->next = start;
start = newnode;
}
Insert a node at End
• Let start be the pointer to the first node in the current list
• Algorithm:
1. Create a newnode using malloc function.
2. Assign data to the info field of new node.
3. Set: newnode->next = NULL;
4. If start = NULL then
set: start = newnode;
else
set: temp = start
while(temp->next != NULL)
temp = temp->next;
temp->next = newnode
5. end
C-function to insert a node at the End
void insertEnd( int item)
{
struct node *newnode, *temp;
newnode = (struct node *) malloc(sizeof(struct node));
newnode->info = item;

if(start==NULL)
start = newnode;
else
{
temp= start;

while(temp->next !=NULL)
temp = temp->next;

temp->next = newnode;
}
}
Insert a node at specified position
• Let *start be the pointer to first node.
1. Create a newnode using malloc function
2. Assign data to info of new node
3. Enter position of node at which the node to be inserted. Let position be pos.
4. if start == NULL
i. print "empty list"
ii. exit
5. else
i. temp = start
ii. for(i=1; i<pos-1; i++)
{ temp = temp->next;
if temp = null
print "can't inserted at given position" and exit }
6. Set NewNode ->next=temp->next
7. Set temp->next = NewNode
8. End
Exercise
• Write a C –function to Insert a node at specified
position in the linked list.
Deleting Nodes

A node may be deleted:


• From the beginning of the linked list
• From the end of the linked list
• From the specified position in a linked list
Deleting a node at the beginning
• Let *start be the pointer to first node in the current list.
1. If start == NULL then
print " The list is empty"
exit
2. temp = start
3. start = start - > next
4. free(temp)
5. end
C-function to delete a node at beginning
void deletebeg()
{
struct node * temp;
if(start == NULL)
{
printf(" Empty List");
exit(1);
}
else
{
temp = start;
printf("\n The deleted node is %d", start->info);
start = start ->next;
free(temp);
}
}
Deleting a node at the end
• Let *start be the pointer to first node in the current list.
1. If start == NULL then
print " The list is empty"
exit
2. else if start -> next == NULL
{
temp = start
start = NULL
free(temp)
}
3. else
{
temp = start
while(temp->next->next != NULL)
temp= temp -> next
free( temp -> next )
temp - > next = NULL
}
4. End
C-function to delete a node at the end
void deleteend()
{ else
struct node * temp;
{
if(start == NULL)
{ temp = start;
printf(" Empty List"); while( temp->next -> next != NULL)
exit(1);
temp = temp -> next;
}
else if (start -> next == NULL) free(temp->next);
{ temp->next = NULL;
temp = start;
}
printf("\n The deleted node is %d", start->info);
start = NULL;
free(temp);
}
Deleting a node at the specified position
• Let *start be the pointer to first node in the current list.
1. Read position of the node to be deleted. Let it be pos.
2. If start == NULL then
print " The list is empty"
exit
3. else
{
temp = start
for( i = 1; i < pos -1; i++)
{
temp= temp -> next
if temp = null
print "can't delete at given position" and exit
}
hold = temp->next
temp - > next = hold->next
free( hold )
}
4. End
Exercise

• Write C – function to delete a node at the


specified position.
Searching an item in the linked list
• To search an item in the linked list we need to find the node that
contains data value.
Algorithm: Let *start be the pointer to first node in the current list.
1. Read key to search.
2. If start == NULL then
print " The list is empty"
exit
3. else
{
temp = start
while( temp != NULL)
{
if( temp->info == key)
print " Key found" and break
temp = temp -> next
}
if ( temp == NULL)
print " Unsuccessful search"
}
4. End
Exercise

• Write C – function to search an item in the


linked list.
C-function to display all elements in the list
void display()
{
struct node * temp;
if(start == NULL)
{
printf(" Empty List");
exit(1);
}
else
{
temp = start;
printf("\n The list is: ");
while(temp != NULL)
{
printf(" %d\t", temp->info);
temp = temp ->next;
}
}
Linked List Implementation of Stack
• Let *top be a top of a stack or pointer to the first node of the list.
• struct node
{
int item;
struct node * next;
};

struct node * top = NULL;


Push Function
void push(int item)
{
struct node *nnode;
nnode=( struct node *)malloc(sizeof(struct node));

if(top==NULL)
{
nnode->info=item;
nnode->next=NULL;
top=nnode;
}
else
{
nnode->info=item;
nnode->next=top;
top=nnode;
}
}
Pop function
void pop()
{
struct node *temp;
if(top==NULL)
{
printf("Stack contain no elements:\n");
return;
}
else
{
temp=top;
top=top->next;
printf("\n deleted item is %d\t", temp->info);
free(temp);
}
}
Display Function
void display()
{
struct node *temp;
if(top==NULL)
{
printf("Stack is empty\n");
return;
}
else
{
temp=top;
printf("Stack items are:\n");
while(temp!=NULL)
{
printf("%d\t", temp->info);
temp=temp->next;
}
}
}
Linked list Implementation of Queue
struct node
{
int info; //Declaring an info field
struct node *next; // next pointer of node
};

struct node *front = NULL;


struct node *rear = NULL;
Enqueue Function
void insert()
{
int item;
struct node *nnode,*temp;

printf("\Enter the item\t ");


scanf("%d",&item);
nnode = (struct node *) malloc(sizeof(struct node));
nnode->info = item;
nnode->next = NULL;

if(front ==NULL)
front=rear = nnode;
else
{
rear->next = nnode;
rear = nnode;
}
}
Dequeue Function
int del()
{
int item; else
struct node *temp; {

if(front ==NULL) temp=front;


{ front = front->next;
printf("\n Empty List !!!");
printf("\n Deleted item is %d\n",
return 0;
temp->info);
}
else if(front->next==NULL) free(temp);
{ }
temp= front;
rear=front =NULL; }
printf("\n The deleted item is %d",
temp->info);
free(temp);
}
Display Function
int display()
{
struct node *temp;
if(front==NULL)
{
printf("\n The list is empty");
return 0;
}
else
{
temp=front;
printf("\n The list is:\t");
while(temp!=NULL)
{
printf(" %d -> ",temp->info);
temp=temp->next;
}
}

}
Circular Linked List
• A circular linked list is a list where next field of last node points to the very
first node of list.
• Circular linked list can be used to help the traverse he same list again and
again if needed. A circular list is very similar to the linear list where in the
circular list the pointer of the last node points not NULL but the first node.
• There is not last node in the circular list. But for convention, last pointer is
chosen to indicate the last node.
C-Representation of Circular List
• We declare structure for the circular list node in the same way as
done in the linear linked list
struct cnode
{
int info;
struct node * next;
};

struct cnode *start = NULL;


struct cnode *last = NULL;
Adding a node at the Beginning of of CLL
1. Create a new node as : newnode=(NodeType*)malloc(sizeof(NodeType));
2. if start==NULL then
newnode->info=item
newnode->next=newnode
start=newnode
last = newnode
end if
3. else
newnode->info=item
newnode->next=start
start=newnode
last->next=newnode
else end
4. End
Adding a node at the End of CLL
1. Create a new node as : newnode=(NodeType*) malloc(sizeof(NodeType));
2. if start==NULL then
newnode->info=item
newnode->next=newnode
start=newnode
last = newnode
end if
3. else
newnode->info=item
newnode->next=start
last->next=newnode
last = newnode
else end
4. End
Exercise

• Write C-functions to insert a node at:


• Beginning of CLL
• End of CLL
Adding a node at the Beginning of of CLL
if(start ==NULL)
void insert_beg() // inserting a node at the beginning
{
{ nnode->next = nnode;
start = nnode;
int item;
last = nnode;
struct node *nnode; }
else
printf("\Enter the item\t ");
{

scanf("%d",&item); nnode->next = start;


start = nnode;
nnode = (struct node *) malloc(sizeof(struct node)); last->next = nnode;
}
nnode->info = item;
}
Deleting a node at the beginning of CLL

1. if start==NULL then 3. else


print “empty list” and exit temp=start
2. else if start == last then start=start->next
temp = start print the deleted element=temp->info
last->next=start;
start = last = NULL
free(temp)
print " Deleted element: ", temp->info
end else
free(temp)
4. End
end else if
Deleting a node at the beginning of CLL
int del_beg()
{ else
int item; {
struct node *temp; temp= start;
if(start ==NULL)
{
printf("\n The deleted item is %d",start->info);
printf("\n Empty List !!!"); start = start->next;
return 0; last->next= start;
} free(temp);
else if(start==last) }
{
temp = start;
start=last = NULL; }
printf("\n The deleted node is %d",temp->info);
free(temp);
}
Deleting a node at the End of CLL
1. if start==NULL then hold=temp->next
print “empty list” and exit last=temp
last->next=start
2. else if start==last
print the deleted element=hold->info
set temp=start free(hold)
print "Deleted element", temp->info end else
free(temp) 4. End
start=last=NULL
3. else
temp=start
while( temp->next!=last)
temp=temp->next
end while
Deleting a node at the End of CLL
int del_end() start = last = NULL;
{ free(temp);
int item; }
else
struct node *temp,*hold;
{ temp= start;
if(start ==NULL)
while(temp->next!=last)
{
temp=temp->next;
printf("\n Empty List !!!");
hold = temp->next;
return 0;
last = temp;
} last->next = start;
else if( start==last) printf("\n The deleted item is %d",hold->info);
{ free(hold);
temp = start; }
printf("\n Deleted item is %d",start->info); }
Display Function in CLL
int display() temp=start;
{ printf("\n The list is:");
struct node *temp; while(temp!=last )
if(start==NULL) {
{ printf("%d ",temp->info);
printf("\n The list is empty"); temp=temp->next;
return 0; }
}
printf("%d",temp->info);
}
else
{
}
Stack as a Circular List
• A stack is a LIFO data structure.
• In a stack, the most recently inserted element is removed first out the
stack.
• A stack can be easily implemented using a circular list.
o Let pstack be a pointer to the last node of the circular list.
o First node is the top of the stack.
o An empty stack is represented by a null list.
Structure of a node
struct node
{
int info;
struct node * next;

};

struct node *pstack = NULL;


Push Operation
void push() // inserting a node at the beginning if(pstack ==NULL)
{ {
int item; nnode->next = nnode;
struct node *nnode; pstack = nnode;
}
printf("\Enter the item\t "); else
scanf("%d",&item); {
nnode = (struct node *) malloc(sizeof(struct node));
nnode->next = pstack->next;
pstack->next = nnode;
nnode->info = item;
}
}
Pop Operation
int pop() else if(pstack->next==pstack)
{ {
temp = pstack;
int item; pstack = NULL;
struct node *temp; printf("\n The deleted node is %d",temp->info);
free(temp);
if(pstack ==NULL) }
{ else
{
printf("\n Empty List !!!"); temp= pstack->next;
return 0; printf("\n The deleted item is %d",temp->info);
pstack->next = temp->next;
} free(temp);
}

}
Display Function
int display()
{
struct node *temp;
if(pstack==NULL)
{
printf("\n The list is empty");
return 0;
}
else
{
temp=pstack->next;
printf("\n The list is:");
while(temp!=pstack )
{
printf("%d ",temp->info);
temp=temp->next;
}
printf("%d",temp->info);
}

}
Queue as a Circular List
• A queue is a FIFO data structure.
• By using a circular list, a queue may be specified using a single pointer
q to that list.
• A node pointed by q is rear of the queue and the node following q is
its front.
q
Structure of a node
struct node
{
int info;
struct node * next;

};

struct node *q = NULL;


Insert Operation
void insert_node() if( q ==NULL)
{ {
int item; nnode->next = nnode;
q = nnode;
struct node *nnode;
}
else
printf("\Enter the item\t ");
{
scanf("%d",&item); nnode->next = q->next;
nnode = (struct node *) malloc(sizeof(struct node)); q->next = nnode;
nnode->info = item; q = nnode;
}
}
Delete Operation
int delete_node() else if( q->next== q)
{ {
temp = q;
int item; q = NULL;
struct node *temp; printf("\n The deleted node is %d",temp->info);
free(temp);
if( q ==NULL) }
{ else
{
printf("\n Empty List !!!"); temp= q->next;
return 0; printf("\n The deleted item is %d",temp->info);
q->next = temp->next;
} free(temp);
}

}
Display Function
int display()
{
struct node *temp;
if(q ==NULL)
{
printf("\n The list is empty");
return 0;
}
else
{
temp= q->next;
printf("\n The list is:");
while(temp!= q )
{
printf("%d ",temp->info);
temp=temp->next;
}
printf("%d",temp->info);
}

}
Doubly Linked List
• A linked list in which each node contains three fields: two pointer
fields and one data field is called doubly linked list.
• Two pointers link next node and previous nodes of a node.
• It provides bidirectional traversal.

start
NULL 10 5 7 NULL
Structure of a node
struct node
{
int info;
struct node * next;
struct node * prev;
};

struct node *start = NULL;


inserting a node at the beginning of DLL
void insert_beg()
{ nnode ->prev = NULL;
int item; start = nnode;

struct node *nnode; }


else
printf("\Enter the item\t");
{
scanf("%d",&item);
nnode->next= start;
nnode = (struct node *) malloc(sizeof(struct node));
nnode->prev= NULL;
nnode->info = item;
start->prev = nnode;
if(start == NULL) start = nnode;
{ }
nnode->next = NULL;
}
Inserting a Node at the End of DLL
void insert_end() else
{
int item; {
struct node *nnode,*temp; temp = start;
printf("\Enter the item\t "); while(temp->next!=NULL)
scanf("%d",&item);
nnode = (struct node *) malloc(sizeof(struct node)); temp=temp->next;
nnode->info = item; temp->next = nnode;
nnode->next = NULL;
nnode->prev = temp;
if(start ==NULL) }
{
}
start = nnode;
nnode->prev = NULL;
}
Deletion of a Node at Beginning of DLL
int del_beg() else
{ {
int item;
struct node *temp; temp= start;
printf("\n The deleted item is %d",start->info);
if(start ==NULL)
{ start = start->next;
printf("\n Empty List !!!"); start->prev= NULL;
return 0;
} free(temp);
else if (start->next==NULL) }
{
temp=start; }
start = NULL;
free(temp);
}
Delete a node at the End of DLL
int del_end()
{ else
int item; {
struct node *temp, *hold; temp= start;
if(start ==NULL) while(temp->next->next!=NULL)
{ temp=temp->next;
printf("\n Empty List !!!"); hold = temp->next;
return 0;
temp->next = NULL;
}
printf("\n The deleted item is %d",hold->info);
else if( start->next==NULL)
{ free(hold);
temp = start;
printf("\n Deleted item is %d",start->info); }
start = NULL;
free(temp); }
}
Display Function in DLL
int display()
{
struct node *temp;
if(start==NULL)
{
printf("\n The list is empty");
return 0;
}
else
{
temp=start;
printf("\n The list is:");
while(temp!=NULL)
{
printf("%d ",temp->info);
temp=temp->next;
}
}

}
Doubly Circular Linked List
• A circular doubly linked list is one which has the successor and
predecessor pointer in circular manner.
• That is, a circular doubly linked list is a doubly linked list where the
next field of the last node has pointer to the first node and previous
field of first node points to the last node of the list.
• The main objective of doubly circular linked list is to simplify the
insertion and deletion operations performed on doubly linked list.
last
start
10 5 7
Structure of a node
struct node
{
int info;
struct node *prev;
struct node *next;
};
struct node *start = NULL;
struct node *last = NULL;
Adding a node at the Beginning of DCLL
void insert_beg() nnode ->prev = nnode;
{ start = nnode;
last = nnode;
int item;
}
struct node *nnode;
else
{
printf("\Enter the item\t "); nnode->next= start;
scanf("%d",&item); nnode->prev= last;
nnode = (struct node *) malloc(sizeof(struct node)); start->prev = nnode;
nnode->info = item; start = nnode;
if(start == NULL) last->next = nnode;
}
{
}
nnode->next = nnode;
Adding a node at the End of DCLL
void insert_end() nnode->prev = nnode;
{ nnode->next= nnode;
int item; start = last = nnode;
struct node *nnode,*temp; }
printf("\Enter the item\t "); else
scanf("%d",&item); {
nnode = (struct node *) malloc(sizeof(struct node)); last->next= nnode;
nnode->info = item; nnode->prev= last;
if(start ==NULL) last= nnode;
{ nnode->next = start;
start = nnode; start->prev= nnode;
last = nnode; }
}
Deleting a node at the Beginning of DCLL
int del_beg()
{ else
int item; {
struct node *temp;
temp= start;
if(start ==NULL)
{ printf("\n The deleted item is %d",start->info);
printf("\n Empty List !!!"); start = start->next;
return 0; start->prev= last;
}
last->next= start;
else if (start==last)
{ free(temp);
temp=start; }
start = NULL;
last = NULL;
}
free(temp);
}
Deleting a node at the End of DCLL
int del_end() else
{
int item; {
struct node *temp, *hold; temp= last;
if(start ==NULL)
{ start->prev = last->prev;
printf("\n Empty List !!!"); last= last->prev;
return 0;
} last->next = start;
else if( start==last) printf("\n The deleted item is %d",temp->info);
{
temp = start; free(temp);
printf("\n Deleted item is %d",start->info); }
start = NULL;
last = NULL;
free(temp); }
}
Homework #6
1. State relative merits and demerits of contiguous list and linked list.
2. Write an algorithm to add a node at the beginning of singly linked list.
3. How can you insert a node at the end of doubly linked list? Explain.
4. Differentiate between singly linked list, doubly linked list, CLL and DCLL.
5. Write a C-function to delete a node at the end of singly linked list.
6. Write a C function to add a node at the beginning of DLL.
7. Write a C function to delete a node at the end in DCLL.
8. Write an algorithm to add a node at the beginning in the CLL.
Thank You !

You might also like