Thursday, 16 November 2017

Double ended queue using linklist

#include<stdio.h>
#include<stdlib.h>
struct node
{
 int info;
 struct node *next;
}*front,*temp,*rear,*ptr;
void insFront()
{
 temp=(struct node*)malloc(sizeof(struct node));
 printf("\nEnter element: ");
 scanf("%d",&temp->info);
 if(front==NULL && rear==NULL)
 {
  front=rear=temp;
  temp->next=NULL;
  return;
 }
 temp->next=front;
 front=temp;
}
void insRear()
{
 temp=(struct node*)malloc(sizeof(struct node));
 printf("\nEnter element: ");
 scanf("%d",&temp->info);
 temp->next=NULL;
 if(front==NULL && rear==NULL)
 {
  front=rear=temp;
  return;
 }
 rear->next=temp;
 rear=rear->next;
}
void delFront()
{
 if(front==NULL)
 {
  printf("\nEmpty Dequeue\n");
  return;
 }
 if(front==rear && front!=NULL)
 {
  ptr=front;
  front=rear=NULL;
  free(ptr);
 }
 else
 {
  ptr=front;
  front=front->next;
  free(ptr);
 }
}

void delRear()
{
 if(rear==front && rear==NULL)
 {
  printf("Empty DEQUE");
  return;
 }
 else if(rear==front)
 {
  rear=front=NULL;
  free(rear);
  free(front);
 }
 else
 {
  ptr=front;
  while(ptr->next!=NULL)
  {
   temp=ptr;
   ptr=ptr->next;
  }
  temp->next=NULL;
  free(ptr);
  rear=temp;
 }
}
void display()
{
 if(front==NULL && rear==NULL)
 {
  printf("\nEmpty Dequeue\n");
  return;
 }
 ptr=front;
 while(ptr!=NULL)
 {
  printf("%d  ",ptr->info);
  ptr=ptr->next;
 }
}
int  main()
{
 int ch;

 while(1)
 {

  printf("\n1. Insert at rear");
  printf("\n2. Insert at Front");
  printf("\n3. Delete from front");
  printf("\n4. Delete from rear");
  printf("\n5. Display");
  printf("\n6. Exit");
  printf("\nEnter your choice:\n");
  scanf("%d",&ch);
  switch (ch)
  {
   case 1: insRear(); break;
   case 2: insFront(); break;
   case 3: delFront(); break;
   case 4: delRear(); break;
   case 5: display(); break;
   case 6: exit(0); break;
   default: printf("Invalid option");
  }
 }
}

All In One Blog

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

0 comments:

Post a Comment

 

Copyright @ 2015