-
Notifications
You must be signed in to change notification settings - Fork 0
/
6. Basic in my way for Queue.c
98 lines (96 loc) · 1.58 KB
/
6. Basic in my way for 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
#include<stdio.h>
#include<stdlib.h>
#define MAX 8
int queue[MAX], front=-1, rear=-1;
int enqueue(int data){
if(isFull()){
printf("Queue Overflow.\n");
return 0;
}
else{
if(front==-1)
front=0;
rear++;
queue[rear]=data;
return 1;
}
}
/*int enqueue(int data){
if(isFull()){
printf("Queue Overflow.\n");
}
else if(front==-1){
front=rear=0;
queue[rear]=data;
}
else {
rear++;
queue[rear]=data;
}
}*/
int dequeue(){
int data;
if(isEmpty()){
printf("Queue Underflow.\n");
return 0;
}
else{
data=queue[front];
front++;
return data;
}
}
int isEmpty(){
if(front==-1 || front==rear+1)
return 1;
else
return 0;
}
int isFull(){
if(rear==MAX-1)
return 1;
else
return 0;
}
int peek(){
if(isEmpty())
return 0;
else
return queue[front];
}
void print(){//void traverse()
int i;
if(isEmpty()){
exit(1);}//return 0;
else{
printf("Queue: ");
for(i=front; i<=rear; i++){
printf("%d ",queue[i]);
}
printf("\n");
}
}
//to delete all the queue at once*************************************
/*void emptyQueue() {
while (!isEmpty()) {
dequeue();
}
}*/
int main(){
enqueue(30);
enqueue(50);
enqueue(70);
enqueue(80);
enqueue(100);
enqueue(66);
enqueue(77);
printf("The first element of the queue is %d\n", peek());
print();
printf("Deleted element is: %d\n", dequeue());
print();
printf("Is the Queue empty? %s\n", isEmpty() ? "Yes" : "No");
printf("Is the Queue full? %s\n", isFull() ? "Yes" : "No");
print();
//emptyQueue();
return 0;
}