Data Structures: Arrays vs Linked Lists
Data Structures: Arrays vs Linked Lists
Array elements are independent of each other. Linked list elements are dependent on each
other. As each node contains the address of
the next node so to access the next node, we
need to access its previous node.
Array takes more time while performing any Linked list takes less time while performing
operation like insertion, deletion, etc. any operation like insertion, deletion, etc.
Accessing any element in an array is faster as Accessing an element in a linked list is
the element in an array can be directly slower as it starts traversing from the first
accessed through the index. element of the linked list.
Memory utilization is inefficient in the array. Memory utilization is efficient in the case of
For example, if the size of the array is 6, and a linked list as the memory can be allocated
array consists of 3 elements only then the rest or deallocated at the run time according to
of the space will be unused. our requirement.
struct node
{
int data;
struct node *next;
};
The above structure declaration defines a new data type called node that represents a
linked list node. The node structure contains two members, data for storing integer
data values and next for storing address of the next node.
The statement, struct node *next, indicates that the pointer next points at same
structure type i.e. node.
2. Linked List Operations
The typical operations performed on a linked list are:
1. Insert
2. Deletion
3. Search
4. Print
1. Insert: 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.
1.1 Insertion at beginning
1.2 Insert at the end of the list
1.3 Insertion after specified node
Store data.
Algorithm
ptr = GetNode(node)
If(ptr = NULL) then
Print"OVERFLOW”
Else
Input item
ptr->data = item
ptr->next = head
head = ptr
Print"Node inserted"
EndIf
Stop
1.2 Insertion at end of the list
The steps to insert a node at beginning is given below
Store data
Algorithm
ptr = GetNode(node)
If(ptr = NULL) then
Print"OVERFLOW”
Else
Input item
ptr->data = item
Input loc
temp=head
For i=1 to loc-1 do
temp = temp->next
If(temp = NULL) then
Print "can't insert”
Return
EndIf
EndFor
ptr ->next = temp ->next
temp ->next = ptr
Print “Node inserted"
EndIf
Stop
2.Delete
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.
Algorithm
Store second last node address in ptr1 and address of last node in
ptr
Change next of ptr1 to NULL
Free(ptr)
Algorithm
If (head= NULL) then
Print “Empty List”
Else
If(head->next=NULL) then
ReturnNode (head)
head=NULL
Print “Only node of the list deleted”
Else
ptr = head
While (ptr -> next!= NULL) do
ptr1 = ptr
ptr = ptr -> next
EndWhile
ptr1 -> next = NULL
ReturnNode (ptr)
Print “Deleted node from last”
EndIf
EndIf
Stop
2.3 Deletion specified node-
Traverse till the node that need to be deleted. Such that at each
movement the address of the previous and current node is stored in
separate pointers.
Change next pointer of the previous node (previous to the specified
node) to hold the address of next node of the specified node to
exclude the specified node from the chain.
Algorithm
Input location
ptr = head
For i = 1 to loc-1 do
ptr1 = ptr
ptr= ptr->next;
If(ptr= NULL) then
Print “Cant delete”
Return
EndIf
EndFor
ptr1->Next= ptr->next
ReturnNode(ptr)
Print "Deleted node",loc
Stop
3. Search
You can search an element on a linked list using a loop using the following steps. We
are finding item on a linked list.
Copy head to temp, initialize flag =0 and i =1
Run a loop until the temp is NULL because the last element points
to NULL.
Diagram for search not found is given below for the link list
Another Example is given below
Algorithm:
i=1
flag =0
temp = head
If (temp= NULL) then
` Print “List is Empty”
Else
Input item
While (temp != NULL) do
If (temp → data = item) then
print “Item found at location”,i
flag =1
Break
EndIf
i=i+1
temp= temp->next
EndWhile
If (flag=0) then
Print “Element not found”
EndIf
EndIf
Stop
4.Print
The print operation prints or displays the linked list elements on the screen. To print
the elements, the linked list is traversed from start till end using NEXT pointers.
Algorithm
ptr = head
If (ptr= NULL) then
Print “Nothing to print”
Else
Print "\nprinting values . . . . .”
While (ptr!= NULL) do
Print ptr→ data
ptr = ptr → next
EndWhile
EndIf
Stop
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
}
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;
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)
{
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=1;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\n can't insert\n");
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
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 beginning ...\n");
}
}
void last_delete()
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("\n list is empty");
}
else if(head -> next == NULL)
{
free(head);
head = NULL;
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()
{
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node which you want to delete \n");
scanf("%d",&loc);
ptr=head;
for(i=1;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);
}
void search()
{
struct node *ptr;
int item,i=1,flag=0;
temp = head;
if(temp == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (temp!=NULL)
{
if(temp->data == item)
{
printf("item found at location %d ",i);
flag=1;
break;
}
i++;
temp = temp -> next;
}
if(flag==0)
{
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)
{
printf("\n %d",ptr->data);
ptr = ptr -> next;
}
}
}
struct node
{
int data;
struct node *next;
};
The above structure declaration defines a new data type called node that represents a
Circular linked list node. The node structure contains two members, data for storing
integer data values and next for storing address of the next node.
The statement, struct node *next, indicates that the pointer next points at same
structure type i.e. node.
Store data.
Algorithm
ptr = GetNode(node)
If(ptr = NULL) then
Print"OVERFLOW”
Else
Input item
ptr->data = item
If(head = NULL) then
head = ptr
ptr -> next = head
Else
temp = head;
While(temp->next != head) do
temp = temp->next
EndWhile
ptr->next = head
temp -> next = ptr
head = ptr
EndIf
Print “Node inserted”
EndIf
Stop
Traverse to last node and store the address of new node in the next
part of last node.
Store the first node address into the next part of new node.
Algorithm
ptr = GetNode(node)
If(ptr = NULL) then
Print"OVERFLOW”
Else
Input item
ptr->data = item
If(head = NULL) then
head = ptr
ptr -> next = head
Else
temp = head
While (temp -> next !=head) do
temp = temp -> next
EndWhile
temp->next = ptr
ptr->next = head
EndIf
Print “Node inserted”
EndIf
Stop
temp = temp->next;
If(temp = NULL) then
Print " can't insert”
Return
EndIf
EndFor
ptr ->next = temp ->next
temp ->next = ptr
Print “Node inserted"
EndIf
Stop
2. Deletion
The Deletion of a node from a Circular 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.
2.1 Delete at beginning
2.2 Deletion at the end of the list
2.3 Deletion specified node
In order to delete a node in circular linked list, we need to make a few pointer
adjustments.
There are three scenarios of deleting a node from circular singly linked list at
beginning.
Scenario 1: (The list is Empty)
If the list is empty then the condition head == NULL will become true, in this case,
we just need to print “Empty list” on the screen and make exit.
Scenario 2: (The list contains single node)
If the list contains single node then, the condition head → next == head will become
true. In this case, we need to do make the head pointer free and head=NULL.
Scenario 3: (The list contains more than one node)
If the list contains more than one node then, in that case, we need to traverse the list
by using the pointer ptr to reach the last node of the list.
At the end of the loop, the pointer ptr point to the last node of the list. Since, the last
node of the list points to the head node of the list. Therefore this will be changed as
now, the last node of the list will point to the next of the head node.
Now, free the head pointer by using the free() method
Make the node pointed by the next of the last node, the new head of the list.
Algorithm
If (head = NULL) then
Print “Empty List”
Else
If(head->next == head) then
ReturnNode(head)
head = NULL;
Print "node deleted”
Else
ptr = head;
While (ptr -> next != head) do
ptr = ptr -> next
EndWhile
ptr->next = head->next
ReturnNode(head)
head = ptr->next
print "node deleted”
EndIf
EndIf
Stop
2.2 Deletion at End
Removing the node from circular singly linked list at the end.
There are three scenarios of deleting a node in circular singly linked list at the end.
Scenario 1 (the list is empty)
If the list is empty then the condition head == NULL will become true, in this case,
we just need to print underflow on the screen and make exit.
Scenario 2(the list contains single element)
If the list contains single node then, the condition head → next == head will become
true. In this case, we need to make the head pointer free and do head=NULL.
Scenario 3(the list contains more than one element)
If the list contains more than one element, then in order to delete the last element, we
need to reach the last node. We also need to keep track of the second last node of the
list. For this purpose, the two pointers ptr and preptr are defined.
now, we need to make just one more pointer adjustment. We need to make the next
pointer of preptr point to the next of ptr (i.e. head) and then make pointer ptr free.
Algorithm
If (head= NULL) then
Print “UNDERFLOW”
Else
If(head->next=head) then
ReturnNode(head)
Head=NULL
Else
ptr = head
While (ptr -> next!=head) do
preptr = ptr
ptr = ptr -> next
EndWhile
preptr -> next = ptr->next
ReturnNode (ptr)
Print “Node Deleted”
Endif
EndIf
Stop
2.3 Delete Specific Node
Traverse till the node that need to be deleted. Such that at each movement the
address of the previous and current node is stored in separate pointers.
Change next pointer of the previous node (previous to the specified node) to
hold the address of next node of the specified node (exclude the specified node
from the chain).
Algorithm
Input loc
ptr = head
If(loc=1)
begin_delete()
Return
EndIf
For i = 1 to loc-1 do
ptr1 = ptr
ptr= ptr->next;
If(ptr= NULL) then
Print “Cant delete”
Return
EndIf
EndFor
ptr1->Next= ptr->next;
ReturnNode(ptr)
Print “Deleted node”,loc
Stop
3. Searching
You can search an element on a linked list using a loop using the following steps. We
are finding item on a Circular linked list.
Copy head to ptr, initialize flag =0 and i =1
void randomInsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
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=1;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\n can't insert\n");
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
}
}
void random_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node which you want to delete \n");
scanf("%d",&loc);
ptr=head;
if(ptr == NULL)
{
printf("\nCan't delete");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc);
}
}
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)
{
free(head);
head = NULL;
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)
{
free(head);
head = NULL;
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 search()
{
struct node *ptr;
int item,i=1,flag=0;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
do
{
if(ptr->data == item)
{
printf("item found at location %d ",i);
flag=1;
break;
}
i++;
ptr = ptr -> next;
} while (ptr != head);
}
if(flag == 0)
{
printf("Item not found\n");
}
}
void display()
{
struct node *ptr;
ptr=head;
if(head == NULL)
{
printf("\nnothing to print");
}
else
{
printf("\n printing values ... \n");
do {
}
Applications of Circular Linked Lists:
2. Operations
The typical operations performed on a linked list are:
1.Insert
2.Deletion
3.Search
4. Print
1. The insertion into a doubly 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.
1.1 Insertion at beginning
1.2 Insert at the end of the list
1.3 Insertion after specified node
As in doubly linked list, each node of the list contain double pointers therefore we
have to maintain more number of pointers in doubly linked list as compare to singly
linked list.
There are two scenarios of inserting any element into doubly linked list. Either the list
is empty or it contains at least one element. Perform the following steps to insert a
node in doubly linked list at beginning.
Firstly, allocate a new node (say new_node).
Now put the required data in the new node.
Make the next of new_node point to the current head of the doubly linked list.
Make the previous of the current head point to new_node.
Lastly, point head to new_node.
Algorithm :
ptr = GetNode(node)
If(ptr = NULL) then
Print"OVERFLOW”
Else
Input item
ptr->data = item
ptr->prev =NULL
If(head=NULL) then
ptr->next = NULL
Else
ptr->next = head
head->prev = ptr
EndIf
head=ptr
Print"Node inserted"
EndIf
Stop
In order to insert a node in doubly linked list at the end, we must make sure whether
the list is empty or it contains any element.
Allocate the memory for the new node. Make the pointer ptr point to the new node
being inserted.
Check whether the list is empty or not. The list is empty if the condition head ==
NULL holds. In that case, the node will be inserted as the only node of the list and
therefore the prev and the next pointer of the node will point to NULL and the head
pointer will point to this node.
In the second scenario, the condition head == NULL become false. The new node will
be inserted as the last node of the list. For this purpose, we have to traverse the whole
list in order to reach the last node of the list. Initialize the pointer temp to head and
traverse the list by using this pointer.
the pointer temp point to the last node at the end of this while loop. Now, we just need
to make a few pointer adjustments to insert the new node ptr to the list. First, make
the next pointer of temp point to the new node being inserted i.e. ptr.
make the previous pointer of the node ptr point to the existing last node of the list i.e.
temp.
make the next pointer of the node ptr point to the NULL as it will be the new last
node of the list.
Algorithm
ptr = GetNode(node)
If(ptr = NULL) then
Print"OVERFLOW”
Else
Input item
ptr->data = item
If(head = NULL) then
ptr -> next = NULL
ptr ->prev = NULL
head = ptr
Else
temp = head
While (temp -> next != NULL) do
temp = temp -> next
EndWhile
temp->next = ptr;
ptr ->prev=temp
ptr->next = NULL;
EndIf
Print “Node inserted”
EndIf
Stop
1.3 Insertion after Specified Node
In order to insert a node after the specified node in the list, we need to skip the
required number of nodes in order to reach the mentioned node and then make the
pointer adjustments as required.
Allocate the memory for the new node. Use the following statements for this.
Traverse the list by using the pointer temp to skip the required number of nodes in
order to reach the specified node.
The temp would point to the specified node at the end of the for loop. The new node
needs to be inserted after this node therefore we need to make a ptr pointer
adjustments here. Make the next pointer of ptr point to the next node of temp.
make the prev of the new node ptr point to temp.
make the next pointer of temp point to the new node ptr.
make the previous pointer of the next node of temp point to the new node.
Algorithm:
ptr = GetNode(node)
If(ptr = NULL) then
Print"OVERFLOW”
Else
Input loc
temp=head
For i=1 to loc-1 do
temp = temp->next;
If(temp = NULL) then
Print " There are less than “,loc,”Elements”
Return
EndIf
EndFor
Input item
ptr->data = item
ptr ->next = temp ->next
ptr ->prev=temp;
temp ->next = ptr
temp->next->prev=ptr;
Print “Node inserted"
EndIf
Stop
2.Delete
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.
In order to delete the last node of the list, we need to follow the following steps.
If the list is already empty then the condition head == NULL will become true and
therefore the operation can not be carried on.
If there is only one node in the list then the condition head → next == NULL become
true. In this case, we just need to free head and assign the head of the list to NULL in
order to completely delete the list.
Otherwise, just traverse the list to reach the last node of the list. This will be done by
using the following statements.
The ptr would point to the last node of the list at the end of the loop. Just make the
next pointer of the previous node of ptr to NULL.
Free the pointer as this the node which is to be deleted.
Algorithm:
If (head= NULL) then
Print “Empty List”
Else
If(head->next=NULL) then
free(head)
Head=NULL
Print “Node deleted”
Else
ptr = head
While (ptr -> next!= NULL) do
ptr = ptr -> next
EndWhile
ptr -> prev->next = NULL
ReturnNode (ptr)
Print “Node deleted”
Endif
EndIf
Stop
2.3 Deletion the Specified Node
3.Searching
You can search an element on a doubly linked list using a loop using the following
steps. We are finding item on a doubly linked list.
Run a loop until the ptr is NULL because the last element points
to NULL.
Algorithm
ptr = head
If (head= NULL) then
Print “Nothing to print”
Else
Print “Printing values”
While (ptr!= NULL) do
Print ptr→ data
ptr = ptr → next
EndWhile
EndIf
Stop
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *prev;
int data;
struct node *next;
};
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\n5.Delete from last\n6.Delete specifies node\n7.Search\
n8.Show\n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
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;
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);
ptr->data=item;
ptr->prev=NULL;
if(head==NULL)
{
ptr->next = NULL;
}
else
{
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;
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
{
temp=head;
printf("Enter the location after which u want to insert");
scanf("%d",&loc);
for(i=1;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");
}
else if(head->next == NULL)
{
free(head);
head = NULL;
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)
{
free(head);
head = NULL;
printf("\nnode deleted\n");
}
else
{
ptr = head;
while(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_specified()
{
struct node *ptr, *temp;
int loc,i;
printf("\n Enter the node number that you want to delete");
scanf("%d", &loc);
ptr = head;
if(loc==1) {deletion_beginning();return;}
for(i=1;i<loc-1;i++)
ptr = ptr -> next;
if(ptr->next== NULL)
{
printf("\nCan't delete\n");
}
else
{
temp = ptr -> next;
temp -> prev->next = temp -> next;
temp -> next -> prev = temp->prev;
free(temp);
printf("\nnode deleted\n");
}
}
void display()
{
struct node *ptr;
ptr = head;
if(head==NULL)
printf(“Nothing to print”);
else{
printf("\n printing values...\n");
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
}
void search()
{
struct node *ptr;
int item,i=1,flag=0;
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("\nitem found at location %d ",i);
flag=1;
break;
}
i++;
ptr = ptr -> next;
}
if(flag==0)
{
printf("\nItem not found\n");
}
}
Open