Unit II Notes Ds
Unit II Notes Ds
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.
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.
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.
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.
struct node {
int data;
struct node *next;
};
When temp is NULL, we know that we have reached the end of the linked list so
we get out of the while loop.
Store data
Store data
temp->next = newNode;
head = head->next;
temp->next = temp->next->next;
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;
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...
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.
Deletion
In a single linked list, the deletion operation can be performed in three ways. They are as follows...
#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){
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;
}
}
}
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);
}
}
A doubly linked list is a type of linked list in which each node consists of 3
components:
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.
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.
1. Insertion
2. Deletion
3. Display
Insertion
In a double linked list, the insertion operation can be performed in three ways as follows...
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.
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.
Deletion
In a double linked list, the deletion operation can be performed in three ways as follows...
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;
}
}
}
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;
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);
}
}
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.
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...
Deletion
In a circular linked list, the deletion operation can be performed in three ways those are as follows...
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.
#include<stdio.h>
#include<conio.h>
struct Node
int data;
}*top = NULL;
void push(int);
void pop();
void display();
void main()
{
clrscr();
while(1){
scanf("%d",&choice);
switch(choice){
scanf("%d", &value);
push(value);
break;
case 4: exit(0);
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{
top = temp->next;
free(temp);
void display()
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
printf("%d--->",temp->data);
printf("%d--->NULL",temp->data);
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.
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);
}
}