Wednesday, August 13, 2014

write a program to perform dqueue using link list

//write a program to perform insertion,deletion and display operation on dqueue using link list
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<malloc.h>
typedef struct dnode{
int data;
struct dnode *next;
}dnode;
dnode *front=NULL,*rear=NULL;
void insertf(int x){
dnode *temp;
temp=(dnode*)malloc(sizeof(dnode));
temp->data=x;
temp->next=NULL;
   if(front==NULL){
    front=rear=temp;
   }
   else{
      rear->next=temp;
     rear=temp;
 }
}
void insertr(int x){
dnode *temp,*temp1;
temp=(dnode*)malloc(sizeof(dnode));
temp->data=x;
temp->next=NULL;
if(rear==NULL){
front=rear=temp;
}
else{
temp1=front;
   while(temp1->next!=NULL)
temp1=temp1->next;
temp1->next=temp;
}
}
void delr(){
dnode *temp;
int i;
if(rear==NULL)
{
printf("\nunderflow");
return;
}
temp=front;
while(temp->next->next!=NULL)
temp=temp->next;
dnode *t=temp->next;
printf("\nDeleted node is %d\n",temp->next->data);
temp->next=NULL;
free(t);
rear=temp;
}
void delf(){
dnode *temp;
if(front==NULL)
{
printf("\nunderflow");
return;
}
temp=front;
front=front->next;
printf("\nDeleted node is %d\n",temp->data);
free(temp);
}
void display(){
dnode *temp;
    temp=front;
    printf("\n");
    while(temp!=NULL)
   {
    printf("\t%d",temp->data);
    temp=temp->next;
   
  }
  printf("\n");
}
int main(){
int n,ch;
while(1){
printf("1.insert at front \n2.insert at rear \n3.delete at rear \n4.delete at front \n5.display \n6.exit");
printf("\n enter y choice ");
scanf("%d",&ch);
switch(ch){
case 1:
printf("\n enter the data ");
scanf("%d",&n);
insertf(n);
break;
   case 2:
      printf("\n enter the data ");
scanf("%d",&n);
insertr(n);
break;
  case 3:
      delr();
  break;
  case 4:
     delf();
 break;
 case 5:
  display();
  break;
case 6:
exit(0);
break;
default:
printf("check y option ");
}
  }
  getch();
  return 0;
}

No comments: