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

Unit II Notes Ds

A linked list is a data structure consisting of nodes connected by links, where each node contains data and a reference to the next node. There are various types of linked lists, including simple, doubly, and circular linked lists, and they support basic operations such as insertion, deletion, and searching. The document also provides detailed algorithms for performing these operations in a single linked list using C/C++.

Uploaded by

divyapodapati2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit II Notes Ds

A linked list is a data structure consisting of nodes connected by links, where each node contains data and a reference to the next node. There are various types of linked lists, including simple, doubly, and circular linked lists, and they support basic operations such as insertion, deletion, and searching. The document also provides detailed algorithms for performing these operations in a single linked list using C/C++.

Uploaded by

divyapodapati2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Linked List:

A linked list is a sequence of data structures, which are connected together via links.

Linked List is a sequence of links which contains items. Each link contains a connection
to another link. Linked list is the second most-used data structure after array. Following
are the important terms to understand the concept of Linked List.

Link − Each link of a linked list can store a data called an element.

Next − Each link of a linked list contains a link to the next link called Next.

LinkedList − A Linked List contains the connection link to the first link called First.

Linked List Representation


Linked list can be visualized as a chain of nodes, where every node points to the next
node.

As per the above illustration, following are the important points to be considered.
 Linked List contains a link element called first.
 Each link carries a data field(s) and a link field called next.
 Each link is linked with its next link using its next link.
 Last link carries a link as null to mark the end of the list.

Types of Linked List


Following are the various types of linked list.
 Simple Linked List − Item navigation is forward only.
 Doubly Linked List − Items can be navigated forward and backward.
 Circular Linked List − Last item contains link of the first element as next and the
first element has a link to the last element as previous.

Basic Operations
Following are the basic operations supported by a list.
 Insertion − Adds an element at the beginning of the list.
 Deletion − Deletes an element at the beginning of the list.
 Display − Displays the complete list.
 Search − Searches an element using the given key.
 Delete − Deletes an element using the given key.

Linked List Operations: Traverse,


Insert and Delete
In this tutorial, you will learn different operations on a linked list. Also, you will
find implementation of linked list operations in C/C++, Python and Java.

There are various linked list operations that allow us to perform different
actions on linked lists. For example, the insertion operation adds a new
element to the linked list.

Here's a list of basic linked list operations that we will cover in this article.

 Traversal - access each element of the linked list


 Insertion - adds a new element to the linked list
 Deletion - removes the existing elements
 Search - find a node in the linked list
 Sort - sort the nodes of the linked list
Before you learn about linked list operations in detail, make sure to know
about Linked List first.
Things to Remember about Linked List

 head points to the first node of the linked list


 next pointer of the last node is NULL , so if the next current node is NULL , we
have reached the end of the linked list.
In all of the examples, we will assume that the linked list has three nodes 1

--->2 --->3 with node structure as below:

struct node {
int data;
struct node *next;
};

Traverse a Linked List


Displaying the contents of a linked list is very simple. We keep moving the
temp node to the next one and display its contents.

When temp is NULL, we know that we have reached the end of the linked list so
we get out of the while loop.

struct node *temp = head;


printf("\n\nList elements are - \n");
while(temp != NULL) {
printf("%d --->",temp->data);
temp = temp->next;
}

The output of this program will be:

List elements are -


1 --->2 --->3 --->

Insert Elements to a Linked List:

1. Insert at the beginning

 Allocate memory for new node

 Store data

 Change next of new node to point to head


 Change head to point to recently created node

struct node *newNode;


newNode = malloc(sizeof(struct node));
newNode->data = 4;
newNode->next = head;
head = newNode;

2. Insert at the End

 Allocate memory for new node

 Store data

 Traverse to last node

 Change next of last node to recently created node

struct node *newNode;


newNode = malloc(sizeof(struct node));
newNode->data = 4;
newNode->next = NULL;

struct node *temp = head;


while(temp->next != NULL){
temp = temp->next;
}

temp->next = newNode;

3. Insert at the Middle

 Allocate memory and store data for new node

 Traverse to node just before the required position of new node

 Change next pointers to include new node in between

struct node *newNode;


newNode = malloc(sizeof(struct node));
newNode->data = 4;

struct node *temp = head;

for(int i=2; i < position; i++) {


if(temp->next != NULL) {
temp = temp->next;
}
}
newNode->next = temp->next;
temp->next = newNode;

Delete from a Linked List


You can delete either from the beginning, end or from a particular position.

1. Delete from beginning

 Point head to the second node

head = head->next;

2. Delete from end

 Traverse to second last element

 Change its next pointer to null

struct node* temp = head;


while(temp->next->next!=NULL){
temp = temp->next;
}
temp->next = NULL;

