Queue using Array in C

In computer science, a queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed. Often a peek or front operation is also implemented, returning the value of the front element without dequeuing it. A queue is an example of a linear data structure, or more abstractly a sequential collection.



Implementation of Queue using Array in C :)



#include<stdio.h>
#include<conio.h>
#define MAX 20
 
int que[MAX], start = 0, end = 0, i;
 
void disp();
void ins();
void del();
void menu();
int main()
{
 clrscr();
 ins();
 disp();
 getch();
 menu();
 return 0;
}
 
 
void menu()
{
 int ch;
 clrscr();
 do
 {
  printf("\n1. Insert\n2. Delete\n3. Display\n\nPress 0 to exit.\n\n");
  printf("\nChoice your option :: ");
  fflush(stdin);
  scanf("%d", &ch);
 
  switch (ch)
  {
  case 1:ins(); break;
  case 2:del(); break;
  case 3:disp(); break;
  default:exit();
  }
 
 } while (ch != 0);
}
 
void disp()
{
 printf("\nElements of Queue :: ");
 for (i = start; i < end; i++)
 {
  printf(" %d , ", que[i]);
 }
}
 
void ins()
{
 char choice = 'y';
 
 do
 {
  printf("\nEnter Element of Queue :: ");
  scanf("%d", &que[end]);
  end++;
  fflush(stdin);
  printf("\nDo you want to insert more ? (Y/N) :: ");
  scanf("%c", &choice);
 } while (choice == 'Y' || choice == 'y' && end < MAX);
}
 
void del()
{
 char choice = 'y';
 do
 {
  printf("\nElement Deleted :: %d ", que[start]);
  start++;
  fflush(stdin);
 
  printf("\nDo you want to delete more ? (Y/N) :: ");
  scanf("%c", &choice);
 
 } while (choice == 'Y' || choice == 'y' && start != end);
}

Post a Comment