-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackQueueUse.java
150 lines (114 loc) · 4.04 KB
/
StackQueueUse.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
package DS;
import java.util.Scanner;
import java.util.Stack;
public class StackQueueUse {
public static boolean isExpressionBalanced(String s) {
StackUsingLL stack = new StackUsingLL();
for (int i = 0; i < s.length(); i++) {
try {
switch (s.charAt(i)) {
case '{':
case '(':
case '[':
stack.push(s.charAt(i));
break;
case '}':
if (stack.top() == '{') stack.pop();
else return false;
break;
case ']':
if (stack.top() == '[') stack.pop();
else return false;
break;
case ')':
if (stack.top() == '(') stack.pop();
else return false;
break;
default: break;
}
}
catch(StackEmptyException e) { return false; }
}
return stack.isEmpty();
}
public void reverseStack(StackUsingLL s) {
StackUsingLL s1 = new StackUsingLL(),
s2 = new StackUsingLL();
while (!s.isEmpty()) {
try {
s1.push(s.pop());
} catch (StackEmptyException e) { /* Not possible */ }
}
while (!s1.isEmpty()) {
try {
s2.push(s1.pop());
} catch (StackEmptyException e) { /* Not possible */ }
}
while (!s2.isEmpty()) {
try {
s.push(s2.pop());
} catch (StackEmptyException e) { /* Not possible */ }
}
}
public static int[] findSpans(int[] a) {
if (a.length == 0)
return new int[0];
int[] output = new int[a.length];
output[0] = 1;
Stack<GPair<Integer, Integer>> s = new Stack<>();
GPair<Integer, Integer> p = new GPair<>();
p.index = 0;
p.value = a[0];
s.push(p);
for (int i = 1; i < a.length; i++) {
while (!s.empty() && s.peek().value < a[i] )
s.pop();
if (s.empty()) output[i] = (i + 1);
else output[i] = (i - s.peek().index);
GPair<Integer, Integer> p1 = new GPair<>();
p1.index = i;
p1.value = a[i];
s.push(p1);
}
return output;
}
public static void main(String[] args) throws QueueEmptyException, QueueFullException {
// int[] a = {10,9,8,15,4,2,7,6,10,8,6},
// spans = findSpans(a);
// for (int i = 0; i < a.length; i++)
// System.out.println(a[i] + " " + spans[i]);
int[] a = {1,2,3,4,5};
StackUsingLL s = new StackUsingLL();
for (int i = 0; i < a.length; i++)
s.push(a[i]);
while(!s.isEmpty()) {
try {
System.out.println(s.pop());
} catch (StackEmptyException e) {
// Cant come here
System.out.println("Cant come here");
}
}
// int[] a = {1,2,3,4,5};
// QueueUsingArray q = new QueueUsingArray();
// for (int i = 0; i < a.length; i++) {
// try {
// q.enqueue(a[i]);
// } catch (QueueFullException e) { }
// }
// System.out.println(q.dequeue());
// q.enqueue(10);
//
// Scanner s = new Scanner(System.in);
// q.enqueue(s.nextInt());
// q.enqueue(s.nextInt());
// q.enqueue(s.nextInt());
//
// System.out.println(q.size());
//
// while (!q.isEmpty())
// System.out.println(q.dequeue());
//
// System.out.println(q.size());
}
}