Program to create a Queue (Insert,Delete,Display)

Program to create a Queue (Insert,Delete,Display)

0 240

A queue is a particular kind of 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 and removal of entities from the front terminal position. 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 an element is added, all elements that were added before have to be removed before the new element can be invoked. A queue is an example of a linear data structure.

Queues provide services in computer science, transport, and operations research where various entities such as data, objects, persons, or events are stored and held to be processed later. In these contexts, the queue performs the function of a buffer.
Queues are common in computer programs, where they are implemented as data structures coupled with access routines, as an abstract data structure or in object-oriented languages as classes. Common implementations are circular buffers and linked lists.

There are following basic queue operations:
1. Enqueue :An enqueue to an empty queue is placed in the first element of the array. For enqueue, first we check the condition of ‘Queue Full or Overflow’

2. Dequeue : Dequeues take place at the front of the queue.An element is deleted from the queue.

#include
#include
#define MAX 10
struct que
            {
            int X[20];
            clrscr();
            int f,r;
            }q;
main()
{
int choice,i,n,f,r;
q.f=0;
q.r=-1;
do
{
printf("\n\n\n1-Insert\n2-Delete\n3-Display\n4-Exit\n\n") ;
printf("The selected option:");
scanf("%d",&choice);
switch(choice)
            {
            case 1:
            if(q.r>=MAX-1)
                        {
                        printf("\nQueue is overflow");
                        break;
                        }
            else
                   {
                   printf("\n\nEnter element:");
                   scanf("%d",&n);
                   q.X[++q.r]=n;
                   printf("\n\n\nQueue\n\n");
                   for(i=q.f;i<=q.r;i++)
                   printf("%d\t",q.X[i]);
                   break;
                   }
            case 2:
                    if(q.f>q.r)
                    printf("Queue is empty\n");
                    else
                        {
                        printf("\n\nYou are deleting %d",q.X[q.f]);
                        q.f++;
                        }
       break;
       case 3:
       printf("\n\n\nQueue\n\n");
                   for(i=q.f;i<=q.r;i++)
                   printf("%d\t",q.X[i]);
            break;
            case 4:break;
            }
   }while(choice!=4)                             ;
}

OUTPUT:

1-Insert
2-Delete
3-Display
4-Exit

The selected option:1

Enter element:10

Queue

10

1-Insert
2-Delete
3-Display
4-Exit

The selected option:1

Enter element:20

SIMILAR ARTICLES

NO COMMENTS

Leave a Reply