DS chapter 3
DS chapter 3
Unit-3
Linked List
Definition: Linked list is a linear data structure used for storing the data in the form of list. The
multiple nodes are connected to each other through pointer. In the list the node contains two
parts.
Info -> this part contains the actual information. Link -> this part contains the next node
address.
Or
Linked List is a Linear collection of nodes containing the information part and the link part.
Or
Linked list is a linear collection of data structure where data are not store sequentially inside the
computer memory but they are link with each other by the help of address.
Advantage:
They are dynamic in nature which allocates the memory when required.
Insertion and deletion can be easily implemented.
Stacks and queues can be easily executed.
It reduces the access time.
Disadvantage:
The memory is wasted as pointer required extra memory for storage.
Each mode has to access sequentially.
Reverse traversing is difficult.
Chithra T R Page 1
DATA STRUCTURES
Application:
Linked List a used to implement stacks, queues, graph etc.
Linked List you insert element at the beginning and end of the list.
In Linked List we don’t need to know the size in advance.
Performing arithmetic operation as long integer.
Maintaining Directory of names.
Dynamic Memory Allocations.
Arrays are of fixed size. Linked list size is dynamic (expand or contract
its size)
Memory is allocated during compile time Memory is allocated during runtime.
Random access is allowed. Random access not possible.
Elements are at contiguous location. Elements have non-contiguous location.
Memory utilization is less efficient. Memory utilization is efficient.
Insertion of new element is expensive. Insertion/deletion is easier.
Chithra T R Page 2
DATA STRUCTURES
Types of linked lists:
1. Singly linked list (SLL)
2. Doubly linked list (DLL)
3. Circularly linked list (CLL)
4. Header liked list (HLL)
The first node, if the Backward contains the NULL, it indicated that it is the first node in the
list.
The node is which Forward contains, NULL indicates that the node is the last note
Chithra T R Page 3
DATA STRUCTURES
3. Header liked list (HLL):
A header linked list is a special type of linked list that contains a header node at the beginning of
the list. So, in a header linked list START will not point to the first node of the list but START
will contain the address of the header node. There are 2 types of Headers Linked List.
1. Grounded Header Linked List:
It is a list whose last node contains the NULL pointer. In the header linked list the start pointer
always points to the header node. start -> next = NULL indicates that the grounded header linked
list is empty. The operations that are possible on this type of linked list are Insertion, Deletion,
and Traversing.
Chithra T R Page 4
DATA STRUCTURES
4. Circularly linked list (CLL):
The circular linked list is a linked list where all nodes are connected to form a circle. In a circular
linked list, the first node and the last node are connected to each other which forms a circle.
There is no NULL at the end. The link field of the last node contains the memory address of the
first node, such a linked list is called circular linked list.
Chithra T R Page 5
DATA STRUCTURES
Operations on Singly or linear linked lists: Basic operations that can be performed on linked
list are
1. Creating a linked list:
The node of a linked list can be created by the following structure declaration.
Struct node
{
int data;
Struct node*next;
} *new, *head, *temp;
Where as
Node: name of the structure.
Data: Data or info it contains values for the data type like integer, char, float, etc...
Next: It used to store the address of the next node.
Next also contain address or pointer variable of the next node but the node is belonged to
the same structure is called self-referential structure or self-addressing pointer (EX :.
In this we use 3 pointer variables.
1. *New: It used for allocation of memory for node.
2. *Head: Head is always pointed to beginning of the node or 1st node.
3. *Temp: It is used for traversing all the node or traversing one node to another node.
2. Traversing a linked list:
Traversing is the process of visiting each node of the linked list exactly once to perform some
operation.
Algorithm: Travers (Head, Temp) Head contains the address of the first node. Another pointer
Temp is temporarily used to visit all the nodes from the beginning to the end of the linked list.
Step1: temp=head [Assign]
Step2: While (temp! =NULL) [ Being loop]
Step3: temp->data [ print data]
Step4: temp=temp->next [Move or Linking the next node] Step5: end of while Step6: stop.
Chithra T R Page 6
DATA STRUCTURES
Program to implement Creating and Traversing (or Display) linear or singly linked list.
#include <stdio.h>
#include<stdlib.h> Output:
void main() { Enter a data value for the node:8
struct node Do u want to add one more node to the list
{ [n/y]: y
int data; Enter a data value for the node:4
struct node *next; Do u want to add one more node to the list
}*new,*head,*temp; [n/y]: y
int value; Enter a data value for the node:7
char choice; Do u want to add one more node to the list
do{ [n/y]: n
new=(struct node*) malloc (sizeof(struct node)); 8-> 4-> 7->
printf("Enter a data value for the node:");
scanf("%d",&value);
new->data=value; Creating SLL
new->next=NULL;
if(head==NULL)
{
head=temp=new;
}
else
{
temp->next=new;
temp=new;
}
printf("\n Do u want to add one more node to the list [n/y]:\n");
fflush(stdin);
scanf("\n%c",&choice);
}while(choice=='y');
temp=head;
while(temp!=NULL)
{
printf("%d->\t",temp->data); Traversing Singly Linked List(SLL)
temp=temp->next;
}
getch();
}
3. Inserting an item into a linked list: Inserting an element into the existing Linked list.
Inserting element is divided in to 3 types.
1. Inserting a node at the beginning of the linked list: Steps to Insert a node at the Beginning
of the Linked List are,
1. Create a new node.
2. Initialize the data of this new node.
3. Update the next variable of the new node as head.
4. Update the head as the new node.
Chithra T R Page 7
DATA STRUCTURES
2. Inserting a node at the given position: Steps to Insert a node at the End of the Linked
List are,
1. Create a new node and initialize the data of this new node.
2. Create another node, temp with the value head.
3. Iterate over the Linked List while temp's next variable is not equal to NULL.
4. Now temp is at the temp of next node of the Linked List and we have to just update
the next variable of temp to the new node.
5. Also make sure to update the next variable of the new node as NULL.
3. Inserting a node at the end of the linked list: Steps to Insert a node at the End of the Linked
List are,
1. Create a new node and initialize the data of this new node.
2. Create another node, temp with the value head.
3. Iterate over the Linked List while temp's next variable is not equal to NULL.
4. Now temp is at the tail of the Linked List and we have to just update the next variable of
temp to the new node.
5. Also make sure to update the next variable of the new node as NULL.
Chithra T R Page 8
DATA STRUCTURES
..........MENU......
1. Create
2. Insert at Beginning
Program to implement Inserting element into 3.Insert at Ending
singly linked list at Beginning, Ending and given 4.Insert at specific position
Specific Position. 5.Display
# include <stdio.h> 6.Exit
# include <stdlib.h> Enter your choice:1
void create(); Enter a data value for the node:1
void display(); Do u want to add one more node to the list [n/y]: y
Enter a data value for the node:2
void IAB(); void IAE(); void IAP();
Do u want to add one more node to the list [n/y]: n
struct node ..........MENU......
{ 1. Create
int data; 2. Insert at Beginning
struct node *next; 3.Insert at Ending
}*new,*head,*temp; 4.Insert at specific position
void main() { 5.Display
int choice; clrscr(); 6.Exit
while(1) Enter your choice:2
{ Enter a value:0
printf("..........MENU. .... \n"); 0->1->2->NULL
Total number of nodes = 3
printf("1.Create \n");
..........MENU......
printf("2.Insert at Beginning \n"); 1. Create
printf("3.Insert at Ending \n"); 2. Insert at Beginning
printf("4.Insert at specific position\n"); 3.Insert at Ending
printf("5.Display\n"); 4.Insert at specific position
printf("6.Exit\n"); 5.Display
printf("Enter your choice:"); scanf("%d", &choice); 6.Exit
switch(choice) Enter your choice:3
{ Enter a value:4
case 1 : create();break; 0->1->2->4->NULL
case 2 : IAB();break; Total number of nodes = 4
..........MENU......
case 3 : IAE();break;
1. Create
case 4 : IAP();break; 2. Insert at Beginning
case 5 : display();break; 3.Insert at Ending
case 6 : exit(0); 4.Insert at specific position
default : printf("wrong choice\n"); break; 5.Display
} 6.Exit
} getch(); Enter your choice:4
} Enter the position at which element is inserted:4
void create() Enter an element:3
{ 0->1-> 2->3->4->NULL
int value; Total number of nodes = 5
..........MENU......
char choice;
1. Create
do{ 2. Insert at Beginning
new=(struct node*) malloc (sizeof (struct node)); 3.Insert at Ending
printf("Enter a data value for the node:"); 4.Insert at specific position
scanf("%d", &value); 5.Display
new->data=value; 6.Exit
Enter your choice:5
0->1->2->3->4->NULL
Total number of nodes = 5 8
DATA STRUCTURES
new->next=NULL;
if(head==NULL)
{
head=temp=new;
}
else
{
temp->next=new;
temp=new;
}
printf("Do u want to add one more node to the list [n/y]:");
fflush (stdin); temp->next=new;
scanf("\n %c", &choice); printf("Enter a value:");
}while(choice=='y'); scanf("%d", &value);
} new->data=value;
void display() new->next=NULL;
{ temp=new;
temp=head; display();
int count=0,value; }
while(temp!=NULL) void IAP()
{ {
printf("%d->", temp->data); int pos, i, value;
temp=temp->next; temp=head;
count=count+1; new=(struct node*)malloc(sizeof (struct node));
} printf("Enter the position at which element is
printf("NULL\n"); inserted:");
printf("Total number of nodes = %d \n", count); scanf("%d", &pos);
} printf("Enter an element:");
void IAB() scanf("%d", &value);
{ for(i=1;i<pos-1;i++)
int value; temp=temp->next;
new=(struct node*)malloc(sizeof (struct node)); new->data=value;
printf("Enter a value:"); new->next=temp->next;
scanf("%d", &value); temp->next=new;
new->data=value; display();
new->next=head; }
head=new;
display();
}
void IAE()
{
int value;
temp=head;
new=(struct node*)malloc(sizeof (struct node));
while(temp->next!=NULL)
temp=temp->next;
9
DATA STRUCTURES
4. Deleting an item from the linked list: Deleting an existing node from the Linked list. It
involves following steps.
1. If the linked list is empty then deletion is not possible & this condition is called underflow.
2. To delete a node, loop through the nodes until the node to be deleted is found.
3. Adjust the link address of the previous node.
4. Free the memory space utilized by the deleted node.
Deleting element is divided in to 3 types.
1. Deleting a node at the beginning of the linked list: First check whether the list is empty or not
(underflow), move the start pointer to the 2nd node and remove the previous node.
2. Deleting a node at the given position: To delete the last node of a linked list, traverse from the
beginning to the end of the list and locate the last node and its previous.
1. Check the list is empty or not.
2. Traverse the pointer variable p1or D to the end of the list.
3. Locate the last node and its previous.
3. Deleting a node at the end of the linked list: This involves following steps.
1) Check the list is empty or not.
2) If the list is not empty then count the number of nodes.
3) Check the position is valid or not.
4) If the position is valid then check two more conditions
a) If pos=1, call DAB function.
b) If pos = count call DAE function.
5) To find the position, locate p1 or D to position and previous node by temp.
6) Disconnect the detected node and remove.
10
DATA STRUCTURES
Program to implement Deleting element into SLL from Beginning, Ending and Specific
position. Output:
# include <stdio.h> ..........MENU......
# include <stdlib.h> 1. Create
void create(); 2. Delete from Beginning
3.Delete from Ending
void display(); 4.Delete from specific position
void DFB(); void DFE(); void DFP(); 5.Display
struct node 6.Exit
{ Enter your choice:1
int data; Enter a data value for the node:1
struct node *next; Do u want to add one more node to the list [n/y]:y
Enter a data value for the node:2
}*new,*head,*temp,*d; Do u want to add one more node to the list [n/y]:y
void main() { Enter a data value for the node:3
int choice; clrscr(); Do u want to add one more node to the list [n/y]:y
while(1) Enter a data value for the node:4
{ Do u want to add one more node to the list [n/y]:n
..........MENU......
printf("..........MENU. .... \n"); 1. Create
printf("1.Create \n"); 2. Delete from Beginning
printf("2.Delete from Beginning \n"); 3.Delete from Ending
printf("3.Delete from Ending \n"); 4.Delete from specific position
printf("4.Delete from specific position\n"); 5.Display
printf("5.Display\n"); 6.Exit
Enter your choice:5
printf("6.Exit\n"); 1->2->3->4->NULL
printf("Enter your choice:"); scanf("%d", &choice); Total number of nodes =4
switch(choice) ..........MENU......
{ 1. Create
case 1 : create();break; 2. Delete from Beginning
case 2 : DFB();break; 3.Delete from Ending
4.Delete from specific position
case 3 : DFE();break; 5.Display
case 4 : DFP();break; 6.Exit
case 5 : display();break; Enter your choice:2
case 6 : exit(0); Deleted node is 1:2->3->4->NULL
default : printf("wrong choice\n"); break; Total number of nodes =3
..........MENU......
} 1. Create
}getch(); 2. Delete from Beginning
} 3.Delete from Ending
void create() 4.Delete from specific position
{ 5.Display 6.Exit
int value; Enter your choice:3
Deleted node is 3:2->3->NULL
char choice; Total number of nodes =2
do{ ..........MENU......
new=(struct node*) malloc (sizeof(struct node)); 1. Create
printf("Enter a data value for the node:"); 2. Delete from Beginning
scanf("%d", &value); 3.Delete from Ending
new->data=value; 4.Delete from specific position
5.Display 6.Exit
new->next=NULL; Enter your choice:4
Enter the position at which element is to be
deleted:2
deleted node is 3:2->NULL 11
Total number of nodes =1
DATA STRUCTURES
if(head==NULL)
{
head=temp=new;
}
else
{
temp->next=new;
temp=new;
}
printf("Do u want to add one more node to the list [n/y]:");
fflush (stdin);
scanf("\n %c", &choice);
}while(choice=='y');
}
}
void display()
void DFP()
{
{
temp=head;
int pos, i, count;
int count=0,value;
printf("Enter the position at which element is to
while(temp!=NULL)
be deleted:");
{
scanf("%d", &pos);
printf("%d->",temp->data);
if (pos==1)
temp=temp->next;
DFB();
count=count+1;
else if(pos==count)
}
DFE();
printf("NULL\n");
else
printf("Total number of nodes =%d \n", count);
temp=head;
}
for(i=1;i<pos-1;i++)
void DFB()
temp=temp->next;
{
d=temp->next;
temp=head;
printf("deleted node is %d:", d->data);
printf("Deleted node is %d:", temp->data);
temp->next=d->next;
head=head->next;
d->next=NULL;
temp->next=NULL;
free(d);
free(temp);
display();
display();
}
}
void DFE()
{
temp=head;
while(temp->next->next!=NULL)
temp=temp->next;
d=temp->next;
printf("Deleted node is %d:", temp->data);
temp->next=NULL;
free(d);
display();
12
DATA STRUCTURES
6.Searching an item in the linked list: Searching is performed in order to find the location of a
particular element in the list. The most common search used on linked list structures is sequential
search. In this method the given item is searched by traversing through the list and makes the
comparison of every element of the list until it is found. If the element is matched with any of the
list element, then the location of the element is returned.
Algorithm:
Step-1: Initialize the current pointer with the beginning of the List.
Step-2: Compare the KEY value with the Current node value; if they match then exit else go
to step-3.
Step-3: Move the current pointer to point to the next node in the list and go to step-2, till the
list is not over or else quit.
Chithra T R Page 15
DATA STRUCTURES
Output:
..........MENU......
1.Create
2.Display
3.Insert at Beginning
4.Delete from Beginning
5.Exit
Enter your choice:1
Enter a data value for the node:1
Do u want to add one more node to the list
[n/y]:y
Enter a data value for the node:2
Do u want to add one more node to the list
[n/y]:y
Enter a data value for the node:3
Do u want to add one more node to the list
[n/y]:n
..........MENU......
1.Create
2.Display
3.Insert at Beginning
4.Delete from Beginning
5.Exit
Enter your choice:21-
>2->3->NULL
Total number of nodes =3
..........MENU......
1.Create
2.Display
3.Insert at Beginning
4.Delete from Beginning
5.Exit
Enter your choice:3
Enter a value:0
0->1->2->3->NULL
Total number of nodes =4
..........MENU......
1.Create
2.Display
3. Insert at Beginning
4.Delete from Beginning
5.Exit
Enter your choice:4
Deleted node is 0:1->2->3->NULL
Total number of nodes =3
Chithra T R Page 16
DATA STRUCTURES
head=temp=new;
}
else
{
temp->next=new;
temp=new;
}
printf("Do u want to add one more node to the list [n/y]:");
fflush (stdin);
scanf("\n %c", &choice);
}while(choice=='y');
}
void display()
{
temp=head;
int count=0,value;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
count=count+1;
}
printf("NULL\n");
printf("Total number of nodes =%d \n", count);
}
void IAB()
{
int value;
new=(struct node*)malloc(sizeof (struct node));
printf("Enter a value:");
scanf("%d", &value);
new->data=value;
new->next=head;
head=new;
display();
}
void DFB()
{
temp=head;
printf("Deleted node is %d:", temp->data);
head=head->next;
temp->next=NULL;
free(temp);
display();
}
Chithra T R Page 17
DATA STRUCTURES
Memory allocation:
The library function malloc is used to allocate a block of memory on the heap. The program
accesses this block of memory via a pointer that malloc returns. When the memory is no longer
needed, the pointer is passed to free which deallocates the memory so that it can be used for
other purposes.
There are two types of memory allocations: Compile-time or Static Memory Allocation. Run-
time or Dynamic Memory Allocation. And briefly explain in unit 1 notes
Garbage collection:
Collect all node which are allocated but no longer in use is known as Garbage collection.
Or
Garbage collection is an automated dynamic memory management that identifies dead memory
blocks and reallocates storage for reuse.
Chithra T R Page 18