0% found this document useful (0 votes)
3 views

Data Structure_singly Linked List

The document provides a comprehensive overview of linked lists, including their structure, advantages, disadvantages, and basic operations such as creation, traversal, insertion, and deletion. It details the declaration of nodes in C, memory allocation methods, and various types of linked lists like singly, doubly, and circular linked lists. Additionally, it covers specific algorithms for performing operations on linked lists, including handling overflow and underflow conditions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Data Structure_singly Linked List

The document provides a comprehensive overview of linked lists, including their structure, advantages, disadvantages, and basic operations such as creation, traversal, insertion, and deletion. It details the declaration of nodes in C, memory allocation methods, and various types of linked lists like singly, doubly, and circular linked lists. Additionally, it covers specific algorithms for performing operations on linked lists, including handling overflow and underflow conditions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 69

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:

(Structure of a node in linked list)

-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;

p = (struct node *) malloc (sizeof (struct node));

NOTE: If a pointer to a structure is declared as a structure


element of the same structure, it is referred as “SELF
REFERENTIAL STRUCTURE”.
MEMORY
ALLOCATIONS
Static memory allocation:
Static memory allocation is the one in which the required memory is
allocated at compile time. e.g: arrays are the best example of static
memory allocation.
Drawbacks :
1.Size is fixed before execution.
2.Insertion and deletion is a time taking process as the elements need to
be shifted towards left or right
3.There may be a possibility that memory may get wasted or memory is
deficit.
Dynamic memory allocation:
Dynamic memory allocation is the one in which the required memory is
allocated at run time. e.g: linked lists are the best example of dynamic
memory allocation.
The drawbacks of static memory allocation can be overcome by dynamic
memory allocation.
ADVANTAGES AND DISADVANTAGES
Advantages of Linked List:
A Linked list has the following advantages over arrays:
1. The allocated size can be increased or decreased as per the
requirement at run time.
2. Memory can be utilized efficiently as the programmer can
choose exact amount of memory needed.
3. The operations like insertion and deletion are less time-
consuming as compared to arrays.

Disadvantages of Linked List:


A linked list has following disadvantages:
1. It takes more memory space because each node contains
the link or address part.
2. Moving to a specific node directly like array is not
possible.
BASIC OPERATIONS ON
LINKED LIST:
The basic operations that can be performed on a linked list are
as follows:
1. Creation
2. Traversal
3. Insertion
4. Deletion
5. Searching
6. Sorting
7. Concatenation
TYPES OF LINKED
LIST

Linked lists are of the following categories:

• Singly Linked List

• Doubly Linked List

• Circular Linked List

• Circular Doubly Linked List


SINGLY LINKED
A single linked list isLIST
a collection of nodes whereeach node
contains the address of next node. In other words, it is also
referred as one-way list as traversing is possible in only one
direction i.e. from START to NULL.

1) A Linear List having link part in each node.


2) Traverse can be from START to NULL and Backward
traversing Not possible (i.e. one way List)
It is a linear collection of data item, where each element in
the list contains a field called link, which contains the address of
next element in the list.)
MEMORY ALLOCATION FOR A NODE

1.Assume a set of free memory blocks exist in the memory of the


computer.
2.We will use these memory blocks, each time we want to create a
node.
3.Assume FRESH is pointer which will point to the new node to be
added to a Linked List.
4. Each time we create a node, we will verify whether the required
memory is available in free memory of computer or not.
5.If required memory is not available, then overflow situation
occurs. Otherwise, we will update the INFO and LINK part of the
new node (pointed by FRESH).
6.Finally, the node will be added to Linked list.
OVERFLOW AND
UNDERFLOW
Overflow Condition:

When FRESH= =NULL


The condition FRESH= =NULL indicates that no free
memory blocks (or nodes) exist in the memory. At this point of
time if we try to create a new node overflow situation occurs.

Underflow Condition:

When START= =NULL


The condition START= =NULL indicates that linked list
has no nodes or linked list does not exist. At this point of time if
we try to delete a node from linked list it leads to underflow
situation.
MEMORY ALLOCATION FOR A 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( ) )

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

The following deletion operations can be performed on a Singly Linked


List.
1.Deletion from the beginning ( Deletion of 1st node)
2.Deletion from the end ( Deletion of last node)
3.Deletion from specific location
DELETION FROM THE BEGINNING OF
LINKED LIST
DELETION FROM THE BEGINNING
ALGORITHM : DELETION FROM THE
BEGINNING
Algorithm:

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;

fresh = (struct node *) malloc(sizeof(struct node));


printf("\nEnter the info : ");
scanf("%d", &item);
fresh->info = item;
fresh->link = NULL;

printf("\nEnter the location :");


scanf("%d", &loc);

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;

printf("\nEnter the location :");


scanf("%d", &loc);

ptr = start;
while ((i < loc) && (ptr!= NULL))
{
prev = ptr;
ptr = ptr->link;
i++;
}
prev->link = ptr->link;
}
int main()
{
int choice;
while (1)
{

printf("\n\t1 Traverse the list\n");


printf("\t2 Insertion at beginning\n");
printf("\t3 Insertion at end\n");
printf("\t4 Insertion at specific location\n");
printf("\t5 Deletion of first node\n");
printf("\t6 Deletion of last node\n");
printf("\t7 Deletion from specific location\n");
printf("\t8 Exit\n");

printf("\nEnter Choice :\n");


scanf("%d", &choice);
switch (choice) case 5:
{ deletion_beginning();
case 1: break;
traverse(); case 6:
break; deletion_last();
case 2: break;
insert_beginning(); case 7:
break; deletion_loc();
case 3: break;
insert_end(); case 8:
break; exit(1);
case 4: default:
insert_loc(); printf("Incorrect Choice\n");
break; }
} // End of while true loop
return 0;
}
OUTPUT:
APPLICATIONS OF SINGLY LINKED LIST
APPLICATIONS OF LINKED LIST

A linked list can be used in the following situations:

1. Linked Stack with its operations

2. Linked Queue with its operations

3. Linked Dictionary

4. Representation and manipulation of Polynomial Expression


LINKED STACK
LINKED STACK
It is possible that concept of stack can be implemented by using
singly linked list, referred as linked stack.

1.In linked stack, we assume TOP is the pointer which identifies


the topmost node ( like START)
2.PUSH operation in linked stack is similar to insertion of a node
at the beginning ( i.e. at top of linked stack).
3.POP operation in linked stack is similar to deletion of a node
from the beginning (i.e. from the top of linked stack ).
4.TRAVERSE operation in linked stack is similar to traversing
from top node till we reach NULL.
5.The overflow condition is same as that of linked list. i.e. when
FRESH == NULL
6.The underflow condition is : when TOP == NULL (i.e. when
linked stack is empty).
PUSH OPERATION IN LINKED
STACK
PUSH OPERATION IN LINKED STACK :
PUSH_STACK( ) ALGORITHM
Step 1: Start
Step 2: Let TOP, 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 (TOP = = NULL), Then [ When Linked Stack is empty ]
Step 7.1: Set TOP := FRESH
Step 8: Else
Step 8.1: Set LINK[FRESH] := TOP
Step 8.2: Set TOP := FRESH
[End of If – Step 7]
Step 9: Stop
POP OPERATION IN LINKED
STACK
POP OPERATION IN LINKED STACK :
ALGORITHM
POP_STACK( TOP )

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

You might also like