forked from namigoel/Hacktober-Open
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyqueue.java
More file actions
93 lines (87 loc) · 1.72 KB
/
Copy pathmyqueue.java
File metadata and controls
93 lines (87 loc) · 1.72 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
import java.util.*;
class myqueue
{ int q[];
int max=100;
int front,rear;
public myqueue()
{
max=5;
q=new int[max];
front=rear=-1;
}
public myqueue(int m)
{ max=m;
q=new int[max];
front=rear=-1;
}
void insert(int num)
{
// System.out.println(max+" max, rear "+rear);
if(rear==max-1)
{
System.out.println("Queue is full");
}
else if(rear==-1)
{
front++;
rear++;
q[rear]=num;
}
else
{
rear++;
q[rear]=num;
}
} // End of insert
int delete()
{int temp=-999;
if(front==-1)
{System.out.println("Queue empty");
return temp;}
else if(front==rear)
{temp=q[front];
front=-1;rear=-1;
return temp;}
else
{temp=q[front];
front++;
return temp;}
}
void traverse()
{
if(front>-1)
{
for(int i=front;i<=rear;i++)
{
System.out.print(q[i]+" ");
}
System.out.println();
}
}
public static void main(String[] args)
{
myqueue m1= new myqueue(5);
Scanner sc= new Scanner(System.in);
int ch=1;
while(ch != 4) //menu driven program
{
System.out.println("Enter choice:\n1.insert\n2.delete\n3.traverse\n4.exit");
ch=sc.nextInt();
switch(ch)
{
case 1:System.out.println("Enter num to insert");
int num=sc.nextInt();
int x=num;
m1.insert(x);
break;
case 2: System.out.println(m1.delete());
break;
case 3: m1.traverse();
break;
//case 4:System.exit([]);
default: m1.traverse();
break;
}
}
}
}