-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.c
145 lines (127 loc) · 3.05 KB
/
Queue.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//Program for a priority queue where priority increases accoording to the decimal weight
#include<stdio.h>
#define MAX 5
int head=-1; //Where head is the position in which the last element was removed
int tail=0; //Where the tail is the position where the next element is to be added
int val=-1; //value for storage after dequeuing
int Array[MAX]; //initializaton of the array queue
int checker=0; //value to check if the array has gone cyclic so that we know how to print
//Creation of the function to check if it is empty
int IsEmpty(){
if((head==-1 && tail==0)|| ((head+1)%MAX)==tail){
//printf("The queue is empty\n");
return 0;
}
else if ((head==MAX-1) && (tail<MAX))
{
head=head+1;
head=head%MAX;//Or simply just set head to be zero...
return 1; //return value of 1 is for negative
}
else{
return 1;
}
}
//Function to check if the array is full
int IsFull(){
if((tail==MAX && head==-1)||(tail==(head+1))&& checker!=0){
// printf("The queue is full\n");
return 0;
}
else if (tail==MAX && head!=-1)
{
tail=tail%MAX;
checker=checker+1;
return 1;
}
else{
return 1;
}
}
//Function to enqueue
void Enqueue(int x){
if(IsFull()==1){
Array[tail]=x;
tail=tail+1;
//printf("The value of IsFull is %d\n", IsFull());
// printf("Value of TAIL is %d\n", tail);
//printf("Value of HEAD is %d\n", head);
//printf("The %d element of the queue is %d\n",tail,Array[tail]);
}
else{
printf("The queue is full\n");
return;
}
}
//Function to dequeue
int Dequeue(){
if(IsEmpty()==1){
// printf("Value of IsEmpty is %d\n", IsEmpty());
head=head+1;
val=Array[head];
return val;
}
else{
printf("The queue is empty\n");
}
return -1;
}
//Peek function
int peek(){
if(IsEmpty()!=0){
val=Array[head];
return val;
}
else{
printf("The queue is empty\n");
}
return -1;
}
//Function to print the queue
void printQ(){
printf("Queue members are as follows:\n");
if (checker!=0)
{
for(int i=(head+1);i<MAX;i++){
printf("%d and it's of index %d\n",Array[i], i);
}
for(int i=0;i<tail;i++){
printf("%d and it's of index %d\n", Array[i],i);
}
printf("Position of head is %d\n",head);
printf("Position of tail is %d\n",tail);
}
else{
for(int i=(head+1);i<tail;i++){
printf("%d and it's of index %d\n",Array[i], i);
printf("Position of head is %d\n",head);
printf("Position of tail is %d\n",tail);
}
}
/* if(tail<MAX){
for(int i=0;i<=tail;i++){
printf("%d\n", Array[i]);
}
}*/
}
//main function
#include<stdio.h>
int main(){
Enqueue(5);
Enqueue(8);
Enqueue(6);
Dequeue();
Enqueue(44);
Dequeue();
Dequeue();
// Dequeue();
Enqueue(17);
Enqueue(85);
Enqueue(18);
Enqueue(50);
Enqueue(98);
Enqueue(49);
printQ();
// printf("Position of head is %d\n",head);
return 0;
}