-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreeStacks.java
More file actions
106 lines (91 loc) · 3.04 KB
/
Copy pathThreeStacks.java
File metadata and controls
106 lines (91 loc) · 3.04 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
98
99
100
101
102
103
104
105
106
package basic;
public class ThreeStacks {
public int stackSize;
public int indexUsed;
public int[] stackPointer;
public StackNode[] buffer;
public ThreeStacks(int size) {
// do intialization if necessary
stackSize = size;
stackPointer = new int[3];
for (int i = 0; i < 3; ++i)
stackPointer[i] = -1;
indexUsed = 0;
buffer = new StackNode[stackSize * 3];
}
public void push(int stackNum, int value) {
// Write your code here
// Push value into stackNum stack
int lastIndex = stackPointer[stackNum];
stackPointer[stackNum] = indexUsed;
indexUsed++;
buffer[stackPointer[stackNum]] = new StackNode(lastIndex, value, -1);
}
public int pop(int stackNum) {
// Write your code here
// Pop and return the top element from stackNum stack
int value = buffer[stackPointer[stackNum]].value;
int lastIndex = stackPointer[stackNum];
if (lastIndex != indexUsed - 1)
swap(lastIndex, indexUsed - 1, stackNum);
stackPointer[stackNum] = buffer[stackPointer[stackNum]].prev;
if (stackPointer[stackNum] != -1)
buffer[stackPointer[stackNum]].next = -1;
buffer[indexUsed - 1] = null;
indexUsed--;
return value;
}
public int peek(int stackNum) {
// Write your code here
// Return the top element
return buffer[stackPointer[stackNum]].value;
}
public boolean isEmpty(int stackNum) {
// Write your code here
return stackPointer[stackNum] == -1;
}
public void swap(int lastIndex, int topIndex, int stackNum) {
if (buffer[lastIndex].prev == topIndex) {
int tmp = buffer[lastIndex].value;
buffer[lastIndex].value = buffer[topIndex].value;
buffer[topIndex].value = tmp;
int tp = buffer[topIndex].prev;
if (tp != -1) {
buffer[tp].next = lastIndex;
}
buffer[lastIndex].prev = tp;
buffer[lastIndex].next = topIndex;
buffer[topIndex].prev = lastIndex;
buffer[topIndex].next = -1;
stackPointer[stackNum] = topIndex;
return;
}
int lp = buffer[lastIndex].prev;
if (lp != -1)
buffer[lp].next = topIndex;
int tp = buffer[topIndex].prev;
if (tp != -1)
buffer[tp].next = lastIndex;
int tn = buffer[topIndex].next;
if (tn != -1)
buffer[tn].prev = lastIndex;
else {
for (int i = 0; i < 3; ++i)
if (stackPointer[i] == topIndex)
stackPointer[i] = lastIndex;
}
StackNode tmp = buffer[lastIndex];
buffer[lastIndex] = buffer[topIndex];
buffer[topIndex] = tmp;
stackPointer[stackNum] = topIndex;
}
}
class StackNode {
public int prev, next;
public int value;
public StackNode(int p, int v, int n) {
value = v;
prev = p;
next = n;
}
}