3. Delete from middle

 Traverse to element before the element to be deleted

 Change next pointers to exclude the node from the chain

for(int i=2; i< position; i++) {


if(temp->next!=NULL) {
temp = temp->next;
}
}

temp->next = temp->next->next;

Search an Element on a Linked List


You can search an element on a linked list using a loop using the following
steps. We are finding item on a linked list.
 Make head as the current node.
 Run a loop until the current node is NULL because the last element points
to NULL .

 In each iteration, check if the key of the node is equal to item . If it the key
matches the item, return true otherwise return false .

// Search a node
bool searchNode(struct Node** head_ref, int key) {
struct Node* current = *head_ref;

while (current != NULL) {


if (current->data == key) return true;
current = current->next;
}
return false;
}

ALGORITHMS FOR INSERTION AND DELETION IN SINGLE LINKED


LIST:

Operations on Single Linked List


The following operations are performed on a Single Linked List

 Insertion
 Deletion
 Display

Before we implement actual operations, first we need to set up an empty list. First, perform the
following steps before implementing actual operations.

 Step 1 - Include all the header files which are used in the program.
 Step 2 - Declare all the user defined functions.
 Step 3 - Define a Node structure with two members data and next
 Step 4 - Define a Node pointer 'head' and set it to NULL.
 Step 5 - Implement the main method by displaying operations menu and make suitable
function calls in the main method to perform user selected operation.

Insertion
In a single linked list, the insertion operation can be performed in three ways. They are as follows...

1. Inserting At Beginning of the list


2. Inserting At End of the list
3. Inserting At Specific location in the list

Inserting At Beginning of the list


We can use the following steps to insert a new node at beginning of the single linked list...

 Step 1 - Create a newNode with given value.


 Step 2 - Check whether list is Empty (head == NULL)
 Step 3 - If it is Empty then, set newNode→next = NULL and head = newNode.
 Step 4 - If it is Not Empty then, set newNode→next = head and head = newNode.
Inserting At End of the list
We can use the following steps to insert a new node at end of the single linked list...

 Step 1 - Create a newNode with given value and newNode → next as NULL.
 Step 2 - Check whether list is Empty (head == NULL).
 Step 3 - If it is Empty then, set head = newNode.
 Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
 Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list
(until temp → next is equal to NULL).
 Step 6 - Set temp → next = newNode.

Inserting At Specific location in the list (After a Node)


We can use the following steps to insert a new node after a node in the single linked list...

 Step 1 - Create a newNode with given value.


 Step 2 - Check whether list is Empty (head == NULL)
 Step 3 - If it is Empty then, set newNode → next = NULL and head = newNode.
 Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
 Step 5 - Keep moving the temp to its next node until it reaches to the node after which we
want to insert the newNode (until temp1 → data is equal to location, here location is the
node value after which we want to insert the newNode).
 Step 6 - Every time check whether temp is reached to last node or not. If it is reached to last
node then display 'Given node is not found in the list!!! Insertion not possible!!!' and
terminate the function. Otherwise move the temp to next node.
 Step 7 - Finally, Set 'newNode → next = temp → next' and 'temp → next = newNode'

Deletion
In a single linked list, the deletion operation can be performed in three ways. They are as follows...

1. Deleting from Beginning of the list


2. Deleting from End of the list
3. Deleting a Specific Node

Deleting from Beginning of the list


We can use the following steps to delete a node from beginning of the single linked list...

 Step 1 - Check whether list is Empty (head == NULL)


 Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
 Step 4 - Check whether list is having only one node (temp → next == NULL)
 Step 5 - If it is TRUE then set head = NULL and delete temp (Setting Empty list conditions)
 Step 6 - If it is FALSE then set head = temp → next, and delete temp.

Deleting from End of the list


We can use the following steps to delete a node from end of the single linked list...

 Step 1 - Check whether list is Empty (head == NULL)


 Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize
'temp1' with head.
 Step 4 - Check whether list has only one Node (temp1 → next == NULL)
 Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate the function.
(Setting Empty list condition)
 Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node.
Repeat the same until it reaches to the last node in the list. (until temp1 → next == NULL)
 Step 7 - Finally, Set temp2 → next = NULL and delete temp1.

Deleting a Specific Node from the list


We can use the following steps to delete a specific node from the single linked list...

 Step 1 - Check whether list is Empty (head == NULL)


 Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize
'temp1' with head.
 Step 4 - Keep moving the temp1 until it reaches to the exact node to be deleted or to the last
node. And every time set 'temp2 = temp1' before moving the 'temp1' to its next node.
 Step 5 - If it is reached to the last node then display 'Given node not found in the list!
