-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMyStack.java
More file actions
84 lines (72 loc) · 2.08 KB
/
MyStack.java
File metadata and controls
84 lines (72 loc) · 2.08 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
import java.util.ArrayList;
import java.util.Stack;
/**
* My implementation of basic stack using ArrayList
* Created by Devang on 29-Dec-16.
*/
public class MyStack<E> {
private ArrayList<E> myList = new ArrayList<>();
public MyStack(){
}
public void push(E item){
myList.add(item);
}
public E pop() {
if (myList.size() > 0)
return myList.remove(myList.size()-1);
return null;
}
public E peek() {
if (myList.size() > 0)
return myList.get(myList.size()-1);
return null;
}
public Boolean isEmpty() {
return myList.isEmpty();
}
public int size() {
return myList.size();
}
@Override
public String toString() {
return myList.toString();
}
public static void main (String[] args){
/* Integer */
MyStack<Integer> s1 = new MyStack<>();
s1.pop();
System.out.println("IsEmpty: " + s1.isEmpty());
s1.push(1);
System.out.println(s1);
s1.push(5);
s1.push(11);
s1.push(8);
s1.push(2);
System.out.println(s1);
System.out.println("Pop: " + s1.pop());
System.out.println("Pop: " + s1.pop());
System.out.println("Peek : " + s1.peek());
System.out.println("Size: " + s1.size());
System.out.println();
/* String */
MyStack<String> s2 = new MyStack<>();
s2.push("Algorithm Design");
s2.push("Java Software Solution");
s2.push("Cracking the Technical Interview");
System.out.println(s2);
System.out.println("Pop: " + s2.pop());
System.out.println("Peek: " + s2.peek());
System.out.println("Size: " + s2.size());
System.out.println("IsEmpty: " + s2.isEmpty());
System.out.println();
/* Built in */
Stack<Integer> s3 = new Stack<>();
s3.push(5);
s3.push(7);
s3.push(1);
s3.push(3);
System.out.println(s3);
System.out.println("Pop: " + s3.pop());
System.out.println("Peek: " + s3.peek());
}
}