Saturday, 31 March 2012

Program - Simple Queue


Program -  Simple Queue (Static Queue/ queue implementation using Array)

// Program for Static queue

#include<stdio.h>
#include<conio.h>
#define SIZE 5

int front=-1;
int rear=-1;
int q[SIZE];

void insert();
void del();
void display();

void main()
{
  int choice;

  do
  {
    clrscr();
    printf("\t Menu");
    printf("\n 1. Insert");
    printf("\n 2. Delete");
    printf("\n 3. Display ");
    printf("\n 4. Exit");

    printf("\n Enter Your Choice");
    scanf("%d",&choice);

    switch(choice)
    {
      case 1:
          insert();
          display();
          getch();
          break;
      case 2:
          del();
          display();
          getch();
          break;
      case 3:
          display();
          getch();
          break;
      case 4:
          printf("End of Program");
          getch();
          exit(0);
    }
  }while(choice!=4);
}


void insert()
{
  int no;
  printf("\n Enter No.:");
  scanf("%d",&no);

  if(rear<SIZE-1)
  {
    q[++rear]=no;
    if(front==-1)
      front=0;// front=front+1;
  }
  else
  {
    printf("\n Queue overflow");
  }
}

void del()
{
  if(front==-1)
  {
    printf("\nQueue Underflow");
    return;
  }
  else
  {
    printf("\nElement deleted:-->%d",q[front]);
  }
  if(front==rear)
  {
    front=-1;
    rear=-1;
  }
  else
  {
    front=front+1;
  }
}

void display()
{
  int i;
  if(front==-1)
  {
    printf("\nQueue is empty....");
    return;
  }
  for(i=front;i<=rear;i++)
    printf("\n%d",q[i]);
}

No comments:

Post a Comment