Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define n 5
- int queue[n];
- int front =-1;
- int rear =-1;
- void insert();
- void delete();
- void display();
- void peek();
- int main()
- {
- int choice;
- while(1)
- {
- printf("\t\t\t1.Insert");
- printf("\n\t\t\t2.Delete");
- printf("\n\t\t\t3.Display");
- printf("\n\t\t\t4.Top");
- printf("\n\t\t\t5.Quit");
- printf("\nEnter your choice: ");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1: insert();
- break;
- case 2: delete();
- break;
- case 3: display();
- break;
- case 4: peek();
- break;
- case 5: exit(1);
- break;
- default: printf("error\n");
- }
- fflush(stdin);
- }
- }
- void insert()
- {
- int x;
- printf("Enter the value to insert: ");
- scanf("%d",&x);
- if(front==-1 && rear==-1)
- {
- front=rear=0;
- queue[rear]=x;
- }
- else if(((rear+1)%n)== front)
- {
- printf("Queue is full.Overflow");
- }
- else
- {
- rear =(rear+1)%n;
- queue[rear]=x;
- }
- }
- void delete()
- {
- if(front==-1 && rear==-1)
- {
- printf("Underflow");
- }
- else if(front == rear)
- {
- front=rear=-1;
- }
- else
- {
- printf("Dequeued:%d\n",queue[front]);
- front=(front+1)%n;
- }
- }
- void display()
- {
- int i=front;
- if(front==-1 && rear==-1)
- {
- printf("Queue is empty");
- }
- else
- {
- printf("\n\nQueue elements are\n");
- while(i!=rear)
- {
- printf("%d\t",queue[i]);
- i=(i+1)%n;
- }
- printf("%d",queue[rear]);
- }
- }
- void peek()
- {
- if(front==-1 && rear==-1)
- {
- printf("it is empty\n");
- }
- else
- {
- printf("\n\nPeek element:%d\n",queue[front]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement