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

U-2

The document provides an overview of linked lists, a fundamental data structure consisting of nodes that contain data and references to the next node. It details the characteristics, operations, and complexities of singly linked lists, including insertion, deletion, and searching methods. Additionally, the document briefly introduces doubly linked lists, which have pointers to both the previous and next nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

U-2

The document provides an overview of linked lists, a fundamental data structure consisting of nodes that contain data and references to the next node. It details the characteristics, operations, and complexities of singly linked lists, including insertion, deletion, and searching methods. Additionally, the document briefly introduces doubly linked lists, which have pointers to both the previous and next nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

R23 Data Structures

Unit-2
Linked Lists
A linked list is a fundamental data structure in computer science. It consists of nodes where each
node contains data and a reference (link) to the next node in the sequence. This allows for dynamic
memory allocation and efficient insertion and deletion operations compared to arrays.

Linked-List-Data-Structure
In simple words, a linked list consists of nodes where each node contains a data field and a
reference(link) to the next node in the list.
R23 Data Structures

Uses of Linked List:

o The list is not required to be contiguously present in the memory. The node can reside any
where in the memory and linked together to make a list. This achieves optimized utilization
of space.
o list size is limited to the memory size and doesn't need to be declared in advance.
o Empty node can not be present in the linked list.
o We can store values of primitive types or objects in the singly linked list.

Singly linked lists: representation and operations

Singly linked list or One way chain

Singly linked list can be defined as the collection of ordered set of elements. The number of
elements may vary according to need of the program. A node in the singly linked list consist of
two parts: data part and link part. Data part of the node stores actual information that is to be
represented by the node while the link part of the node stores the address of its immediate
successor.

One way chain or singly linked list can be traversed only in one direction. In other words, we can
say that each node contains only next pointer, therefore we can not traverse the list in the reverse
direction.

Consider an example where the marks obtained by the student in three subjects are stored in a
linked list as shown in the figure.

In the above figure, the arrow represents the links. The data part of every node contains the marks
obtained by the student in the different subject. The last node in the list is identified by the null
pointer which is present in the address part of the last node. We can have as many elements we
require, in the data part of the list.
R23 Data Structures

Complexity:

Data Time Complexity Space


Structure Compleity

Average Worst Worst

Access Search Insertion Deletion Access Search Insertion Deletion

Singly θ(n) θ(n) θ(1) θ(1) O(n) O(n) O(1) O(1) O(n)
Linked
List

Operations on Singly Linked List:

There are various operations which can be performed on singly linked list. A list of all such
operations is given below.

Node Creation

struct node

int data;

struct node *next;

};

struct node *head, *ptr;

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


R23 Data Structures

Insertion: The insertion into a singly linked list can be performed at different positions. Based on
the position of the new node being inserted, the insertion is categorized into the following
categories.

SN Operation Description

1 Insertion at It involves inserting any element at the front of the


beginning list. We just need to a few link adjustments to make
the new node as the head of the list.

2 Insertion at It involves insertion at the last of the linked list.


end of the list The new node can be inserted as the only node in
the list or it can be inserted as the last one. Different
logics are implemented in each scenario.

3 Insertion after It involves insertion after the specified node of the


specified node linked list. We need to skip the desired number of
nodes in order to reach the node after which the
new node will be inserted. .

Deletion and Traversing:

The Deletion of a node from a singly linked list can be performed at different positions. Based on
the position of the node being deleted, the operation is categorized into the following categories

.
R23 Data Structures

SN Operation Description

1 Deletion at It involves deletion of a node from the beginning of the list. This is the simplest
beginning operation among all. It just need a few adjustments in the node pointers.

2 Deletion at the It involves deleting the last node of the list. The list can either be empty or full.
end of the list Different logic is implemented for the different scenarios.

3 Deletion after It involves deleting the node after the specified node in the list. we need to skip
specified node the desired number of nodes to reach the node after which the node will be
deleted. This requires traversing through the list.

