-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLa2_01.java
More file actions
99 lines (99 loc) · 1.92 KB
/
La2_01.java
File metadata and controls
99 lines (99 loc) · 1.92 KB
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
#include<stdio.h>
#define max 5
int queue[max];
int front=-1;
int rear=-1;
int isEmpty();
int isFull();
void insert(int);
int delete();
void traverse();
int main()
{
int choice, item;
do
{
printf("--Queue Operations--\n");
printf("Press 1 to Insert\n");
printf("Press 2 to Delete\n");
printf("Press 3 to Traverse\n");
printf("Press 4 for Exit\n");
printf("Enter your choice:\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the element to insert:\n");
scanf("%d", &item);
insert(item);
break;
case 2:
item=delete();
printf("Deleted element: %d\n", item);
break;
case 3:
traverse();
break;
case 4:
exit(0);
default:
printf("Invalid choice");
}
}
while(choice>=1 && choice<=4);
}
int isEmpty()
{
if(rear==-1)
return 1;
else
return 0;
}
int isFull()
{
if(rear==max-1)
return 1;
else
return 0;
}
void insert(int item)
{
if(isFull())
printf("Queue is full\n");
else
{
if(isEmpty())
front=rear=0;
else
rear++;
queue[rear]=item;
}
}
int delete()
{
int item;
if(isEmpty())
printf("Queue is empty\n");
else
{
item=queue[front];
if(front==rear)
front=rear=-1;
else
front++;
}
return item;
}
void traverse()
{
int i;
if(isEmpty())
printf("Queue is empty\n");
else
{
printf("Queue contains ");
for(i=front; i<=rear; i++)
printf("%d ", queue[i]);
printf("\n");
}
}