Monday, 11 December 2017

Write a C programs to implement a double ended queue ADT using doubly linked list.

Implementation of above problem:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *previous;
struct node *next;
};
struct node *front, *rear;
int count;
void display();
void insert_begin(int x);
void insert_last(int x);
int delete_begin();
int delete_last();
// definition of the main function with different function call
int main()
{
int ch, ele;
printf("\n1. Insert-begin");
printf("\n2. Insert-last");
printf("\n3. Delete-begin");
printf("\n4. Delete-last");
printf("\n5. Display");
printf("\n6.exit");
while(1)
{
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter value for insertion :");
scanf("%d",&ele);
insert_begin(ele);
break;
case 2: printf(" Enter the value for insertion:");
scanf("%d",&ele);
insert_last(ele);
break;
case 3: ele = delete_begin();
if(ele!=-1)
printf("%d is deleted .",ele);
break;
case 4: ele = delete_last();
if(ele!=-1)
printf("%d is deleted .",ele);
break;
case 5: display();
break;
case 6: exit(0);
}
}
}
// definition of display function
void display()
{
struct node * ptr;
ptr = front;
if(front==NULL || rear==NULL)
{
printf("List is empty");
return;
}
while(ptr != NULL)
{
printf( "%d -> ",ptr ->data);
ptr = ptr->next;
}
printf("\n");
}
// definition of insert_begin function
void insert_begin(int x)
{
struct node *new1;
new1 = (struct node*)malloc(sizeof(struct node));
new1 -> data =x;
new1 ->previous = new1 ->next =NULL;
if(front == NULL||rear==NULL)
front = rear = new1;
else
{
new1 ->next = front;
front ->previous = new1;
front = new1;
}
}
// definition of insert_last function
void insert_last(int x)
{
struct node *new1;
new1 = (struct node*)malloc(sizeof(struct node));
new1 ->data = x;
new1 -> previous = new1 ->next = NULL;
if (front == NULL||rear==NULL)
front = rear = new1;
else
{
rear ->next = new1;
new1 ->previous = rear;
rear = new1;
}
}
// definition of delete_begin function
int delete_begin()
{
int x;
struct node *temp;
if (front == NULL || rear==NULL)
{
printf( " LIST IS EMPTY ");
return -1;
}
else
{
temp = front;
x= temp->data;
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
{
front = front->next;
front->previous = NULL;
}
count --;
free(temp);
return x;
}
}
// definition of delete_last function
int delete_last( ) 
{
int x;
struct node *temp;
if(rear == NULL || front==NULL)
{
printf( " LIST IS EMPTY ");
return -1;
}
else
{
temp = rear;
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
{
rear = rear->previous;
rear -> next = NULL;
}
x= temp ->data;
free(temp);
count --;
return x;
}
}


Thanks
Mukesh Rajput

No comments:

Post a Comment

Thanks
Mukesh Rajput