2. VARIOUS TYPES OF LINKED LIST
able of Contents
Circular Linked List
Linked Implementation of Stack
Linked Implementation of Queue
Application of Linked List
3. Concept of Linked List
Linked list is a linear data structure.
A Linked List is a set of nodes where each node has two fields ‘data’
and ‘link’.
DATA LINK
Piece of Information
Data Type : Int,char,float etc.
Used to point to next node
4. C Representation of Simple Linked List
C structure for creating a node in a Linked List :
typedef struct node
{
int data;
struct node *next;
}NODE;
Stores Information part of data
Stores address of next node
5. Circular Linked List
Circular linked list is a sequence of elements in which every element
has link to its next element in the sequence and the last element has
the link to the first element in the sequence.
The circular linked list is as shown below :
10 20 30 40 50
Head Node
6. Operation performed on Circular Linked List
The following operation can be performed on Circular linked list.
1. Insertion
2. Deletion
3. Display
7. Insertion of Circular Linked list
Algorithm:
Step 1: Create a new_node with a given value.
Step 2: Check whether list is empty (head==NULL).
Step 3: If empty, then head=new_node and new_node→next=head.
Step 4: If not empty then create a temp node pointer and initialize with
‘head’.
Step 5: Move temp node to its next node, until head node not achieved
(temp→next==head).
Step 6: Set new_node→next=head, head=new_node and
temp→next=head.
Inserting a node as a head node
8. Algorithm:
Step 1: Create a new_node with a given value.
Step 2: Check whether list is empty (head==NULL).
Step 3: If empty, then head=new_node and new_node→next=head.
Step 4: If not empty then create a temp node pointer and initialize with
‘head’.
Step 5: Move temp node to its next node, until head node not achieved
(temp->next==head).
Step 6: Set temp→next=new_node and new_node→next=head.
Inserting a node as a last node
9. Algorithm:
Step 1: Create a new_node with a given value.
Step 2: Check whether list is empty (head==NULL).
Step 3: If empty, then head=new_node and new_node->next=head.
Step 4: If not empty then create a temp node pointer and initialize with
‘head’.
Step 5: Define key value after which the node is to be inserted.
Step 6: Check for key value (temp→data==key).
Step 7: Every time check whether temp is reached to the last node or
not. If yes then print Node not found.
Step 8: If temp=key then check whether it is last node
(temp→next==head).
Step 9: If temp is last node then temp->next=new_node and
new_node→next=head.
Step 10: If temp is not last node then new_node→next=temp→next and
temp→next=new_node.
Inserting a node at specific location
10. Deletion of any node
Algorithm:
Step 1: Check whether list is Empty (head == NULL).
Step 2: If it is Empty then, print list is empty.
Step 3: If it is Not Empty then, define two Node pointers 'temp1' and
'temp2' and initialize 'temp1' with head.
Step 4: Keep moving the temp1 until it reaches to the specified or to the
last node. And every time set 'temp2 = temp1' before moving the
'temp1' to its next node.
Step 5: If it is reached to the last node then display Node not found.
Step 6: If it is reached to the exact node which we want to delete, then
check whether list is having only one node (temp1→next == head).
11. Step 7: If list has only one node and that is the node to be deleted then
set head=NULL and delete temp1 (free(temp1)).
Step 8: If list contains multiple nodes then check whether temp1 is the
first node in the list (temp1 == head).
Step 9: If temp1 is the first node then set temp2 = head and keep
moving temp2 to its next node until temp2 reaches to the last node.
Then set head=head→next, temp2→next=head and delete temp1.
Step 10: If temp1 is not first node then check whether it is last node in
the list (temp1→next == head).
Step 11: If temp1 is last node then set temp2→next=head and delete
temp1 (free(temp1)).
Step 12: If temp1 is not first node and not last node then set
temp2→next = temp1→next and delete temp1 (free(temp1)).
12. Display Circular Linked List
Algorithm:
Step 1: Check whether list is Empty (head == NULL).
Step 2: If it is Empty, then print list is empty.
Step 3: If it is Not Empty then, define a Node pointer 'temp' and
initialize with head.
Step 4: Keep displaying temp→data with an arrow (--->) until temp
reaches to the last node.
Step 5: Finally display temp→data with arrow pointing to head→data.
13. Advantages of Circular Linked list over Singly Linked
list
In Circular linked list, the next pointer of last node in pointing to the
head node.
Hence we can move from last node to the head node of the list very
efficiently.
So, accessing of any node is much faster then singly linked list.
The last node’s next pointer have null value in singly linked list while
in circular linked list it is having the address of the head node.
Hence there is no loss of memory.
14. Linked Implementation of Stack
Stack is a special case of list and therefore we can represent stack
using arrays as well as using linked list.
The advantage of implementing stack using linked list is that we
need not have to worry about the size of the stack.
Since we are using linked list as stack, we are inserting the nodes in
the stack and the size is dynamically changed. So there is not stack
full (Overflow condition).
15. C structure of Linked
Stack
Representation of Linked
Stack
struct stack
{
int data;
struct stack *next;
}NODE;
40
30
20
10 NULL
Top Node
16. Insert a node into Linked Stack
We can insert the node using user defined function named
push(value).
Algorithm:
Step 1: Create a new_node with given value.
Step 2: Check whether stack is Empty (top == NULL).
Step 3: If it is Empty, then set new_node→next=NULL.
Step 4: If it is Not Empty, then set new_node→next=top.
Step 5: set top = new_node.
17. Delete a node from Linked Stack
We can delete a node from linked stack using user defined function
called pop().
Algorithm:
Step 1: Check whether stack is Empty (top == NULL).
Step 2: If it is Empty, then display “Stack is empty”.
Step 3: If it is Not Empty, then define a Node pointer 'temp' and set it
to 'top'.
Step 4: Then set 'top = top → next'.
Step 5: Delete 'temp' (free(temp)).
18. Display Linked Stack
Algorithm:
Step 1: Check whether stack is Empty (top == NULL).
Step 2: If it is Empty, then display “Stack is Empty”.
Step 3: If it is Not Empty, then define a Node pointer 'temp' and
initialize with top.
Step 4: Display 'temp → data --->' and move it to the next node.
Repeat the same until temp reaches to the first node in the stack
(temp → next != NULL).
Step 5: Display 'temp → data ---> NULL'.
19. Linked Implementation of Queue
We can implement queue using linked list.
The main advantage is we need not have to worry about size of the
queue, so there will not be a queue full condition.
The left most pointer is called front node and the right most pointer is
called rear node.
We can not remove any arbitrary node from queue.
20. C structure of Linked
Queue
Representation of Linked
Queue
struct node
{
int data;
struct stack *next;
}Q;
40302010
Front
node
Rear
node
21. Insert a node into Linked Queue
Algorithm:
Step 1: Create a new_node and set new_node → next = NULL.
Step 2: Check whether queue is Empty (rear == NULL)
Step 3: If it is Empty then, set front = new_node and rear =
new_node.
Step 4: If it is Not Empty then, set rear→next = new_node and rear =
new_node.
22. Delete a node from Linked Queue
Algorithm:
Step 1: Check whether queue is Empty (front == NULL).
Step 2: If it is Empty, then display "Queue is Empty!!!”
Step 3: If it is Not Empty then, define a Node pointer temp and set it
to front.
Step 4: Then 'front = front → next' and delete 'temp' (free(temp)).
23. Display Linked Queue
Algorithm:
Step 1: Check whether queue is Empty (front == NULL).
Step 2: If it is Empty then, display “Queue is Empty!!!”
Step 3: If it is Not Empty then, define a Node pointer temp and
initialize with front.
Step 4: Display temp → data ---> and move it to the next node.
Repeat the same until temp reaches to 'rear' (temp → next != NULL).
Step 5: Display temp → data ---> NULL.
24. Application of Linked List
Various application of linked list are :
1. Representation of polynomial and performing various operations
such as addition, multiplication and evaluation on it.
2. Performing addition of long positive integers.
3. Representing non integer and non homogeneous list.