Module 3-LL
Module 3-LL
Linked Lists
Linked Lists
• A linked list is a very flexible dynamic data structure in which elements can be added
to or deleted from anywhere.
• A linked list can be perceived as a train or a sequence of nodes in which each node
contains one or more data fields and a pointer to the next node.
• In a linked list, each element (called a node) is allocated space as it is added to the list
• Linked list is a data structure which in turn can be used to implement other data
structures. Thus, it acts as building block to implement data structures like stacks,
queues and their variations.
• Every node in the list points to the next node in the list. Therefore, in a linked list ever
node contains two types of information.
✓ The data stored in the node
✓ A pointer or link to the next node in the list
1 2 3 4 5 6 7 X
Traversing Linked Lists
• We can traverse the entire linked list using a single pointer
variable called START.
• The START node contains the address of the first node; the next
part of the first node in turn stores the address of its succeeding
node.
• Using this technique the individual nodes of the list will form a
chain of nodes.
• If START = NULL, this means that the linked list is empty and
contains no nodes.
• In C, we can implement a linked list using the following code:
struct node
{
int data;
struct node *next;
};
START Pointer
START
NEXT
1 DATA
1
H 4
2
START pointing to
the first element of 3
the linked list in 4 E 7
memory 5
AVAIL 6
9 7 L 8
L 10
8
9
10 O -1
Singly Linked Lists
• A singly linked list is the simplest type of linked list in which
every node contains some data and a pointer to the next node
of the same data type.
START
1 2 3 4 5 6 7 X
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
Inserting a Node at the Beginning
1 7 3 4 2 6 5 X
START
9 1 7 3 4 2 6 5 X
START
AVAIL is a pointer that stores the address of the first free node in the memory.
Inserting a Node at the End
1 7 3 4 2 6 5 X
START, PTR
1 7 3 4 2 6 5 9 X
START
AVAIL is a pointer that stores the address of the first free node in the memory.
Inserting a Node after Node that has
Value NUM
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Steps 8 and 9 while PREPTR->DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR->NEXT
[END OF LOOP]
Step 10: PREPTR->NEXT = New_Node
Step 11: SET New_Node->NEXT = PTR
Step 12: EXIT
1 7 3 4 2 6 5 X
Inserting a Node before a Node that
has Value NUM
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Steps 8 and 9 while PTR->DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR->NEXT
[END OF LOOP]
Step 10: PREPTR->NEXT = New_Node
Step 11: SET New_Node->NEXT = PTR
Step 12: EXIT
1 7 3 4 2 6 5 X
START
Deleting the First Node
Algorithm to delete the first node from the linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 5
[END OF IF]
1 7 3 4 2 6 5 X
START
7 3 4 2 6 5 X
START
Deleting the Last Node
ALGORITHM TO DELETE THE LAST NODE OF THE LINKED LIST
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Steps 4 and 5 while PTR->NEXT != NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR->NEXT
[END OF LOOP]
Step 6: SET PREPTR->NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT
1 7 3 4 2 6 5 X
1 7 3 4 2 6 X 5 X
PREPTR PTR
START
Deleting the Node After a Given Node
with data NUM
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 10
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Step 5 and 6 while PREPTR->DATA != NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR->NEXT
[END OF LOOP]
Step7: SET TEMP = PTR
Step 8: SET PREPTR->NEXT = TEMP->NEXT
Step 9: FREE TEMP
Step 10: EXIT
1 7 3 4 6 5 X
Deleting the Node 2 After a Given Node 4
1 7 3 4 2 6 5 X
1 7 3 4 2 6 5 X
START
PREPTR PTR
1 7 3 4 2 6 5 X
START
1 7 3 4 6 5 X
TRAVERSING A LINKED LIST
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Steps 3 and 4 while PTR != NULL
Step 3: Apply Process to PTR->DATA
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT
void traverse()
{
struct node *ptr;
ptr= start;
if (ptr == NULL)
{
printf("Linked list is empty.\n");
return;
}
printf("There are %d elements in linked list.\n", count);
while (ptr->next != NULL)
{
printf("%d\n", ptr->data);
ptr = ptr->next;
}
printf("%d\n", ptr->data);
}
ALGORITHM TO INSERT A NEW NODE IN THE BEGINNING OF THE LINKED LIST
void insert_at_begin(int x)
Step 1: IF AVAIL = NULL, then {
Write OVERFLOW struct node *t;
Go to Step 7
[END OF IF]
t = (struct node*)malloc(sizeof(struct node));
Step 2: SET New_Node = node.
Step 3: SET AVAIL = AVAIL->NEXT t->data=x;
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START if (start == NULL)
{
Step 6: SET START =New_Node
start = t;
Step 7: EXIT
start->data = t->data;
start->next = NULL;
return;
}
t->next = start;
start = t;
}
ALGORITHM TO INSERT A NEW NODE AT THE END OF THE LINKED LIST
void insert_at_end(int x)
Step 1: IF AVAIL = NULL, then {
Write OVERFLOW struct node *t, *ptr;
Go to Step 10
[END OF IF]
t = (struct node*)malloc(sizeof(struct node));
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT t->data=x;
ptr = start;
ptr->next = t;
t->next = NULL;
}
ALGORITHM TO INSERT hasA Value NUM AFTER A NODE NUM
NEW NODE
nserting a Node after Node that has Value NUM
Step 1: IF AVAIL = NULL, then void insert_after(int x,int x1)
Write OVERFLOW {
Go to Step 12 struct node *t, *ptr,*preptr;
[END OF IF]
t = (struct node*)malloc(sizeof(struct node));
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT t->data=x;
Step 4: SET New_Node->DATA = VAL ptr=start;
Step 5: SET PTR = START preptr=ptr;
Step 6: SET PREPTR = PTR while(preptr->data!=x1)
Step 7: Repeat Steps 8 and 9 while
{ preptr=ptr;
PREPTR->DATA != NUM
Step 8: SET PREPTR = PTR ptr=ptr->next;
Step 9: SET PTR = PTR->NEXT }
[END OF LOOP] preptr->next=t;
Step 10: PREPTR->NEXT = New_Node t->next=ptr;
Step 11: SET New_Node->NEXT = PTR
}
Step 12: EXIT
ALGORITHM TO INSERT A NEWhasNODE
ValueBEFORE
NUM A NODE with VALUE NUM
nserting a Node after Node that has Value NUM
Step 1: IF AVAIL = NULL, then void insert_before(int x,int x1)
Write OVERFLOW {
Go to Step 12 struct node *t, *ptr,*preptr;
[END OF IF]
t = (struct node*)malloc(sizeof(struct node));
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT t->data=x;
ptr=start;
Step 4: SET New_Node->DATA = VAL preptr=ptr;
while(ptr->data!=x1)
Step 5: SET PTR = START
{ preptr=ptr;
Step 6: SET PREPTR = PTR
ptr=ptr->next;
Step 7: Repeat Steps 8 and 9 while PTR- }
>DATA != NUM ==NUM preptr->next=t;
Step 8: SET PREPTR = PTR t->next=ptr;
Step 9: SET PTR = PTR->NEXT
}
[END OF LOOP]
n = start->data;
ptr=start;
start = start->next;
free(ptr);
printf("%d deleted from beginning.\n", n);
}
has Value NUM
ALGORITHM TO DELETE LAST NODE
nserting a Node aftervoid
Nodedelete_from_end() {
that has Value NUM
struct node *ptr, *preptr;
Step 1: IF START = NULL, then int n;
Write UNDERFLOW
Go to Step 8 if (start == NULL) {
[END OF IF] printf("Linked list is already empty.\n");
Step 2: SET PTR = START
return;
Step 3: Repeat Steps 4 and 5 while PTR-> }
NEXT != NULL
if (start->next == NULL)
Step 4: SET PREPTR = PTR {
Step 5: SET PTR = PTR->NEXT
free(start);
[END OF LOOP]
start = NULL;
Step 6: SET PREPTR->NEXT = NULL return;
Step 7: FREE PTR }
Step 8: EXIT ptr= start;
while (ptr->next != NULL) {
preptr = ptr;
ptr = ptr->next; }
preptr->next = NULL;
free(ptr);
}
has Value NUM
ALGORITHM TO DELETE A NODE AFETR void delete_after(int x)
nserting a Node after Node that has Value NUM
A GIVEN NODE {
struct node *ptr, *preptr,*temp;
int n;
Step 1: IF START = NULL, then if (start == NULL)
Write UNDERFLOW
{
Go to Step 10
[END OF IF] printf("Linked list is already empty.\n");
Step 2: SET PTR = START return;
Step 3: SET PREPTR = PTR }
Step 4: Repeat Step 5 and 6 while PREPTR-> ptr=start;
DATA != NUM
preptr=ptr;
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR->NEXT while(preptr->data!=x)
[END OF LOOP] {
Step7: SET TEMP = PTR preptr=ptr;
Step 8: SET PREPTR->NEXT = TEMP->NEXT ptr=ptr->next;
Step 9: FREE TEMP
}
Step 10: EXIT
temp=ptr;
preptr->next=temp->next;
free(temp);
}
Circular Linked List
• In a circular linked list, the last node contains a pointer to the first
node of the list.
• A circular linked list has no beginning and no ending.
START
1 2 3 4 5 6 7
Circular Linked List
1 7 3 4 2 6 5
START, PTR
1 7 3 4 2 6 5
PTR
START
9 1 7 3 4 2 6 5
START
Circular Linked List
1 7 3 4 2 6 5
START, PTR
1 7 3 4 2 6 5 9
START PTR
Circular Linked List
Algorithm to insert a new node after a node that has value NUM
Algorithm to insert a new node before a node that has value NUM
1 7 3 4 2 6 5
START, PTR
1 7 3 4 2 6 5
PTR
START
7 3 4 2 6 5
START
Circular Linked List
Algorithm to delete the last node of the circular linked list
1 7 3 4 2 6 5
1 7 3 4 2 6 5
PREPTR
PTR
START
1 7 3 4 2 6
START
Circular Linked List
Algorithm to delete the node after a given node from the circular LL
1 7 3 4 2 6 5
1 7 3 4 2 6 5
1 7 3 4 6 5
START
Doubly Linked List
▪ A doubly linked list or a two way linked list is a more complex
type of linked list which contains a pointer to the next as well
as previous node in the sequence. Therefore, it consists of
three parts and not just two. The three parts are data, a
pointer to the next node and a pointer to the previous node
START
X 1 1 2 3 4 X
Doubly Linked List
• In C language, the structure of a doubly linked list is given as,
struct node
{ struct node *prev;
int data;
struct node *next;
};
• The prev field of the first node and the next field of the last
node will contain NULL. The prev field is used to store the
address of the preceding node. This would enable to traverse
the list in the backward direction as well.
Doubly Linked List
Algorithm to insert a new node in the beginning of the doubly LL
X 1 7 3 4 2 X
START
2 X
X 9 1 7 3 4
START
Doubly Linked List
Algorithm to insert a new node at the end of the doubly linked list
X 1 7 3 4 2 X
START, PTR
X 1 7 3 4 2 9 X
PTR
Doubly Linked List
Algorithm to insert a new node after a node that has value NUM
Algorithm to insert a new node before a node that has value NUM
X 1 7 3 4 2 X
START, PTR
7 3 4 2 X
Doubly Linked List
Algorithm to delete the last node of the doubly linked list
X 1 3 5 7 8 9 X
1
START, PTR
X 1 3 5 7 8 9 X
1
START PTR
X 1 3 5 7 8 X
START
Doubly Linked List
Algorithm to delete the node after a given node from the doubly
linked list
Algorithm to delete the node before a given node from the doubly
linked list
START
1 1 2 3 4
Circular Doubly Linked List
1 7 3 4 2
START
1 7 3 4 2 9
START
Circular Doubly Linked List
1 3 5 7 8 9
1
START, PTR
3 5 7 8 9
1
START
Circular Doubly Linked List
Algorithm to delete the last node of the circular doubly
linked list
1 3 5 7 8 9
1
START PTR
X 1 3 5 7 8
START
Circular Doubly Linked List
1 7 3 4 2 6 5 X
TOP
Push Operation on a Linked Stack
Algorithm to PUSH an element in a linked stack
Step 1: Allocate memory for the new node and name it as New_Node
Step 2: SET New_Node->DATA = VAL
Step 3: IF TOP = NULL, then
SET New_Node->NEXT = NULL
SET TOP = New_Node
ELSE
SET New_node->NEXT = TOP
SET TOP = New_Node
[END OF IF]
Step 4: END
1 7 3 4 2 6 5 X
TOP
9 1 7 3 4 2 6 5 X
TOP
Pop Operation on a Linked Stack
Algorithm to POP an element from a stack
9 1 7 3 4 2 6 5 X
TOP
1 7 3 4 2 6 5 X
TOP
Linked Representation of Queues
• In a linked queue, every element has two parts: one that stores data
and the other that stores the address of the next element.
• The START pointer of the linked list is used as FRONT.
• We will also use another pointer called REAR which will store the
address of the last element in the queue.
• All insertions will be done at the rear end and all the deletions will
be done at the front end.
• If FRONT = REAR = NULL, then it indicates that the queue is empty.
1 7 3 4 2 6 5 X
FRONT REAR
Inserting an Element in a Linked Queue
Step 1: Allocate memory for the new node and name it as NewPTR
Step 2: SET NewPTR->DATA = VAL
Step 3: IF FRONT = NULL, then
SET FRONT = REAR = NewPTR
SET FRONT->NEXT = REAR->NEXT = NULL
ELSE
SET REAR->NEXT = NewPTR
SET REAR = NewPTR
SET REAR->NEXT = NULL
[END OF IF]
Step 4: END
Deleting an Element from a Linked
Queue
Algorithm to delete an element from a linked queue