-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueUsingStructure.c
90 lines (81 loc) · 1.6 KB
/
QueueUsingStructure.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<stdio.h>
#include<stdlib.h>
#define SIZE 10
int enqueue();
int dequeue();
int display();
struct queue
{
int front;
int rear;
int *q;
};
int choice,item;
int main()
{
struct queue Q;
Q.front=-1;
Q.rear=-1;
Q.q=(int*)malloc(10*sizeof(int));
for(;;)
{
printf("1-Enqueue \n2-Dequeue \n3-Display \n4-EXIT \nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:enqueue(&Q);
break;
case 2:dequeue(&Q);
break;
case 3:display(&Q);
break;
case 4:return 0;
default: printf("\nINVALID CHOICE\n");
}
}
}
int enqueue(struct queue *Q)
{
printf("Enter the element to enqueue: ");
scanf("%d",&item);
if(Q->rear==SIZE-1)
{
printf("\nQueue Overflow\n");
}
else
{
if(Q->front==-1)
{
Q->front++;
}
Q->rear++;
*(Q->q+Q->rear)=item;
}
int dequeue(struct queue *Q)
{
if (Q->front == -1)
{
printf("\nQueue Underflow\n");
return 0;
}
item=*(Q->q+Q->front);
Q->front++;
if(Q->front>Q->rear)
{
Q->front=Q->rear=-1;
}
printf("\nDequeued Element is: %d\n",item);
}
int display(struct queue *Q)
{
if(Q->front==-1)
{
printf("\nQueue is Empty\n");
}
printf("\nQueue Elements are: ");
for(int i=Q->front;i<Q->rear;i++)
{
printf("%d ",Q->q[i]);
}
printf("\n");
}