-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneric-stack.java
More file actions
164 lines (134 loc) · 4.19 KB
/
Generic-stack.java
File metadata and controls
164 lines (134 loc) · 4.19 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
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
154
155
156
157
158
159
160
161
162
163
164
class Stack<E> {
private final int size;
private int top;
private E[] elements;
public Stack() {
this(10);
}
public Stack(int s) {
size = s > 0 ? s : 10;
top = -1;
elements = (E[]) new Object[size]; // create array
}
public void push(E pushValue) {
if (top == size - 1) // if stack is full
throw new FullStackException(String.format("Stack is full, cannot push %s", pushValue));
elements[++top] = pushValue; // place pushValue on Stack
}
public E pop() {
if (top == -1) // if stack is empty
throw new EmptyStackException("Stack is empty, cannot pop");
return elements[top--]; // remove and return top element of Stack
}
}
class EmptyStackException extends RuntimeException {
public EmptyStackException() {
this("Stack is empty");
}
public EmptyStackException(String exception) {
super(exception);
}
}
class FullStackException extends RuntimeException {
public FullStackException() {
this("Stack is full");
}
public FullStackException(String exception) {
super(exception);
}
}
class MainClass {
public static void main(String args[]) {
double[] doubleElements = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
int[] integerElements = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
String [] strElements = {"rudra","rud","cs","course"};
Stack<Double> doubleStack = new Stack<Double>(5);
Stack<Integer> integerStack = new Stack<Integer>(10);
Stack<String> strStack = new Stack<String>(10);
// test Push Double
try {
System.out.println("\nPushing elements onto doubleStack");
for (double element : doubleElements) {
System.out.printf("%.1f ", element);
doubleStack.push(element);
}
} catch (FullStackException fullStackException) {
System.err.println();
fullStackException.printStackTrace();
}
// test Pop Double
try {
System.out.println("\nPopping elements from doubleStack");
double popValue;
while (true) {
popValue = doubleStack.pop(); // pop from doubleStack
System.out.printf("%.1f ", popValue);
}
} catch (EmptyStackException emptyStackException) {
System.err.println();
emptyStackException.printStackTrace();
}
// test push method with integer stack
try {
System.out.println("\nPushing elements onto integerStack");
for (int element : integerElements) {
System.out.printf("%d ", element);
integerStack.push(element);
}
} catch (FullStackException fullStackException) {
System.err.println();
fullStackException.printStackTrace();
}
// test pop method with integer stack
try {
System.out.println("\nPopping elements from integerStack");
int popValue; // store element removed from stack
// remove all elements from Stack
while (true) {
popValue = integerStack.pop();
System.out.printf("%d ", popValue);
}
} catch (EmptyStackException emptyStackException) {
System.err.println();
emptyStackException.printStackTrace();
}
//test push for strings
try {
System.out.println("\nPushing elements onto stringStack");
for (String element : strElements) {
System.out.printf("%s ", element);
strStack.push(element);
}
} catch (FullStackException fullStackException) {
System.err.println();
fullStackException.printStackTrace();
}
//test pop for strings
try {
System.out.println("\nPopping elements from stringStack");
String popValue; // store element removed from stack
// remove all elements from Stack
while (true) {
popValue = strStack.pop();
System.out.printf("%s ", popValue);
}
} catch (EmptyStackException emptyStackException) {
System.err.println();
emptyStackException.printStackTrace();
}
}
}
/*OUTPUT
Pushing elements onto doubleStack
1.1 2.2 3.3 4.4 5.5 6.6
Popping elements from doubleStack
5.5 4.4 3.3 2.2 1.1
Pushing elements onto integerStack
1 2 3 4 5 6 7 8 9 10 11
Popping elements from integerStack
10 9 8 7 6 5 4 3 2 1
Pushing elements onto stringStack
rudra rud cs course
Popping elements from stringStack
course cs rud rudra
*/