You are on page 1of 24

EXP NO:

DATE:

AIM:
To write a c program to perform implementation of doubly linked list.

OBJECTIVE:
The objective of this program is to design and demonstrate proficiency in using a doubly linked lists.
This also makes the programmer familiar with operations like insertion and deletion and also searching and
traversing. It gives importance to edge conditions and also on memory utilization.

ALGORITHM:
1. Create a struct node with integer data and a pointer to the next node and the previous node.

2. Declare a global variable `head` to keep track of the linked list.

3. Implement the following functions:

4.Insertion at the beginning

 Create a ptr with given value and ptr → previous as NULL.


 Check whether list is Empty (head == NULL)
 If it is Empty then, assign NULL to ptr → next and ptr to head.
 If it is not Empty then, assign head to ptr → next and ptr to head.

5.Insertion at the end

 Create a ptr with given value and ptr → next as NULL.


 Check whether list is Empty (head == NULL)
 If it is Empty, then assign NULL to ptr → previous and ptr to head.
 If it is not Empty, then, define a node pointer temp and initialize with head.
 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)
 Assign ptr to temp → next and temp to ptr → previous

5.Insertion at any intermediate position

 Create a ptr with given value.


 Define a node pointers temp and initialize temp with head.
 Keep moving the temp to its next node until it reaches to the node after which we want to insert the
ptr.
 Every time check whether temp is reached to the last node. If it is reached to the last node then
display there are less elements and terminate the function.
 Else set the next of temp to ptr, previous of ptr to temp and also set next of temp to next of ptr.
 Finally set ptr to the previous of next of temp

6.Deletion at the beginning

 Check whether list is Empty (head == NULL)


 If it is Empty then, display underflow.
 If it is not Empty then, define a Node pointer 'ptr' and initialize with head.
 Check whether list is having only one node.
 If it is TRUE, then set head to NULL and delete ptr.
 If it is FALSE, then assign ptr→ next to head, NULL to head → previous and delete ptr.

7.Deletion at last

 Check whether list is Empty (head == NULL)


 If it is Empty, then display underflow.
 If it is not Empty then, define a Node pointer ptr and initialize with head.
 Check whether list has only one Node
 If it is TRUE, then assign NULL to head and delete ptr.
 If it is FALSE, then keep moving temp until it reaches to the last node in the list.
 Assign NULL to ptr→ previous → next and delete ptr.

8.Deletion at random position

 Define a Node pointer 'ptr' and initialize with head.


 Keep moving the temp until it reaches to the exact node to be deleted or to the last node.
 If it is reached to the exact node which we want to delete, then check whether list is having only one
node or not
 If list has only one node and that is the node which is to be deleted then set head to NULL and delete
ptr.
 Else set next of ptr to temp. Set next of temp to next of ptr and previous of next of temp to ptr.
 Delete temp

9.Search

 Define ptr=head.
 If ptr is NULL then it is an empty list .
 Else enter the value and traverse till end.
 If data of ptr is the item to be searched item is found else not found.

10. Display

 Define a Node pointer 'ptr' and initialize with head.


 Traverse ptr till it reaches the end.
 Keep displaying ptr → data
PROGRAM:
#include<stdio.h>

#include<stdlib.h>

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\n5.Delete from last\n6.Delete the node after the given data\n7.Show\n8.Search\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:

display();

break;

case 8:

search();

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);

if(head==NULL)

ptr->next = NULL;

ptr->prev=NULL;

ptr->data=item;

head=ptr;

else

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;

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");

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");

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);

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)

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;

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");

RESULT:
Thus, the c program for implementation of doubly linked list was executed and the output was verified.
FLOW CHART:

You might also like