4 Traversing In traversing, we simply visit each node of the list at least once in order to
perform some specific operation on it, for example, printing data part of each
node present in the list.

5 Searching In searching, we match each element of the list with the given element. If the
element is found on any of the location then location of that element is returned
otherwise null is returned. .

Implementing Single linked List

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

};

struct node *head;


R23 Data Structures

void beginsert ();

void lastinsert ();

void randominsert();

void begin_delete();

void last_delete();

void random_delete();

void display();

void search();

void main ()

int choice =0;

while(choice != 9)

printf("\n\n*********Main Menu*********\n");

printf("\nChoose one option from the following list ...\n");

printf("\n===============================================\n");

printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete from


Beginning\n

5.Delete from last\n6.Delete node after specified location\n7.Search for an


element\n8.Show\n9.Exit\n");

printf("\nEnter your choice?\n");

scanf("\n%d",&choice);

switch(choice)

{
R23 Data Structures

case 1:

beginsert();

break;

case 2:

lastinsert();

break;

case 3:

randominsert();

break;

case 4:

begin_delete();

break;

case 5:

last_delete();

break;

case 6:

random_delete();

break;

case 7:

search();

break;

case 8:

display();
R23 Data Structures

break;

case 9:

exit(0);

break;

default:

printf("Please enter valid choice..");

void beginsert()

struct node *ptr;

int item;

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

if(ptr == NULL)

printf("\nOVERFLOW");

else

printf("\nEnter value\n");

scanf("%d",&item);

ptr->data = item;
R23 Data Structures

ptr->next = head;

head = ptr;

printf("\nNode inserted");

void lastinsert()

struct node *ptr,*temp;

int item;

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

if(ptr == NULL)

printf("\nOVERFLOW");

else

printf("\nEnter value?\n");

scanf("%d",&item);

ptr->data = item;

if(head == NULL)

ptr -> next = NULL;


R23 Data Structures

head = ptr;

printf("\nNode inserted");

else

temp = head;

while (temp -> next != NULL)

temp = temp -> next;

temp->next = ptr;

ptr->next = NULL;

printf("\nNode inserted");

void randominsert()

int i,loc,item;

struct node *ptr, *temp;

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

if(ptr == NULL)
R23 Data Structures

printf("\nOVERFLOW");

else

printf("\nEnter element value");

scanf("%d",&item);

ptr->data = item;

printf("\nEnter the location after which you want to insert ");

scanf("\n%d",&loc);

temp=head;

for(i=0;i<loc;i++)

temp = temp->next;

if(temp == NULL)

printf("\ncan't insert\n");

return;

ptr ->next = temp ->next;

temp ->next = ptr;


R23 Data Structures

printf("\nNode inserted");

void begin_delete()

struct node *ptr;

if(head == NULL)

printf("\nList is empty\n");

else

ptr = head;

head = ptr->next;

free(ptr);

printf("\nNode deleted from the begining ...\n");

void last_delete()

struct node *ptr,*ptr1;

if(head == NULL)

{
R23 Data Structures

printf("\nlist is empty");

else if(head -> next == NULL)

head = NULL;

free(head);

printf("\nOnly node of the list deleted ...\n");

else

ptr = head;

while(ptr->next != NULL)

ptr1 = ptr;

ptr = ptr ->next;

ptr1->next = NULL;

free(ptr);

printf("\nDeleted Node from the last ...\n");

void random_delete()
R23 Data Structures

struct node *ptr,*ptr1;

int loc,i;

printf("\n Enter the location of the node after which you want to perform deletion \n");

scanf("%d",&loc);

ptr=head;

for(i=0;i<loc;i++)

ptr1 = ptr;

ptr = ptr->next;

if(ptr == NULL)

printf("\nCan't delete");

return;

ptr1 ->next = ptr ->next;

free(ptr);

printf("\nDeleted node %d ",loc+1);

void search()

{
R23 Data Structures

struct node *ptr;

int item,i=0,flag;

ptr = head;

if(ptr == NULL)

printf("\nEmpty List\n");

else

printf("\nEnter item which you want to search?\n");

scanf("%d",&item);

while (ptr!=NULL)

if(ptr->data == item)

printf("item found at location %d ",i+1);

flag=0;

else

flag=1;

i++;
R23 Data Structures

ptr = ptr -> next;

if(flag==1)

printf("Item not found\n");

void display()

struct node *ptr;

ptr = head;

if(ptr == NULL)

printf("Nothing to print");

else

printf("\nprinting values . . . . .\n");

while (ptr!=NULL)

{
R23 Data Structures

printf("\n%d",ptr->data);

ptr = ptr -> next;

Output:

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show
R23 Data Structures

9.Exit

Enter your choice?

Enter value

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element


R23 Data Structures

8.Show

9.Exit

Enter your choice?

Enter value?

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location


R23 Data Structures

7.Search for an element

8.Show

9.Exit

Enter your choice?

Enter element value1

Enter the location after which you want to insert 1

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning


R23 Data Structures

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

printing values . . . . .

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last
R23 Data Structures

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

Enter value?

123

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
R23 Data Structures

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

Enter value

1234

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================
R23 Data Structures

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

Node deleted from the begining ...

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last
R23 Data Structures

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

Deleted Node from the last ...

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning


R23 Data Structures

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

Enter the location of the node after which you want to perform deletion

Deleted node 2

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location


R23 Data Structures

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

printing values . . . . .

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last
R23 Data Structures

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

Enter item which you want to search?

item found at location 1

item found at location 2

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
R23 Data Structures

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

Doubly linked lists representation and operations

Doubly linked list is a complex type of linked list in which a node contains a pointer to the previous
as well as the next node in the sequence. Therefore, in a doubly linked list, a node consists of three
parts: node data, pointer to the next node in sequence (next pointer) , pointer to the previous node
(previous pointer). A sample node in a doubly linked list is shown in the figure.

A doubly linked list containing three nodes having numbers from 1 to 3 in their data part, is shown
in the following image.
R23 Data Structures

In C, structure of a node in doubly linked list can be given as :

struct node
{
struct node *prev;
int data;
struct node *next;
}
The prev part of the first node and the next part of the last node will always contain null indicating
end in each direction.
In a singly linked list, we could traverse only in one direction, because each node contains address
of the next node and it doesn't have any record of its previous nodes. However, doubly linked list
overcome this limitation of singly linked list. Due to the fact that, each node of the list contains
the address of its previous node, we can find all the details about the previous node as well by
using the previous address stored inside the previous part of each node.
Operations on doubly linked list

Node Creation

struct node
{
struct node *prev;
R23 Data Structures

int data;
struct node *next;
};
struct node *head;
All the remaining operations regarding doubly linked list are described in the following table.

SN Operation Description

1 Insertion at beginning Adding the node into the linked list at beginning.

2 Insertion at end Adding the node into the linked list to the end.

3 Insertion after specified Adding the node into the linked list after the specified node.
node

4 Deletion at beginning Removing the node from beginning of the list

5 Deletion at the end Removing the node from end of the list.

6 Deletion of the node Removing the node which is present just after the node containing the
having given data given data.

7 Searching Comparing each node data with the item to be searched and return the
location of the item in the list if the item found else return null.

8 Traversing Visiting each node of the list at least once in order to perform some
specific operation like searching, sorting, display, etc.

Program to implement all the operations of doubly linked list

#include<stdio.h>
#include<stdlib.h>
R23 Data Structures

struct node
{
struct node *prev;
struct node *next;
int data;
};
struct node *head;
void insertion_beginning();
void insertion_last();
void insertion_specified();
void deletion_beginning();
void deletion_last();
void deletion_specified();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete
from Beginning\n
5.Delete from last\n6.Delete the node after the given data\n7.Search\n8.Show\n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
R23 Data Structures

{
case 1:
insertion_beginning();
break;
case 2:
insertion_last();
break;
case 3:
insertion_specified();
break;
case 4:
deletion_beginning();
break;
case 5:
deletion_last();
break;
case 6:
deletion_specified();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
R23 Data Structures

default:
printf("Please enter valid choice..");
}
}
}
void insertion_beginning()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value");
scanf("%d",&item);

if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
R23 Data Structures

ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode inserted\n");
}

}
void insertion_last()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
R23 Data Structures

head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}

}
printf("\nnode inserted\n");
}
void insertion_specified()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
R23 Data Structures

temp=head;
printf("Enter the location");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\n There are less than %d elements", loc);
return;
}
}
printf("Enter value");
scanf("%d",&item);
ptr->data = item;
ptr->next = temp->next;
ptr -> prev = temp;
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
}
}
void deletion_beginning()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
R23 Data Structures

}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nnode deleted\n");
}

}
void deletion_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
R23 Data Structures

printf("\nnode deleted\n");
}
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_specified()
{
struct node *ptr, *temp;
int val;
printf("\n Enter the data after which the node is to be deleted : ");
scanf("%d", &val);
ptr = head;
while(ptr -> data != val)
ptr = ptr -> next;
if(ptr -> next == NULL)
{
printf("\nCan't delete\n");
}
else if(ptr -> next -> next == NULL)
R23 Data Structures

{
ptr ->next = NULL;
}
else
{
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
printf("\nnode deleted\n");
}
}
void display()
{
struct node *ptr;
printf("\n printing values...\n");
ptr = head;
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
R23 Data Structures

if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("\nitem found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("\nItem not found\n");
}
}
R23 Data Structures

}
Output:

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit

Enter your choice?


1

Enter Item value12

Node inserted

*********Main Menu*********
R23 Data Structures

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit

Enter your choice?


1

Enter Item value123

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================
R23 Data Structures

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit

Enter your choice?


1

Enter Item value1234

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
R23 Data Structures

6.Delete the node after the given data


7.Search
8.Show
9.Exit

Enter your choice?


8

printing values...
1234
123
12

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
R23 Data Structures

Enter your choice?


2

Enter value89

node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit

Enter your choice?


3
Enter the location 3
Enter value12345
R23 Data Structures

node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit

Enter your choice?


8

printing values...
1234
123
12345
12
89
R23 Data Structures

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit

Enter your choice?


4

node deleted

*********Main Menu*********

Choose one option from the following list ...

===============================================
R23 Data Structures

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit

Enter your choice?


5

node deleted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
R23 Data Structures

8.Show
9.Exit

Enter your choice?


8

printing values...
123
12345
12

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit

Enter your choice?


R23 Data Structures

Enter the data after which the node is to be deleted : 123

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit

Enter your choice?


8

printing values...
123
12

*********Main Menu*********
R23 Data Structures

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit

Enter your choice?


7

Enter item which you want to search?


123

item found at location 1


*********Main Menu*********

Choose one option from the following list ...

===============================================
R23 Data Structures

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit

Enter your choice?


6

Enter the data after which the node is to be deleted : 123

Node deleted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
R23 Data Structures

6.Delete the node after the given data


7.Search
8.Show
9.Exit

Enter your choice?


9

Exited..

Circular linked lists representation and operations

In a circular Singly linked list, the last node of the list contains a pointer to the first node of the
list. We can have circular singly linked list as well as circular doubly linked list.
We traverse a circular singly linked list until we reach the same node where we started. The circular
singly liked list has no beginning and no ending. There is no null value present in the next part of
any of the nodes.
The following image shows a circular singly linked list.

Circular linked list are mostly used in task maintenance in operating systems. There are many
examples where circular linked list are being used in computer science including browser surfing
R23 Data Structures

where a record of pages visited in the past by the user, is maintained in the form of circular linked
lists and can be accessed again on clicking the previous button.

Operations on Circular Singly linked list:

Insertion

SN Operation Description

1 Insertion at beginning Adding a node into circular singly linked list at the beginning.

2 Insertion at the end Adding a node into circular singly linked list at the end.

Deletion & Traversing

SN Operation Description

