Skip to content

Commit 339399c

Browse files
Merge pull request #170 from AayushBangroo/master
Generic Stack class added
2 parents f202d63 + 3cc8e9a commit 339399c

1 file changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
Statement: Implement a Generic Stack class in Java
3+
*/
4+
5+
import java.util.ArrayList;
6+
7+
class Stack<T> {
8+
private final ArrayList<T> stack;
9+
private final int size;
10+
private int top;
11+
12+
//initialize stack and its fields
13+
public Stack(int size) {
14+
this.size = size;
15+
this.top = -1;
16+
stack = new ArrayList<>(size);
17+
}
18+
19+
//Returns top value in stack
20+
public T top() {
21+
if (top == -1) {
22+
System.out.println("Stack is empty");
23+
return null;
24+
}
25+
26+
return stack.get(top);
27+
}
28+
29+
//Removes top value in stack and returns the popped value
30+
public T pop() {
31+
if (top == -1) {
32+
System.out.println("Stack is empty");
33+
return null;
34+
}
35+
36+
T popped = stack.get(top);
37+
//set value to null
38+
stack.set(top, null);
39+
top--;
40+
return popped;
41+
}
42+
43+
//Push value in stack
44+
public void push(T val) {
45+
//check if stack is full
46+
if (top == size - 1) {
47+
System.out.println("Stack overflow");
48+
return;
49+
}
50+
51+
//push new value in stack
52+
stack.add(val);
53+
top++;
54+
}
55+
56+
//checks if stack is empty
57+
public boolean isEmpty() {
58+
return top == -1;
59+
}
60+
61+
//get stack size
62+
public int size() {
63+
return this.size;
64+
}
65+
66+
//print stack data and meta-data
67+
@Override
68+
public String toString() {
69+
return "Stack{" +
70+
"stack=" + stack +
71+
", size=" + size +
72+
", top=" + top +
73+
'}';
74+
}
75+
}
76+
77+
public class GenericStack {
78+
public static void main(String[] args) {
79+
//stack of integers of size 10
80+
Stack<Integer> integerStack = new Stack<>(10);
81+
82+
//fill the stack completely
83+
for (int i = 0; i < integerStack.size(); i++) {
84+
integerStack.push(i);
85+
}
86+
87+
//print stack information
88+
System.out.println(integerStack);
89+
90+
//print top value in stack
91+
System.out.println(integerStack.top());
92+
93+
//stack is full
94+
integerStack.push(100);
95+
96+
//pop all elements in stack
97+
for (int i = 0; i < integerStack.size(); i++) {
98+
integerStack.pop();
99+
}
100+
101+
//print stack information
102+
System.out.println(integerStack);
103+
104+
//stack underflow
105+
integerStack.pop();
106+
107+
//stack of String of size 5
108+
Stack<String>stringStack = new Stack<>(5);
109+
110+
stringStack.push("Hello");
111+
stringStack.push("Java");
112+
stringStack.push("Programmers");
113+
114+
//get top value in stack
115+
System.out.println(stringStack.top());
116+
117+
//pop the stack and get popped value
118+
String poppedValue = stringStack.pop();
119+
120+
//print popped value
121+
System.out.println(poppedValue);
122+
123+
//print stack information
124+
System.out.println(stringStack.toString());
125+
}
126+
}

0 commit comments

Comments
 (0)