-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
73 lines (63 loc) · 1.46 KB
/
Stack.java
File metadata and controls
73 lines (63 loc) · 1.46 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
public class Stack<E>{
private LinkedList<E> container;
public Stack(){
container = new LinkedList<E>();
}
/**
* Pushes the given val onto the stack
* @param val the val to be pushed
*/
public void push(E val){
container.addFront(val);
}
/**
* Pops the top of the stack
* @return the value that was on the top of the stack
*/
public E pop(){
return container.removeFront();
}
/**
* Returns the value at the top of the stack
* @return the value at the top of the stack
*/
public E peek(){
return container.getHead();
}
/**
*
* @return the size of the stack
*/
public int size(){
return container.size();
}
/**
*
* @return whether or not the stack is empty
*/
public boolean isEmpty(){
return container.size() == 0;
}
/**
* Empties the stack
*/
public void empty(){
container.empty();
}
@Override
public String toString(){
StringBuilder str = new StringBuilder();
Stack<E> temp = new Stack<E>();
while(!this.isEmpty()){
//push this onto temp
temp.push(this.pop());
str.append(temp.peek().toString());
str.append(", ");
}
while(!temp.isEmpty()){
//return state
this.push(temp.pop());
}
return str.toString();
}
}