Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX 10
- void enqueue();
- void dequeue();
- void display();
- void peek();
- int qArray[MAX];
- int rear = -1;
- int front = -1;
- int main()
- {
- int choice = 0;
- while (1)
- {
- printf("1. Insert\n2. Delete\n3. Display\n4. "
- "Peek\n5. Quit\nEnter your choice : ");
- scanf(" %d", &choice);
- switch (choice)
- {
- case 1:
- enqueue();
- break;
- case 2:
- dequeue();
- break;
- case 3:
- display();
- break;
- case 4:
- peek();
- break;
- case 5:
- exit(1);
- default:
- printf("Wrong choice \n");
- }
- }
- return 0;
- }
- void enqueue()
- {
- int add_item;
- int newRear = ((rear + 1) % MAX);
- if (newRear == front)
- printf("Queue overflow \n");
- else
- {
- printf("Insert the element in queue : ");
- scanf(" %d", &add_item);
- if (front == -1)
- {
- front = 0;
- rear = 0;
- qArray[rear] = add_item;
- }
- else
- {
- rear = newRear;
- qArray[rear] = add_item;
- }
- }
- }
- void dequeue()
- {
- if (front == -1)
- {
- printf("Queue underflow \n");
- return;
- }
- else
- {
- printf("Element deleted from queue is %d\n", qArray[front]);
- if (front == rear)
- {
- front = -1;
- rear = -1;
- }
- else
- front = ((front + 1) % MAX);
- }
- }
- void display()
- {
- int i = 0;
- if (front == -1)
- printf("Queue is empty \n");
- else
- {
- i = front;
- printf("Queue is : \n[%d, ", qArray[i]);
- while (((i++) % MAX) != rear)
- printf("%d%s", qArray[i % MAX], (i) % MAX == rear ? "" : ", ");
- printf("]\n\n");
- }
- }
- void peek()
- {
- if (front != -1)
- printf("front item = %d\n", qArray[front]);
- else
- printf("Queue is empty\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment