-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearqueue.java
More file actions
97 lines (91 loc) · 1.81 KB
/
linearqueue.java
File metadata and controls
97 lines (91 loc) · 1.81 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
package datastructures;
import java.util.*;
public class linearqueue {
static int front=1,rear,n;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Enter maximum size::");
n=sc.nextInt();
int a[]=new int[n+1];
System.out.println("Enter no of elements you want to insert");
rear=sc.nextInt();
if(n<rear)
{
System.out.println("Size is less please enter right values");
return;
}
System.out.println("Enter elements:");
for(int i=1;i<=rear;i++)
a[i]=sc.nextInt();
while(true)
{
System.out.println("Select action to perform::");
System.out.println(" 1. Insert/push");
System.out.println(" 2. Pop/Delete");
System.out.println("Enter choice:");
int c=sc.nextInt();
if(c==1)
{
if(rear==n)
{
System.out.println("Queue is already full cannot insert");
}
else
{
System.out.println("Enter item to insert");
int item=sc.nextInt();
insert(item,a);
}
}
if(c==2)
{
if(front==0)
{
System.out.println("Cannot delete , empty queue");
}
else
{
delete(a);
}
}
System.out.println("To display select 1");
int d=sc.nextInt();
if(d==1)
display(a);
System.out.println("Do want to continue Press 1 to continue and 0 to end");
int ch=sc.nextInt();
if(ch==0)
return;
else
continue;
}
}
public static void insert(int item,int a[])
{
rear++;
a[rear]=item;
}
public static void delete(int a[])
{
int item;
if(rear==front)
{
item=a[front];
rear=0;
front=0;
System.out.println("Deleted "+item);
}
else
{
item=a[front];
front++;
System.out.println("Deleted "+item);
}
}
public static void display(int a[])
{
for(int i=front;i<=rear;i++)
System.out.println(a[i]);
}
}