-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathKStacks.java
76 lines (56 loc) · 1.85 KB
/
KStacks.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
public class Main {
static class kstack {
int arr[], top[], next[], free;
public kstack(int n, int k) {
arr = new int [n];
top = new int [k];
next = new int [n];
for(int i=0; i<k; i++) top[i] = -1;
free = 0;
for(int i=0; i<n-1; i++) next[i] = i+1;
next[n-1] = -1;
}
public boolean isFull() {
return (free == -1);
}
public boolean isEmpty(int sn) {
return (top[sn] == -1);
}
void push(int data, int sn) {
if(isFull()) {
System.out.println("Stack Overflow");
}
int i = free;
free = next[i];
next[i] = top[sn];
top[sn] = i;
arr[i] = data;
}
int pop(int sn) {
if(isEmpty(sn)) System.out.println("Stack is Empty");
int i = top[sn];
top[sn] = next[i];
next[i] = free;
free = i;
return arr[i];
}
}
public static void main(String args[]) {
kstack stack = new kstack(10, 3);
stack.push(10, 1);
stack.push(15, 1);
stack.push(20, 1);
stack.push(25, 1);
stack.push(6, 2);
stack.push(8, 2);
stack.push(1, 0);
stack.push(9, 0);
stack.push(13, 0);
System.out.println("Popped element from stack 2 is " + stack.pop(1));
System.out.println("Popped element from stack 2 is " + stack.pop(1));
System.out.println("Popped element from stack 1 is " + stack.pop(0));
System.out.println("Popped element from stack 1 is " + stack.pop(0));
System.out.println("Popped element from stack 3 is " + stack.pop(2));
System.out.println("Popped element from stack 3 is " + stack.pop(2));
}
}