-
Notifications
You must be signed in to change notification settings - Fork 0
/
5. Full details Basic of Queue.c
93 lines (93 loc) · 1.56 KB
/
5. Full details Basic of 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
#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 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())
exit(1);
else
return queue[front];
}
void print(){
int i;
if(isEmpty()){
exit(1);}
else{
printf("Queue: \n");
for(i=front; i<=rear; i++){
printf("%d ",queue[i]);
}
printf("\n");
}
}
int main(){
int choice,data;
while(1){
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Print the first element\n");
printf("4. Print all the elements\n");
printf("5. Enter your choice: ");
scanf("%d", &choice);
switch(choice){
case 1:
printf("Enter the element to be added in the queue: ");
scanf("%d", &data);
enqueue(data);
printf("Element is added in the queue successfully.\n");
break;
case 2:
data=dequeue();
printf("Deleted element is: %d\n", data);
break;
case 3:
printf("The first element of the queue is %d\n", peek());
break;
case 4:
print();
break;
case 5:
exit(1);
default:
printf("Wrong choice\n");
}
}
return 0;
}