Deletion not possible!!!'. And terminate the function.
 Step 6 - If it is reached to the exact node which we want to delete, then check whether list is
having only one node or not
 Step 7 - If list has only one node and that is the node to be deleted, then
set head = NULL and delete temp1 (free(temp1)).
 Step 8 - If list contains multiple nodes, then check whether temp1 is the first node in the list
(temp1 == head).
 Step 9 - If temp1 is the first node then move the head to the next node (head = head →
next) and delete temp1.
 Step 10 - If temp1 is not first node then check whether it is last node in the list (temp1 →
next == NULL).
 Step 11 - If temp1 is last node then set temp2 → next = NULL and
delete temp1 (free(temp1)).
 Step 12 - If temp1 is not first node and not last node then set temp2 → next = temp1 →
next and delete temp1 (free(temp1)).

Displaying a Single Linked List


We can use the following steps to display the elements of a single linked list...

 Step 1 - Check whether list is Empty (head == NULL)


 Step 2 - If it is Empty then, display 'List is Empty!!!' and terminate the function.
 Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
 Step 4 - Keep displaying temp → data with an arrow (--->) until temp reaches to the last
node
 Step 5 - Finally display temp → data with arrow pointing to NULL (temp → data --->
NULL).

PROGRAM FOR SINGLE LINKED LIST:

#include<stdio.h>
#include<stdlib.h>

void create(int);
void insertAtBeginning(int);
void insertAtEnd(int);
void insertAfterPosition(int,int);
void display();
void removeBeginning();
void removeEnd();
void removeSpecific(int);

