-
Notifications
You must be signed in to change notification settings - Fork 38
/
Queue in Array.c
109 lines (95 loc) · 2.29 KB
/
Queue in Array.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct que
{
int front, rear, size;
unsigned actualSize;
int* arr;
};
struct que* createque(unsigned actualSize)
{
struct que* que = (struct que*) malloc(sizeof(struct que));
que->actualSize = actualSize;
que->front = que->size = 0;
que->rear = actualSize - 1;
que->arr = (int*) malloc(que->actualSize * sizeof(int));
return que;
}
int isFull(struct que* que)
{ return (que->size == que->actualSize); }
void enqueue(struct que* que, int item)
{
if (isFull(que))
return;
que->rear = (que->rear + 1)%que->actualSize;
que->arr[que->rear] = item;
que->size = que->size + 1;
printf("%d enqueued to que\n", item);
}
int isEmpty(struct que* que)
{ return (que->size == 0); }
int dequeue(struct que* que)
{
if (isEmpty(que))
return INT_MIN;
int item = que->arr[que->front];
que->front = (que->front + 1)%que->actualSize;
que->size = que->size - 1;
return item;
}
int front(struct que* que)
{
if (isEmpty(que))
return INT_MIN;
return que->arr[que->front];
}
int rear(struct que* que)
{
if (isEmpty(que))
return INT_MIN;
return que->arr[que->rear];
}
int main()
{
int val,n;
struct que* que = createque(1000);
do
{printf("\n************************* MENU ************************");
printf("\n1.ENQUEUE");
printf("\n2.DEQUEUE");
printf("\n3.IS EMPTY");
printf("\n4.IS FULL");
printf("\n5.FIRST ELE");
printf("\n6.LAST ELE");
printf("\n7.EXIT");
printf("\nenter ur choice : ");
scanf("%d",&n);
switch(n)
{case 1: printf("\nenter the value ");
scanf("%d",&val);
enqueue(que,val);
break;
case 2:
dequeue(que);
break;
case 3:
printf("\nIsEmpty : %d",isEmpty(que));
break;
case 4:
printf("\nIsFull : %d",isFull(que));
break;
case 5:
printf("\nFront element: %d",front(que));
break;
case 6:
printf("\nLast element : %d", rear(que));
break;
case 7: exit(0);
break;
default: printf("\n Wrong Choice!");
break;
}
printf("\ndo u want to cont... ");
}while('y'==getch());
}