-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericStack.java
More file actions
47 lines (44 loc) · 1.09 KB
/
GenericStack.java
File metadata and controls
47 lines (44 loc) · 1.09 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
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
class myStack<E> {
ArrayList<E> stack = new ArrayList<E>();
public int getsize(){
return stack.size();
}
public boolean isEmpty(){
return stack.isEmpty();
}
public E peek(){
return stack.get(getsize()-1);
}
public void push(E o){
stack.add(o);
}
public E pop(){
E o = stack.get(getsize()-1);
stack.remove(getsize()-1);
return o;
}
public <E> void printlist(){
ListIterator<E> it = (ListIterator<E>) stack.listIterator(getsize());
while(it.hasPrevious()){
System.out.println(it.previous());
}
}
}
public class GenericStack{
public static void main(String[] args){
myStack<String> s = new myStack<String>();
s.push("Hello");
s.push("World");
s.push("This");
s.push("Program");
System.out.println("Number of Elements before:"+s.peek());
System.out.println("\n"+"Before pop------------------"+"\n");
s.printlist();
System.out.println("\n"+"After pop-----------------------"+s.pop()+"\n");
s.printlist();
System.out.println("Number of Elements after:"+s.peek());
}
}