struct Node
{
int data;
struct Node *next;
}*head = NULL;
int value,i=1;
void main()
{
int choice,pos;

while(1){

printf("\n\n****** MENU ******\n1.create\n2. InsertAtBegiining\n3.


insertAtend\n4. insertAftergivenPosition\n5.display\n6.deletefronBegin\
n7.DeleteFromEnd\n8.DeleteAtGivenPosition\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value for the node: ");
scanf("%d",&value);
create(value);
break;
case 2: printf("Enter the value to be insert: ");
scanf("%d",&value);
insertAtBeginning(value);
break;
case 3: printf("Enter the value to be insert: ");
scanf("%d",&value);
insertAtEnd(value);
break;
case 4: printf("Enter the the position to insert an element after
it");
scanf("%d",&pos);
printf("Enter the value for the node");
scanf("%d",&value);
insertAfterPosition(pos,value);
break;

case 5: display();
break;
case 6:
removeBeginning();
break;
case 7:removeEnd();
break;
case 8:printf("Enter the node which you wanto delete: ");
scanf("%d",&pos);
removeSpecific(pos);
break;
default: printf("\nWrong Input!! Try again!!!\n\n");
break;
}
}

}
void create(int value)
{
struct Node *newNode,*temp;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next=0;
if(head==0)
{
head=temp=newNode;
}
else
{
temp->next=newNode;
temp=newNode;

}
}

void insertAtBeginning(int value)


{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(head == NULL)
{
newNode->next = NULL;
head = newNode;
}
else
{
newNode->next = head;
head = newNode;
}
printf("\nOne node inserted!!!\n");
}
void insertAtEnd(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if(head == NULL)
head = newNode;
else
{
struct Node *temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}

printf("\nOne node inserted!!!\n");


}
void insertAfterPosition(int pos,int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(head == NULL)
{
newNode->next = NULL;
head = newNode;
}
else
{
struct Node *temp = head;
while(i<pos){
temp = temp->next;
i++;
}
newNode->next = temp->next;
temp->next = newNode;

}
printf("\nOne node inserted!!!\n");
}

void removeBeginning()
{
if(head == NULL)
printf("\n\nList is Empty!!!");
else
{
struct Node *temp = head;
if(head->next == NULL)
{
head = NULL;
free(temp);
}
else
{
head = temp->next;
free(temp);
printf("\nOne node deleted!!!\n\n");
}
}
}
void removeEnd()
{
struct Node *temp= head,*prevnode;
while(temp->next != 0)
{
prevnode=temp;
temp = temp->next;
}
if(temp==head)
{
head=0;
free(temp);
}
else
{

prevnode->next=0;
free(temp);
}
}
void removeSpecific(int pos)
{
int i=1;
struct Node *temp = head, *nextnode;
while(i<pos-1)
{
temp=temp->next;
i++;
}
nextnode=temp->next;
temp->next=nextnode->next;
free(nextnode);
}
void display()
{
if(head == NULL)
{
printf("\nList is Empty\n");
}
else
{
struct Node *temp = head;
printf("\n\nList elements are - \n");
while(temp->next != NULL)
{
printf("%d",temp->data);
temp = temp->next;
}
printf("%d",temp->data);
}
}

Doubly Linked List:

A doubly linked list is a type of linked list in which each node consists of 3
components:

*prev - address of the previous node


data - data item
*next - address of next node
Here, the single node is represented as

struct node {
int data;
struct node *next;
struct node *prev;
}
Doubly Linked List is a variation of Linked list in which navigation is possible in
both ways, either forward and backward easily as compared to Single Linked List.
Following are the important terms to understand the concept of doubly linked list.

Link − Each link of a linked list can store a data called an element.

Next − Each link of a linked list contains a link to the next link called Next.

Prev − Each link of a linked list contains a link to the previous link called Prev.

LinkedList − A Linked List contains the connection link to the first link called
First and to the last link called Last.

Doubly Linked List Representation


Doubly Linked List
As per the above illustration, following are the important points to be considered.
Doubly Linked List contains a link element called first and last.

Each link carries a data field(s) and two link fields called next and prev.

Each link is linked with its next link using its next link.

Each link is linked with its previous link using its previous link.

The last link carries a link as null to mark the end of the list.

ALGORITHM FOR DOUBLE LINKED LIST:

Operations on Double Linked List


In a double linked list, we perform the following operations...

1. Insertion
2. Deletion
3. Display

Insertion
In a double linked list, the insertion operation can be performed in three ways as follows...

1. Inserting At Beginning of the list


2. Inserting At End of the list
3. Inserting At Specific location in the list

Inserting At Beginning of the list


We can use the following steps to insert a new node at beginning of the double linked list...

 Step 1 - Create a newNode with given value and newNode → previous as NULL.
 Step 2 - Check whether list is Empty (head == NULL)
 Step 3 - If it is Empty then, assign NULL to newNode → next and newNode to head.
 Step 4 - If it is not Empty then, assign head to newNode → next and newNode to head.

Inserting At End of the list


We can use the following steps to insert a new node at end of the double linked list...

 Step 1 - Create a newNode with given value and newNode → next as NULL.
 Step 2 - Check whether list is Empty (head == NULL)
 Step 3 - If it is Empty, then assign NULL to newNode → previous and newNode to head.
 Step 4 - If it is not Empty, then, define a node pointer temp and initialize with head.
 Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list
(until temp → next is equal to NULL).
 Step 6 - Assign newNode to temp → next and temp to newNode → previous.

Inserting At Specific location in the list (After a Node)


We can use the following steps to insert a new node after a node in the double linked list...

 Step 1 - Create a newNode with given value.


 Step 2 - Check whether list is Empty (head == NULL)
 Step 3 - If it is Empty then, assign NULL to both newNode → previous & newNode →
next and set newNode to head.
 Step 4 - If it is not Empty then, define two node pointers temp1 & temp2 and
initialize temp1 with head.
 Step 5 - Keep moving the temp1 to its next node until it reaches to the node after which we
want to insert the newNode (until temp1 → data is equal to location, here location is the
node value after which we want to insert the newNode).
 Step 6 - Every time check whether temp1 is reached to the last node. If it is reached to the
last node then display 'Given node is not found in the list!!! Insertion not
possible!!!' and terminate the function. Otherwise move the temp1 to next node.
 Step 7 - Assign temp1 → next to temp2, newNode to temp1 → next, temp1 to newNode
→ previous, temp2 to newNode → next and newNode to temp2 → previous.

Deletion
In a double linked list, the deletion operation can be performed in three ways as follows...

1. Deleting from Beginning of the list


2. Deleting from End of the list
3. Deleting a Specific Node

Deleting from Beginning of the list


We can use the following steps to delete a node from beginning of the double linked list...

 Step 1 - Check whether list is Empty (head == NULL)


 Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head.
 Step 4 - Check whether list is having only one node (temp → previous is equal to temp →
next)
 Step 5 - If it is TRUE, then set head to NULL and delete temp (Setting Empty list
conditions)
 Step 6 - If it is FALSE, then assign temp → next to head, NULL to head → previous and
delete temp.

Deleting from End of the list


We can use the following steps to delete a node from end of the double linked list...
 Step 1 - Check whether list is Empty (head == NULL)
 Step 2 - If it is Empty, then display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head.
 Step 4 - Check whether list has only one Node (temp → previous and temp → next both
are NULL)
 Step 5 - If it is TRUE, then assign NULL to head and delete temp. And terminate from the
function. (Setting Empty list condition)
 Step 6 - If it is FALSE, then keep moving temp until it reaches to the last node in the list.
(until temp → next is equal to NULL)
 Step 7 - Assign NULL to temp → previous → next and delete temp.

Deleting a Specific Node from the list


We can use the following steps to delete a specific node from the double linked list...

 Step 1 - Check whether list is Empty (head == NULL)


 Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize with head.
 Step 4 - Keep moving the temp until it reaches to the exact node to be deleted or to the last
node.
 Step 5 - If it is reached to the last node, then display 'Given node not found in the list!
Deletion not possible!!!' and terminate the fuction.
 Step 6 - If it is reached to the exact node which we want to delete, then check whether list is
having only one node or not
 Step 7 - If list has only one node and that is the node which is to be deleted then
set head to NULL and delete temp (free(temp)).
 Step 8 - If list contains multiple nodes, then check whether temp is the first node in the list
(temp == head).
 Step 9 - If temp is the first node, then move the head to the next node (head = head →
next), set head of previous to NULL (head → previous = NULL) and delete temp.
 Step 10 - If temp is not the first node, then check whether it is the last node in the list ( temp
→ next == NULL).
 Step 11 - If temp is the last node then set temp of previous of next to NULL (temp →
previous → next = NULL) and delete temp (free(temp)).
 Step 12 - If temp is not the first node and not the last node, then
set temp of previous of next to temp of next (temp → previous → next = temp →
next), temp of next of previous to temp of previous (temp → next → previous = temp →
previous) and delete temp (free(temp)).

Displaying a Double Linked List


We can use the following steps to display the elements of a double linked list...

 Step 1 - Check whether list is Empty (head == NULL)


 Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the function.
 Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize with head.
 Step 4 - Display 'NULL <--- '.
 Step 5 - Keep displaying temp → data with an arrow (<===>) until temp reaches to the last
node
 Step 6 - Finally, display temp → data with arrow pointing to NULL (temp → data --->
NULL).

Program for double linked list:


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void insertAtBeginning(int);
void insertAtEnd(int);
void insertAfter(int,int);
void deleteBeginning();
void deleteEnd();
void deleteSpecific(int);
void display();

struct Node
{
int data;
struct Node *prev, *next;
}*head = NULL;
int value,i=1,pos,count=0;
int choice1;
void main()
{

while(choice1!=8)
{
printf("\n*********** MENU *************\n");
printf("1. InsertAtbeginning\n2. InsertAtEnd\n3.InsertAfterPosition\n4.
DeleteFromBeginning\n 5.DeletefromEnd\n6.DeleteAfterPosition\n7.display\
n8.exit\nEnter your choice: ");
scanf("%d",&choice1);
switch(choice1)
{
case 1:printf("Enter the value to be inserted: ");
scanf("%d",&value);
insertAtBeginning(value);
break;
case 2:printf("Enter the value to be inserted: ");
scanf("%d",&value);
insertAtEnd(value);
break;
case 3:printf("Enter the position after which you want to insert: ");
scanf("%d",&pos);
printf("Enter the value to be inserted: ");
scanf("%d",&value);
insertAfter(value,pos);
break;
case 4: deleteBeginning();
break;
case 5: deleteEnd();
break;
case 6: printf("Enter the position to be deleted: ");
scanf("%d",&pos);
deleteSpecific(pos);
break;
case 7: display();
break;
case 8: printf("exited");
break;
default: printf("\nPlease select correct option!!!");
break;
}
}
}

void insertAtBeginning(int value)


{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode -> data = value;
newNode -> prev = NULL;
if(head == NULL)
{
newNode -> next = NULL;
head = newNode;
}
else
{

head->prev=newNode;
newNode -> next = head;
head = newNode;
}
printf("\nInsertion success!!!");
}
void insertAtEnd(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode -> data = value;
newNode -> next = NULL;
newNode -> prev = NULL;
if(head == NULL)
{

head = newNode;
}
else
{
struct Node *temp = head;
while(temp -> next != NULL)
{
temp = temp -> next;
}
temp -> next = newNode;
newNode -> prev = temp;
temp=newNode;
}
printf("\nInsertion success!!!");
}
void insertAfter(int value, int pos)
{
struct Node *newNode,*nextnode,*temp=head;;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode -> data = value;
if(head == NULL)
{
newNode -> prev = newNode -> next = NULL;
head = newNode;
}
else if(pos<1&&pos>count)
{
printf("invalid position");
}
else if(pos==1)
{
insertAtBeginning(value);
}
else{
while(i<pos)
{
temp=temp->next;
i++;
}
nextnode = temp->next;
temp -> next = newNode;
newNode -> prev = temp;
newNode -> next = nextnode;
nextnode->prev = newNode;
printf("\nInsertion success!!!");
}

}
void deleteBeginning()
{
if(head == NULL)
printf("List is Empty!!! Deletion not possible!!!");
else
{
struct Node *temp = head;
if(temp -> prev == temp -> next)
{
head = NULL;
free(temp);
}
else{
head = temp -> next;
head -> prev = NULL;
free(temp);
}
printf("\nDeletion success!!!");
}
}
void deleteEnd()
{
if(head == NULL)
printf("List is Empty!!! Deletion not possible!!!");
else
{
struct Node *temp = head,*prevnode;
if(temp -> prev == temp -> next)
{
head = NULL;
free(temp);
}
else{
while(temp -> next != 0)
{
prevnode=temp;

temp = temp -> next;


}
temp -> prev =0;
prevnode->next=0;

free(temp);
}
printf("\nDeletion success!!!");
}
}
void deleteSpecific(int pos)
{
struct Node *temp=head;
while(i<pos)
{
temp=temp->next;
i++;
}
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);

void display()
{
if(head == NULL)
printf("\nList is Empty!!!");
else
{
struct Node *temp = head;
printf("\nList elements are: \n");

while(temp->next!= NULL)
{
printf("%d",temp -> data);
temp=temp->next;

}
printf("%d",temp -> data);
}
}

Circular Singly Linked List:

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 Doubly Linked List

Here, in addition to the last node storing the address of the first node, the first node
will also store the address of the last node.

Operations
In a circular linked list, we perform the following operations...

1. Insertion
2. Deletion
3. Display

Before we implement actual operations, first we need to setup empty list. First perform the following
steps before implementing actual operations.

 Step 1 - Include all the header files which are used in the program.
 Step 2 - Declare all the user defined functions.
 Step 3 - Define a Node structure with two members data and next
 Step 4 - Define a Node pointer 'head' and set it to NULL.
 Step 5 - Implement the main method by displaying operations menu and make suitable
function calls in the main method to perform user selected operation.
Insertion
In a circular linked list, the insertion operation can be performed in three ways. They are as follows...

1. Inserting At Beginning of the list


2. Inserting At End of the list
3. Inserting At Specific location in the list

Inserting At Beginning of the list


We can use the following steps to insert a new node at beginning of the circular linked list...

 Step 1 - Create a newNode with given value.


 Step 2 - Check whether list is Empty (head == NULL)
 Step 3 - If it is Empty then, set head = newNode and newNode→next = head .
 Step 4 - If it is Not Empty then, define a Node pointer 'temp' and initialize with 'head'.
 Step 5 - Keep moving the 'temp' to its next node until it reaches to the last node (until 'temp
→ next == head').
 Step 6 - Set 'newNode → next =head', 'head = newNode' and 'temp → next = head'.

Inserting At End of the list


We can use the following steps to insert a new node at end of the circular linked list...

 Step 1 - Create a newNode with given value.


 Step 2 - Check whether list is Empty (head == NULL).
 Step 3 - If it is Empty then, set head = newNode and newNode → next = head.
 Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
 Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list
(until temp → next == head).
 Step 6 - Set temp → next = newNode and newNode → next = head.

Inserting At Specific location in the list (After a Node)


We can use the following steps to insert a new node after a node in the circular linked list...

 Step 1 - Create a newNode with given value.


 Step 2 - Check whether list is Empty (head == NULL)
 Step 3 - If it is Empty then, set head = newNode and newNode → next = head.
 Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
 Step 5 - Keep moving the temp to its next node until it reaches to the node after which we
want to insert the newNode (until temp1 → data is equal to location, here location is the
node value after which we want to insert the newNode).
 Step 6 - Every time check whether temp is reached to the last node or not. If it is reached to
last node then display 'Given node is not found in the list!!! Insertion not
possible!!!' and terminate the function. Otherwise move the temp to next node.
 Step 7 - If temp is reached to the exact node after which we want to insert the newNode
then check whether it is last node (temp → next == head).
 Step 8 - If temp is last node then set temp → next = newNode and newNode →
next = head.
 Step 8 - If temp is not last node then set newNode → next = temp → next and temp →
next = newNode.

Deletion
In a circular linked list, the deletion operation can be performed in three ways those are as follows...

1. Deleting from Beginning of the list


2. Deleting from End of the list
3. Deleting a Specific Node

Deleting from Beginning of the list


We can use the following steps to delete a node from beginning of the circular linked list...

 Step 1 - Check whether list is Empty (head == NULL)


 Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize
both 'temp1' and 'temp2' with head.
 Step 4 - Check whether list is having only one node (temp1 → next == head)
 Step 5 - If it is TRUE then set head = NULL and delete temp1 (Setting Empty list
conditions)
 Step 6 - If it is FALSE move the temp1 until it reaches to the last node. (until temp1 →
next == head )
 Step 7 - Then set head = temp2 → next, temp1 → next = head and delete temp2.

Deleting from End of the list


We can use the following steps to delete a node from end of the circular linked list...

 Step 1 - Check whether list is Empty (head == NULL)


 Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize
'temp1' with head.
 Step 4 - Check whether list has only one Node (temp1 → next == head)
 Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate from the
function. (Setting Empty list condition)
 Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node.
Repeat the same until temp1 reaches to the last node in the list. (until temp1 →
next == head)
 Step 7 - Set temp2 → next = head and delete temp1.

Deleting a Specific Node from the list


We can use the following steps to delete a specific node from the circular linked list...

 Step 1 - Check whether list is Empty (head == NULL)


 Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize
'temp1' with head.
 Step 4 - Keep moving the temp1 until it reaches to the exact node to be deleted or to the last
node. And every time set 'temp2 = temp1' before moving the 'temp1' to its next node.
 Step 5 - If it is reached to the last node then display 'Given node not found in the list!
Deletion not possible!!!'. And terminate the function.
 Step 6 - If it is reached to the exact node which we want to delete, then check whether list is
having only one node (temp1 → next == head)
 Step 7 - If list has only one node and that is the node to be deleted then
set head = NULL and delete temp1 (free(temp1)).
 Step 8 - If list contains multiple nodes then check whether temp1 is the first node in the list
(temp1 == head).
 Step 9 - If temp1 is the first node then set temp2 = head and keep moving temp2 to its next
node until temp2 reaches to the last node. Then set head = head → next, temp2 → next
= head and delete temp1.
 Step 10 - If temp1 is not first node then check whether it is last node in the list (temp1 →
next == head).
 Step 1 1- If temp1 is last node then set temp2 → next = head and
delete temp1 (free(temp1)).
 Step 12 - If temp1 is not first node and not last node then set temp2 → next = temp1 →
next and delete temp1 (free(temp1)).

Displaying a circular Linked List


We can use the following steps to display the elements of a circular linked list...

 Step 1 - Check whether list is Empty (head == NULL)


 Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the function.
 Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
 Step 4 - Keep displaying temp → data with an arrow (--->) until temp reaches to the last
node
 Step 5 - Finally display temp → data with arrow pointing to head → data.
 circular linked list Program:

 Note:
 Here ptr variable is for newnode and preptr is prevnode(as per our discussion in
the class)
 #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!=6)
 {
 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.display\n6.Exit\n");
 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:
 display();
 break;
 case 6:
 printf("exited");
 break;
 default:
 printf("Please enter valid choice..");
 }
 }
 }
 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;
 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;
 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
 { 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)
 {
 preptr=ptr;
 ptr = ptr->next;
 }
 preptr->next = ptr -> next;
 free(ptr);
 printf("\nnode deleted\n");

 }
 }

 void display()
 {
 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);
 }

 }

Stack Operations using Linked List


To implement a stack using a linked list, we need to set the following things before implementing
actual operations.

 Step 1 - Include all the header files which are used in the program. And declare all the user
defined functions.
 Step 2 - Define a 'Node' structure with two members data and next.
 Step 3 - Define a Node pointer 'top' and set it to NULL.
 Step 4 - Implement the main method by displaying Menu with list of operations and make
suitable function calls in the main method.

push(value) - Inserting an element into the Stack


We can use the following steps to insert a new node into the stack...

 Step 1 - Create a newNode with given value.


 Step 2 - Check whether stack is Empty (top == NULL)
 Step 3 - If it is Empty, then set newNode → next = NULL.
 Step 4 - If it is Not Empty, then set newNode → next = top.
 Step 5 - Finally, set top = newNode.

pop() - Deleting an Element from a Stack


We can use the following steps to delete a node from the stack...
 Step 1 - Check whether stack is Empty (top == NULL).
 Step 2 - If it is Empty, then display "Stack is Empty!!! Deletion is not possible!!!" and
terminate the function
 Step 3 - If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.
 Step 4 - Then set 'top = top → next'.
 Step 5 - Finally, delete 'temp'. (free(temp)).

display() - Displaying stack of elements


We can use the following steps to display the elements (nodes) of a stack...

 Step 1 - Check whether stack is Empty (top == NULL).


 Step 2 - If it is Empty, then display 'Stack is Empty!!!' and terminate the function.
 Step 3 - If it is Not Empty, then define a Node pointer 'temp' and initialize with top.
 Step 4 - Display 'temp → data --->' and move it to the next node. Repeat the same
