Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <windows.h>
- #define MAX 100
- struct queue
- {
- int item[MAX];
- int front, rear;
- };
- typedef struct queue queue;
- void enqueue (queue *q, int a)
- {
- if (q->rear == MAX - 1)
- printf("Queue is full.\n");
- else if (q->rear == -1)
- {
- (q->rear)++;
- (q->front)++;
- q->item[q->rear] = a;
- }
- else
- {
- (q->rear)++;
- q->item[q->rear] = a;
- }
- }
- int dequeue (queue *q)
- {
- int elt = 0;
- if(q->front== -1 || q->front > q->rear)
- printf("Queue is empty.\n");
- else
- {
- elt = q -> item [q -> front];
- (q->front)++;
- }
- return elt;
- }
- void traverse (queue *q)
- {
- int i;
- for(i=q->front; i<=q->rear; i++)
- {
- printf("%d\t", q -> item[i]);
- }
- }
- int main()
- {
- queue q;
- q.rear = -1;
- q.front = -1;
- int el,ch;
- while(1)
- {
- printf("\n\n1.Enqueue\n");
- printf("2.Dequeue\n");
- printf("3.Display all\n");
- printf("4.Exit\n\n");
- printf("Enter choice:");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1:
- printf("Enter element you want to Enqueue");
- scanf("%d",&el);
- enqueue(&q,el);
- break;
- case 2:
- el=dequeue(&q);
- printf("%d is Dequeued from the stack",el);
- break;
- case 3:
- traverse(&q);
- break;
- case 4:
- exit(0);
- default:
- printf("Invalid choice.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement