Batch - B Practical Test Question

Batch - B Practical Test Question
Batch - B Practical Test Question

Double Ended Queue Implementation using Linked List

#include<stdio.h>
#include<memory.h>
#include<stdlib.h>

//Data Structure Declaration
struct Element
{
 int value;
 struct Element *next;
};

struct Element *FRONT=NULL,*REAR=NULL,*temp=NULL,*head=NULL; 


int SIZE=5,COUNT=0;

//Function Declaration
void enqueue_front(int);
void enqueue_rear(int);

void dequeue_front();
void dequeue_rear();

void displayQueue();
int countElement();

//Main Function
int main()
{
 int choice,value;
 //You are free to create more variable, if needed
 do
 {
  system("cls"); // To clear the screen
  printMenu();
  printf("\nEnter Your Choice : ");
  scanf("%d",&choice);
  
  switch(choice)
  {
   case 1:
    if(COUNT<SIZE)
    {
    printf("\n Enter Value for Element : ");
    scanf("%d",&value);
    enqueue_front(value);
COUNT++; 
}
else
{
printf("\n Queue is Overflow");
}
        
    break;
    
   case 2:
            if(COUNT>0)
    {
    dequeue_front();
COUNT--; 
}
else
{
printf("\n Queue is Underflow");
}
    break;
    
    case 3:
            if(COUNT<SIZE)
    {
    printf("\n Enter Value for Element : ");
    scanf("%d",&value);
    enqueue_rear(value);
COUNT++; 
}
else
{
printf("\n Queue is Overflow");
}
    break;
    
    case 4:
            if(COUNT>0)
    {
    dequeue_rear();
COUNT--; 
}
else
{
printf("\n Queue is Underflow");
}
    break;
    
   case 5:
    if(COUNT>0)
    {
    displayQueue();
}
else
{
printf("\n Queue is Empty");
}
   
    break;
    
   case 6:
    printf("\n No of Element in Queue : %d",countElement());
    break;
    
  
   default:
    printf("Thanks for Using Queue Program");
  }
  
  getch(); //To stop output on Screen
  
 }while(choice>0 && choice<7);
 return 0;
}


//Function Implementation
void printMenu()
{
 printf("\n==============================");
 printf("\n    QUEUE OPERATION MENU");
 printf("\n==============================");
 printf("\n     1 - ENQUEUE from FRONT");
 printf("\n     2 - DEQUEUE from FRONT");
 printf("\n     3 - ENQUEUE from REAR");
 printf("\n     4 - DEQUEUE from REAR");
 printf("\n     5 - DISPLAY");
 printf("\n     6 - SIZE");
 printf("\n     7 - EXIT");
 printf("\n==============================");
}

void enqueue_rear(int value)
{
temp=(struct Element *)malloc(sizeof(struct Element)*1);
temp->value=value;
temp->next=NULL;

if(FRONT==NULL)
{
FRONT=temp;
REAR =temp;
}
else
{
REAR->next=temp;
REAR=temp;
}
}

void enqueue_front(int value)
{
temp=(struct Element *)malloc(sizeof(struct Element)*1);
temp->value=value;
temp->next=NULL;

if(FRONT==NULL)
{
FRONT=temp;
REAR =temp;
}
else
{
temp->next=FRONT;
FRONT=temp;
}
}

void dequeue_front()
{
temp=FRONT;
FRONT=FRONT->next;
free(temp);
}

void dequeue_rear()
{
if(FRONT->next==NULL)
{
temp=FRONT;
free(temp);
FRONT=NULL;
REAR=NULL;
}
else
{
head=FRONT;
while(head->next!=REAR)
{
head=head->next;
}
temp=head->next;
REAR=head;
head->next=NULL;
free(temp);
}
}

void displayQueue()
{
head=FRONT;
while(head!=NULL)
{
printf(" %d ->",head->value);
head=head->next;
}
}

int countElement()
{
return COUNT;
}

Comments