until temp reaches to the first node in the stack. (temp → next != NULL).
 Step 5 - Finally! Display 'temp → data ---> NULL'.

Implementation of Stack using Linked List :

#include<stdio.h>

#include<conio.h>

struct Node

int data;

struct Node *next;

}*top = NULL;

void push(int);

void pop();

void display();

void main()
{

int choice, value;

clrscr();

printf("\n:: Stack using Linked List ::\n");

while(1){

printf("\n****** MENU ******\n");

printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");

printf("Enter your choice: ");

scanf("%d",&choice);

switch(choice){

case 1: printf("Enter the value to be insert: ");

scanf("%d", &value);

push(value);

break;

case 2: pop(); break;

case 3: display(); break;

case 4: exit(0);

default: printf("\nWrong selection!!! Please try again!!!\n");

void push(int value)

struct Node *newNode;

newNode = (struct Node*)malloc(sizeof(struct Node));


newNode->data = value;

if(top == NULL)

newNode->next = NULL;

else

newNode->next = top;

top = newNode;

printf("\nInsertion is Success!!!\n");

void pop()

if(top == NULL)

printf("\nStack is Empty!!!\n");

else{

struct Node *temp = top;

printf("\nDeleted element: %d", temp->data);

top = temp->next;

free(temp);

void display()

if(top == NULL)

printf("\nStack is Empty!!!\n");

else{

struct Node *temp = top;


while(temp->next != NULL){

printf("%d--->",temp->data);

temp = temp -> next;

printf("%d--->NULL",temp->data);

Queue Using Linked List:

The major problem with the queue implemented using an array is, It will work for an only
fixed number of data values. That means, the amount of data must be specified at the
beginning itself. Queue using an array is not suitable when we don't know the size of data
which we are going to use. A queue data structure can be implemented using a linked list
data structure. The queue which is implemented using a linked list can work for an unlimited
number of values. That means, queue using linked list can work for the variable size of data
(No need to fix the size at the beginning of the implementation). The Queue implemented
using linked list can organize as many data values as we want.

In linked list implementation of a queue, the last inserted node is always pointed by 'rear' and
the first node is always pointed by 'front'.

Operations
To implement queue using linked list, we need to set the following things before implementing actual
operations.

 Step 1 - Include all the header files which are used in the program. And declare all the user
defined functions.
 Step 2 - Define a 'Node' structure with two members data and next.
 Step 3 - Define two Node pointers 'front' and 'rear' and set both to NULL.
 Step 4 - Implement the main method by displaying Menu of list of operations and make
suitable function calls in the main method to perform user selected operation.
enQueue(value) - Inserting an element into the Queue
We can use the following steps to insert a new node into the queue...

 Step 1 - Create a newNode with given value and set 'newNode → next' to NULL.
 Step 2 - Check whether queue is Empty (rear == NULL)
 Step 3 - If it is Empty then, set front = newNode and rear = newNode.
 Step 4 - If it is Not Empty then, set rear → next = newNode and rear = newNode.

deQueue() - Deleting an Element from Queue


We can use the following steps to delete a node from the queue...

 Step 1 - Check whether queue is Empty (front == NULL).


 Step 2 - If it is Empty, then display "Queue is Empty!!! Deletion is not possible!!!" and
terminate from the function
 Step 3 - If it is Not Empty then, define a Node pointer 'temp' and set it to 'front'.
 Step 4 - Then set 'front = front → next' and delete 'temp' (free(temp)).

display() - Displaying the elements of Queue


We can use the following steps to display the elements (nodes) of a queue...

 Step 1 - Check whether queue is Empty (front == NULL).


 Step 2 - If it is Empty then, display 'Queue is Empty!!!' and terminate the function.
 Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with front.
 Step 4 - Display 'temp → data --->' and move it to the next node. Repeat the same until
'temp' reaches to 'rear' (temp → next != NULL).
 Step 5 - Finally! Display 'temp → data ---> NULL'.

Implementation of Queue Datastructure using


Linked List
#include<stdio.h>
#include<conio.h>

struct Node
{
int data;
struct Node *next;
}*front = NULL,*rear = NULL;

void insert(int);
void delete();
void display();

void main()
{
int choice, value;
clrscr();
printf("\n:: Queue Implementation using Linked
List ::\n");
while(1){
printf("\n****** MENU ******\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\
n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
insert(value);
break;
case 2: delete(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please
try again!!!\n");
}
}
}
void insert(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct
Node));
newNode->data = value;
newNode -> next = NULL;
if(front == NULL)
front = rear = newNode;
else{
rear -> next = newNode;
rear = newNode;
}
printf("\nInsertion is Success!!!\n");
}
void delete()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
struct Node *temp = front;
front = front -> next;
printf("\nDeleted element: %d\n", temp->data);
free(temp);
}
}
void display()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
struct Node *temp = front;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL\n",temp->data);
}
}

You might also like