1 Deletion at Removing the node from circular singly linked list at the beginning.
beginning

2 Deletion at the Removing the node from circular singly linked list at the end.
end

3 Searching Compare each element of the node with the given item and return the
location at which the item is present in the list otherwise return null.

4 Traversing Visiting each element of the list at least once in order to perform some
specific operation.
R23 Data Structures

Program to implement circular singly linked list

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;

void beginsert ();


void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 7)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beginning\n4.Delete from
last\n5.Search for an element\n6.Show\n7.Exit\n");
R23 Data Structures

printf("\nEnter your choice?\n");


scanf("\n%d",&choice);
switch(choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
begin_delete();
break;
case 4:
last_delete();
break;
case 5:
search();
break;
case 6:
display();
break;
case 7:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
R23 Data Structures

}
}
void beginsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter the node data?");
scanf("%d",&item);
ptr -> data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp->next != head)
temp = temp->next;
ptr->next = head;
temp -> next = ptr;
R23 Data Structures

head = ptr;
}
printf("\nnode inserted\n");
}

}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\nEnter Data?");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
R23 Data Structures

while(temp -> next != head)


{
temp = temp -> next;
}
temp -> next = ptr;
ptr -> next = head;
}

printf("\nnode inserted\n");
}

void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nUNDERFLOW");
}
else if(head->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}

else
R23 Data Structures

{ ptr = head;
while(ptr -> next != head)
ptr = ptr -> next;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\nnode deleted\n");

}
}
void last_delete()
{
struct node *ptr, *preptr;
if(head==NULL)
{
printf("\nUNDERFLOW");
}
else if (head ->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");

}
else
{
ptr = head;
while(ptr ->next != head)
R23 Data Structures

{
preptr=ptr;
ptr = ptr->next;
}
preptr->next = ptr -> next;
free(ptr);
printf("\nnode deleted\n");

}
}

void search()
{
struct node *ptr;
int item,i=0,flag=1;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
if(head ->data == item)
{
printf("item found at location %d",i+1);
flag=0;
R23 Data Structures

}
else
{
while (ptr->next != head)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
}
if(flag != 0)
{
printf("Item not found\n");
}
}

void display()
R23 Data Structures

{
struct node *ptr;
ptr=head;
if(head == NULL)
{
printf("\nnothing to print");
}
else
{
printf("\n printing values ... \n");

while(ptr -> next != head)


{

printf("%d\n", ptr -> data);


ptr = ptr -> next;
}
printf("%d\n", ptr -> data);
}

}
Output:

*********Main Menu*********

Choose one option from the following list ...

===============================================
R23 Data Structures

1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit

Enter your choice?


1

Enter the node data?10

node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
R23 Data Structures

7.Exit

Enter your choice?


2

Enter Data?20

node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit

Enter your choice?


2

Enter Data?30
R23 Data Structures

node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit

Enter your choice?


3

node deleted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
R23 Data Structures

2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit

Enter your choice?


4

node deleted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit

Enter your choice?


5
R23 Data Structures

Enter item which you want to search?


20
item found at location 1
*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit

Enter your choice?


6

printing values ...


20

*********Main Menu*********

Choose one option from the following list ...


R23 Data Structures

===============================================

1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit

Enter your choice?


7
Comparing arrays and linked lists:
Array: Arrays store elements in contiguous memory locations, resulting in easily calculable
addresses for the elements stored and this allows faster access to an element at a specific index.

Data storage scheme of an array

Linked List: Linked lists are less rigid in their storage structure and elements are usually not
stored in contiguous locations, hence they need to be stored with additional tags giving a
reference to the next element.
R23 Data Structures

Linked-List representation

Differences between array and linked-list are:

Applications of linked lists

Applications of linked list in computer science:

1. Implementation of stacks and queues


2. Implementation of graphs: Adjacency list representation of graphs is the most
popular which uses a linked list to store adjacent vertices.
3. Dynamic memory allocation: We use a linked list of free blocks.
4. Maintaining a directory of names
R23 Data Structures

5. Performing arithmetic operations on long integers


6. Manipulation of polynomials by storing constants in the node of the linked list
7. Representing sparse matrices

Applications of linked list in the real world:

1. Image viewer – Previous and next images are linked and can be accessed by the next
and previous buttons.
2. Previous and next page in a web browser – We can access the previous and next URL
searched in a web browser by pressing the back and next buttons since they are linked
as a linked list.
3. Music Player – Songs in the music player are linked to the previous and next songs.
So you can play songs either from starting or ending of the list.
4. GPS navigation systems- Linked lists can be used to store and manage a list of
locations and routes, allowing users to easily navigate to their desired destination.
5. Task Scheduling- Operating systems use linked lists to manage task scheduling,
where each process waiting to be executed is represented as a node in the list.
6. Image Processing- Linked lists can be used to represent images, where each pixel is
represented as a node in the list.
7. File Systems- File systems use linked lists to represent the hierarchical structure of
directories, where each directory or file is represented as a node in the list.
8. Symbol Table- Compilers use linked lists to build a symbol table, which is a data
structure that stores information about identifiers used in a program.
9. Undo/Redo Functionality- Many software applications implement undo/redo
functionality using linked lists, where each action that can be undone is represented
as a node in a doubly linked list.
10. Polynomial Representation- Polynomials can be represented using linked lists, where
each term in the polynomial is represented as a node in the list.

Polynomial Operations using Linked Lists


R23 Data Structures

The linked list can be used to represent a polynomial of any degree. Simply the information field
is changed according to the number of variables used in the polynomial. If a single variable is
used in the polynomial the information field of the node contains two parts: one for coefficient
of variable and the other for degree of variable. Let us consider an example to represent a
polynomial

Polynomial:3x3-4x2+2x-9

Linked List:

In the above linked list, the external pointer ‘ROOT’ point to the first node of the linked list. The
first node of the linked list contains the information about the variable with the highest degree.
The first node points to the next node with next lowest degree of the variable.
Representation of a polynomial using the linked list is beneficial when the operations on the
polynomial like addition and multiplications are performed. The resulting polynomial can also
be traversed very easily to display the polynomial.
R23 Data Structures

Addition of Polynomials: The above two linked lists represent the polynomials,3x 3-4x2+2x-9
and 5x3-2x2+6x+3 respectively. If both the polynomials are added then the resulting linked will
be 8x3-6x2+8x-6.

Multiplication of Polynomials: Given two polynomials in the form of linked list. The task is to
find the multiplication of both polynomials.
Input: Poly1: 3x^2 + 5x^1 + 6, Poly2: 6x^1 + 8
Output: 18x^3 + 54x^2 + 76x^1 + 48
On multiplying each element of 1st polynomial with
elements of 2nd polynomial, we get
18x^3 + 24x^2 + 30x^2 + 40x^1 + 36x^1 + 48
On adding values with same power of x,
18x^3 + 54x^2 + 76x^1 + 48
R23 Data Structures

Representing sparse matrices using Linked List

Sparse Matrix Representations can be done by using Linked lists. In linked list, each node has
four fields. These four fields are defined as:

Row: Index of row, where non-zero element is located


Column: Index of column, where non-zero element is located
Value: Value of the non zero element located at index – (row,column)
Next node: Address of the next node.
R23 Data Structures

Unit-2

1. What is a Linked List? Representation of Linked list and what are its Applications?

2. Explain Single linked list and its operations?

3. Explain Double linked list and its operations?

4. Explain Circular linked list and its operations?

5(a). Explain Sparse Matrix Representation using Linked List for the following matrix?

5(b). Write about Polynomial Expression Representation, Addition and Multiplication?


Polynomial expression representation 3x3-4x2 +2x-9
Polynomial expression Addition 3x3-4x2+2x-9 and 5x3 -2x2 +6x+3
Polynomial expression Multiplication Poly1: 3x^2 + 5x^1 + 6, Poly2: 6x^1 + 8

You might also like