-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathDeQueueApp.java
153 lines (128 loc) · 2.37 KB
/
DeQueueApp.java
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Solved By: Sandeep Ranjan (1641012352)
Solution for Programming Project
4.3
*/
class DeQueue {
private
int max,rear,front;
int[] a;
public
DeQueue (int m) {
max = m;
a = new int[m];
rear = front = -1;
}
void insertRear(int val) {
if(isFullRear()) {
System.out.println("overflow Rear");
} else {
if(isEmpty()) {
front = rear = 0;
a[rear] = val;
} else {
rear++;
a[rear] = val;
}
}
}
void insertFront(int val) {
if(isFullFront()) {
System.out.println("overflow Front");
} else {
if(isEmpty()) {
front = rear = 0;
a[front] = val;
} else {
front--;
a[front] = val;
}
}
}
void display() {
for(int i=front; i<=rear; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
boolean isFullRear() {
if(rear == max -1) return true;
else return false;
}
boolean isFullFront() {
if(front == 0) return true;
else return false;
}
boolean isEmpty() {
if(rear == -1 && front == -1) return true;
else return false;
}
int deleteFront() {
if(isEmpty()) {
System.out.println("Underflow Front");
return -1;
} else {
int temp = a[front];
front++;
return temp;
}
}
int deleteRear() {
if(isEmpty()) {
System.out.println("Underflow Rear");
return -1;
} else {
int temp = a[rear];
rear--;
return temp;
}
}
}
class DeQueueApp {
public static void main(String[] agrs) {
DeQueue q = new DeQueue(5);
q.insertFront(10);
q.insertRear(20);
q.insertRear(30);
q.insertRear(40);
q.insertRear(50);
q.insertRear(60);
q.deleteFront();
q.deleteFront();
q.deleteRear();
q.deleteFront();
q.insertFront(10);
q.insertFront(20);
q.insertFront(80);
q.insertFront(70);
q.insertRear(60);
q.display();
}
}
Project 4.3
class StackY {
private DeQueue dequeStack;
public StackY(int maxSize) {
dequeStack = new DeQueue(maxSize);
}
public void push(int val) {
dequeStack.insertRear(val);
}
public int pop() {
return dequeStack.deleteRear();
}
public void display() {
dequeStack.display();
}
}
class StackYApp {
public static void main(String[] args) {
StackY theStackY = new StackY(5);
theStackY.push(10);
theStackY.push(20);
theStackY.push(30);
theStackY.pop();
theStackY.display();
}
}
--------------------------