-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue
More file actions
90 lines (87 loc) · 1.49 KB
/
Queue
File metadata and controls
90 lines (87 loc) · 1.49 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
import java.util.Scanner;
public class Queue
{
static Scanner sc=new Scanner(System.in);
static int n,front=-1, rear=-1,item;
static int[] Ar=new int[10];
Queue()
{
}
Queue(int n)
{
int ch;
do
{
System.out.println("1. Enqueue\n 2. Dequeue\n 3.Display\n");
System.out.println("enter your choice\n");
ch=sc.nextInt();
switch(ch)
{
case 1:
enqueue(n);
break;
case 2:
dequeue(n);
break;
case 3:
display(n);
break;
default:
break;
}
}
while(ch<3);
}
public static void main(String[] args)
{
Scanner sc1=new Scanner(System.in);
int n;
System.out.println("Enter the size of queue");
n=sc1.nextInt();
new Queue(n);
}
public static void enqueue(int n)
{
if(rear>= n-1)
System.out.println("overflow");
else
if(front==-1 && rear==-1)
{
front++;
}
System.out.println("enter the element to be inserted\n");
item=sc.nextInt();
rear++;
Ar[rear]=item;
display(rear);
}
public static void dequeue(int n)
{
if(front==-1)
System.out.println("underflow\n");
else
{
item=Ar[front];
System.out.printf("the element deleted is %d \n",item);
if(front==rear)
{
front=-1;
rear=-1;
System.out.println("the queue is empty");
}
else
front++;
display(rear);
}
}
public static void display(int rear)
{
int i;
System.out.println("the current queue is\n");
for(i=front;i<=rear;i++)
{
System.out.printf("%d",Ar[i]);
}
System.out.println("\n");
}
}