News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

Implentation of queue using arrays - QUEUE.C

Started by aruljothi, Mar 31, 2009, 10:59 AM

Previous topic - Next topic

aruljothi

Code :
/* Implentation of queue using arrays - QUEUE.C */
# include <stdio.h>
# include <conio.h>
# define SIZE 10
int arr[SIZE], front = -1, rear = -1, i ;
void enqueue() ;
void dequeue() ;
void display() ;
void main()
{
   int ch ;
   clrscr() ;
   do
   {
      printf("
[1].ENQUEUE [2].DEQUEUE [3].Display [4].Exit
") ;
      printf("
Enter your choice [1-4] : ") ;
      scanf("%d", &ch) ;
      switch(ch)
      {
         case 1 :
            enqueue() ;
            break ;
         case 2 :
            dequeue() ;
            break ;
         case 3 :
            display() ;
            break ;
         case 4 :
            break ;
         default :
            printf("
Invalid option
") ;
            getch() ;
      }
   } while(ch != 4) ;
   getch() ;
}

void enqueue()
{
   if(rear == SIZE - 1)
   {
      printf("
Queue is full (overflow)
") ;
      getch() ;
      return ;
   }
   rear++ ;
   printf("
Enter the element to ENQUEUE : ") ;
   scanf("%d", &arr[rear]) ;
   if(front == -1)
      front++ ;
}

void dequeue()
{
   if(front == -1)
   {
      printf("
Queue is empty (underflow)
") ;
      getch() ;
      return ;
   }
   printf("
The DEQUEUE element is : %d
", arr[front]) ;
   getch() ;
   if(front == rear)
      front = rear = -1 ;
   else
      front++ ;
}

void display()
{
   if(front == -1)
   {
      printf("
Queue is empty (underflow)
") ;
      getch() ;
      return ;
   }
   printf("
The elements in queue are :

FRONT ->") ;
   for(i = front ; i <= rear ; i++)
      printf(" ... %d", arr) ;
   printf(" ... <- REAR
") ;
   getch() ;
}