Data Structure_singly Linked List
Data Structure_singly Linked List
TOPICS TO BE
COVERED
• Introduction to Linked List : Node
• Declaration of node in ‘C’ – Self Referential Structure
• Advantages and Disadvantages of Linked list
• Basic Operations on Linked List
• Types of Linked List
• Singly Linked List :
• Memory allocation for a node
• Overflow & Underflow Condition
• Operations on Singly Linked List:
• Creation
• Traversal
• Insertion
• Deletion
LINKED LIST
Linked List is a linear collection of data elements called nodes.
Node: A Node is a memory location that contains two parts.
• Information of element(or) Data
• Link Field contains the address of next Node.
Diagrammatically structure of a node look as follow:
-START is a special node which identifies address of 1st Node i.e. the beginning
of the linked list.
-The address or Link part of last node contains NULL as no further nodes exist.
DECLARATION OF
How to NODE IN ‘C’
declare structure of a node in ‘C’ Language:
struct node
{
int info;
struct node * link; // self
referencing structure
};
struct node *p;
Underflow Condition:
Step 1: Start
Step 2: Let FRESH, ITEM
Step 3: Display “Enter the info”
Step 4: Read ITEM
Step 5: If FRESH=NULL, Then
Step 5.1: Display “Overflow”
Step 5.2: Exit
Step 6: Else
Step 6.1: Set INFO[FRESH] := ITEM
Step 6.2: Set LINK[FRESH] := NULL
[End of If – Step 5]
Step 7: Stop
CREATION OF LINKED LIST WITH
ONE NODE
STEPS FOR CREATION OF LINKED LIST WITH ONE NO
CREATION OF LINKED LIST WITH ONE NODE
Assume FRESH is the pointer which will point to the memory location
taken from free memory of computer using Dynamic Memory Allocation
functions ( i.e. malloc( ) or calloc( ) ). START is initially set to NULL as
there are no nodes in the linked list.
CREATION( )
Step 1: Start
Step 2: Let START, FRESH, ITEM
Step 3: Display “Enter the info”
Step 4: Read ITEM
Step 5: If (FRESH = = NULL), Then
Step 5.1: Display “Overflow”
Step 5.2: Exit
Step 6: Else
Step 6.1: Set INFO[FRESH] := ITEM
Step 6.2: Set LINK[FRESH] := NULL
[End of If – Step 5]
Step 7: Set START := FRESH
Step 8: Stop
TRAVERSING A SINGLY LINKED LIST
TRAVERSING A SINGLY LINKED LIST
Assume a Singly Linked List with some nodes. This algorithm will display
info part of each node by traversing from first node to last node.
TRAVERSAL( )
Step 1: Start
Step 2: Let START, PTR
Step 3: Set PTR := START
Step 4: Repeat While ( PTR != NULL)
Step 4.1: Display INFO[PTR]
Step 4.2: Set PTR := LINK[PTR]
[End of While – Step 4]
Step 5: Stop
PTR
INSERTION AT THE BEGINNING OF
LINKED LIST
INSERTION OF NODE AT THE
BEGINNING
INSERTION AT THE BEGINNING OF LINKED LIS
INSERTION_AT_BEGINNING( )
Step 1: Start
Step 2: Let START, FRESH, ITEM
Step 3: Display “Enter the info”
Step 4: Read ITEM
Step 5: If (FRESH = = NULL), Then
Step 5.1: Display “Overflow”
Step 5.2: Exit
Step 6: Else
Step 6.1: Set INFO[FRESH] := ITEM
Step 6.2: Set LINK[FRESH] := NULL
[End of If – Step 5]
Step 7: If START = = NULL, Then [ Linked List is Empty ]
Step 7.1: Set START := FRESH
Step 8: Else
Step 8.1: Set LINK[FRESH] := START
Step 8.2: Set START := FRESH
[End of If – Step 7]
Step 9: Stop
INSERTION AT THE END OF LINKED
LIST
INSERTION OF NODE AT THE END
INSERTION AT THE END OF
INSERTION_AT_END( ) LINKED LIST
Step 1: Start
Step 2: Let START, FRESH, ITEM, PTR
Step 3: Display “Enter the info”
Step 4: Read ITEM
Step 5: If (FRESH = = NULL), Then
Step 5.1: Display “Overflow”
Step 5.2: Exit
Step 6: Else
Step 6.1: Set INFO[FRESH] := ITEM
Step 6.2: Set LINK[FRESH] := NULL
[End of If – Step 5]
Step 7: If START = = NULL, Then
Step 7.1: Set START := FRESH
Step 8: Else
Step 8.1: Set PTR := START
Step 8.2: Repeat While ( LINK[PTR] != NULL)
Step 8.2.1: Set PTR := LINK[PTR]
[ End of While – Step 8.2]
Step 8.2: Set LINK[PTR] := FRESH
[End of If – Step 7]
Step 9: Stop
INSERTION AT SPECIFIC LOCATION OF
LINKED LIST
INSERTION AT SPECIFIC LOCATION
INSERTION AT SPECIFIC LOCATION OF
LINKED
Assume a linked list with nodes, LIST
FRESH points to the new node to be
inserted at specific location ( LOC ).
INSERTION_AT_LOC( )
Step 1: Start
Step 2: Let START, FRESH, PTR, PREV, LOC, I
Step 3: Display “Enter the location”
Step 4: Read LOC
Step 5: Set PTR:= START, I : = 1
Step 6: Repeat While ( I < LOC ) AND (PTR != NULL)
Step 6.1: Set PREV:= PTR
Step 6.2: Set PTR:= LINK[PTR]
Step 6.3: Set I := I + 1
[End of While – Step 6 ]
Step 7: If PTR = = NULL, Then
Step 7.1: Display “Location Not Found”
Step 8: Else If (PTR==START), Then
Step 8.1: Set LINK[FRESH] := START
Step 8.2: Set START := FRESH
Step 9: Else
Step 9.1: Set LINK[PREV] := FRESH
Step 9.2: Set LINK[FRESH] := PTR
[ End of If – Step 7]
Step 10: Stop
DELETION OPERATION ON LINKED
While performing deletion operationLIST
in a single linked list, the memory
space of the node which will be deleted, should be de-allocated so that the
operating system collects free space.
In case of ‘C’ programming, we will use free( ) function to release the
memory associated with deleted node.
Assuming PTR points to the node to be deleted, we can write
FREE(PTR) to release the memory.
The UNDERFLOW condition need to be tested each time we perform
deletion operation.
Underflow Condition : START == NULL
DELETION_FIRST (START)
Step 1: Start
Step 2: Let PTR
Step 3: If (START = = NULL) , Then
Step 3.1: Display “Underflow”
Step 3.2: Exit
Step 4: Else
Step 4.1: Set PTR := START
Step 4.2: Set START := LINK [PTR]
Step 4.3: FREE(PTR)
[End of If – Step 3]
Step 5: EXIT
DELETION FROM THE END OF LINKED
LIST
DELETION FROM THE END
ALGORITHM : DELETION FROM THE
Algorithm:
END
DELETION_LAST (START)
Step 1: Start
Step 2: Let PTR, PREV
Step 3: If (START = = NULL) , Then
Step 3.1: Display “Underflow”
Step 3.2: Exit
Step 4: Else
Step 4.1: Set PTR := START
Step 4.2: Repeat While ( LINK[PTR] != NULL)
Step 4.2.1: Set PREV := PTR
Step 4.2.2: Set PTR := LINK[PTR]
[End of While – Step 4.2 ]
Step 4.3: Set LINK[PREV] := NULL
Step 4.4: FREE(PTR)
[End of If – Step 3]
Step 5: EXIT
DELETION FROM SPECIFIC LOCATION
OF LINKED LIST
DELETION FROM SPECIFIC
LOCATION
DELETION FROM SPECIFIC LOCATION
Assume a linked list with nodes. LOC refers to the location from where a
node is to be deleted.
DELETION_AT_LOC( )
Step 1: Start
Step 2: Let START, PTR, PREV, LOC, I
Step 3: Display “Enter the location”
Step 4: Read LOC
Step 5: Set PTR:= START, I : = 1
Step 6: Repeat While ( I < LOC ) AND (PTR != NULL)
Step 6.1: Set PREV:= PTR
Step 6.2: Set PTR:= LINK[PTR]
Step 6.3: Set I := I + 1
[End of While – Step 6 ]
Step 7: If (PTR = = NULL), Then
Step 7.1: Display “Location Not Found”
Step 8: Else If (PTR==START), Then
Step 8.1: Set START := LINK[PTR]
Step 9: Else
Step 9.1: Set LINK[PREV] := LINK[PTR]
[ End of If – Step 7]
Step 10: FREE ( PTR)
Step 11: Stop
SEARCHING A NODE IN SINGLY LINKED
LIST
SEARCHING A NODE
Assume a linked list with nodes. Assume ITEM is the INFO part of the node to
be searched.
SEARCH_NODE( )
Step 1: Start
Step 2: Let START, PTR, LOC, ITEM, FLAG
Step 3: Display “Enter the info of node to be searched”
Step 4: Read ITEM
Step 5: Set PTR:= START, LOC : = 0, FLAG := 0
Step 6: Repeat While (PTR != NULL)
Step 6.1: Set LOC := LOC + 1
Step 6.2: If (ITEM == INFO[PTR] ), Then
Step 6.2.1: Set FLAG := 1
Step 6.2.2: Break
[End of If – Step 6.2 ]
[ End of While – Step 6 ]
Step 7: If (FLAG = = 0), Then
Step 7.1: Display “Node Not Found”
Step 8: Else
Step 8.1: Display “Node Found At ”, LOC
[ End of If – Step 7]
Step 8: Stop
SORTING ELEMENTS OF NODES IN
SINGLY LINKED LIST
SORTING NODES IN SINGLY LINKED
LIST
Assume a linked list with nodes having integer info. This algorithm will
arrange the nodes in ascending order.
SORT_NODE( )
Step 1: Start
Step 2: Let START, PTR1, PTR2, TEMP
Step 3: Set PTR1 := START
Step 4: Repeat While (LINK[PTR1] != NULL)
Step 4.1: Set PTR2 := LINK[PTR1]
Step 4.2: Repeat While ( PTR2 != NULL)
Step 4.2.1: If (INFO[PTR1] > INFOP[PTR2]), Then
Step 4.2.1.1: Set TEMP := INFO[PTR1]
Step 4.2.1.2: Set INFO[PTR1] := INFO[PTR2]
Step 4.2.1.3: Set INFO[PTR2] := TEMP
[ End of If – Step 4.2.1]
Step 4.2.2: Set PTR2 := LINK[PTR2]
[End of While – Step 4.2]
Step 4.3: Set PTR1 := LINK[PTR1]
[ End of While – Step 4 ]
Step 5: Stop
CONCATENATION OF TWO SINGLY
LINKED LISTS
CONCATENATION OF TWO SINGLY
LINKED LISTS
CONCATENATION OF TWO SINGLY
LINKED LISTS
Assume START1 holds the address of starting node of 1st single linked list and
START2 holds the address of starting node of 2nd single linked list. This
algorithm will join the 2nd singly linked list at the end of 1st singly linked list.
CONCATENATE( )
Step 1: Start
Step 2: Let PTR, START1,START2
Step 3: Set PTR := START1
Step 4: Repeat While (LINK[PTR1] != NULL)
Step 4.1: Set PTR := LINK[PTR1]
[End of While – Step 4]
Step 5: Set LINK[PTR1] := START 2
Step 6: Set START2 := NULL
Step 7: Stop
MENU BASED “C” PROGRAM ON
OPERATIONS IN A SINGLY LINKED LIST
// C program for operations in the Singly Linked List
#include <stdio.h>
#include <stdlib.h>
// Declaration of Node
struct node
{
int info;
struct node* link;
};
struct node* start = NULL;
// Function to traverse the linked list
void traverse()
{
struct node* ptr;
if (start == NULL)
printf("\nList is empty or Underflow\n");
else
{
ptr = start;
while (ptr != NULL)
{
printf("%d ",ptr->info);
ptr = ptr->link;
}
}
}
// Function to insert at the beginning of the linked list
void insert_beginning()
{
int item;
struct node* fresh;
fresh = (struct node *) malloc(sizeof(struct node));
if(fresh==NULL)
{
printf(“Overflow”);
exit(1);
}
printf("\nEnter the info : ");
scanf("%d", &item);
fresh->info = item;
fresh->link=NULL;
if(start == NULL)
{
start = fresh;
}
else
{
fresh->link = start;
start = fresh;
}
}
// Function to insert at the end of the linked list
void insert_end()
{
int item;
struct node *fresh, *ptr;
fresh = (struct node *) malloc(sizeof(struct node));
if(fresh==NULL)
{
printf(“Overflow”); exit(1);
}
printf("\nEnter the info : ");
scanf("%d", &item);
fresh->info = item;
fresh->link=NULL;
if(start == NULL)
start = fresh;
else
{
ptr = start;
while (ptr->link != NULL)
{
ptr = ptr->link;
}
ptr->link = fresh;
}
}
// Function to insert at any specified position in the linked list
void insert_loc()
{
struct node *ptr,*prev,*fresh;
int loc, item, i = 1;
ptr = start;
while ((i < loc) && (ptr!= NULL))
{
prev = ptr;
ptr = ptr->link;
i++;
}
prev->link = fresh;
fresh->link=ptr;
}
// Function to delete the first node in Singly linked list
void deletion_beginning()
{
struct node* ptr;
if (start == NULL)
{
printf("\nList is empty or Underflow\n");
}
else
{
ptr = start;
start = ptr->link;
free(ptr);
}
}
// Function to delete last node of the linked list
void deletion_last()
{
struct node *ptr, *prev;
if (start == NULL)
{
printf("\nList is Empty or Underflow\n");
}
else
{
ptr = start;
while (ptr->link != NULL)
{
prev = ptr;
ptr = ptr->link;
}
prev->link = NULL;
free(ptr);
}
}
// Function to delete from any specified position from the linked list
void deletion_loc()
{
struct node *ptr,*prev;
int loc, item, i = 1;
ptr = start;
while ((i < loc) && (ptr!= NULL))
{
prev = ptr;
ptr = ptr->link;
i++;
}
prev->link = ptr->link;
}
int main()
{
int choice;
while (1)
{
3. Linked Dictionary
Step 1: Start
Step 2: Let PTR
Step 3: If (TOP = = NULL) , Then
Step 3.1: Display “Underflow”
Step 3.2: Exit
Step 4: Else
Step 4.1: Set PTR := TOP
Step 4.2: Set TOP := LINK [PTR]
Step 4.3: FREE(PTR)
[End of If – Step 3]
Step 5: EXIT
LINKED QUEUE
LINKED QUEUE
It is possible that concept of queue can be implemented by using singly linked
list, referred as linked queue.
1.In linked queue, we assume two pointers FRONT and REAR which will hold
the address of 1st (front node) and last node ( rear node) .
2.INSERTION operation in linked queue refers to insertion of a node at the
REAR end.
3.DELETION operation in linked queue is similar to deletion of a node from
the beginning (i.e. from the FRONT end of Linked queue).
4.TRAVERSE operation in linked queue is traversing from FRONT node to
REAR node.
5.The overflow condition is same as that of linked list. i.e. when FRESH ==
NULL.
6.The underflow condition is : when FRONT == NULL Or REAR == NULL
(i.e. when linked queue is empty).
7.When FRONT == REAR , it indicates there exist only one node in the linked
queue.
INSERTION OPERATION IN
LINKED QUEUE
INSERTION OPERATION IN LINKED QUEUE :
INSERTION_QUEUE( ) ALGORITHM
Step 1: Start
Step 2: Let FRONT,REAR, FRESH, ITEM
Step 3: Display “Enter the info”
Step 4: Read ITEM
Step 5: If (FRESH = = NULL), Then
Step 5.1: Display “Overflow”
Step 5.2: Exit
Step 6: Else
Step 6.1: Set INFO[FRESH] := ITEM
Step 6.2: Set LINK[FRESH] := NULL
[End of If – Step 5]
Step 7: If (FRONT = = NULL) or ( REAR = = NULL), Then [ When queue is
empty ]
Step 7.1: Set FRONT := FRESH , REAR := FRESH
Step 8: Else
Step 8.1: Set LINK[REAR] := FRESH
Step 8.2: Set REAR := FRESH
[End of If – Step 7]
Step 9: Stop
DELETION OPERATION IN LINKED
QUEUE
DELETION OPERATION IN LINKED QUEUE :
ALGORITHM
DELETION_QUEUE( )
Step 1: Start
Step 2: Let PTR,FRONT,REAR
Step 3: If (FRONT = = NULL) or ( REAR = = NULL), Then
Step 3.1: Display “Underflow”
Step 3.2: Exit
[ End of If – Step 3 ]
Step 4: Set PTR := FRONT
Step 5: If (FRONT = = REAR), Then
Step 5.1: Set FRONT:= NULL, REAR :=NULL
Step 6: Else
Step 6.1: Set FRONT := LINK[PTR]
[End of If – Step 5]
Step 7: FREE(PTR)
Step 8: EXIT
Thank You