-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStackLL.java
57 lines (51 loc) · 956 Bytes
/
StackLL.java
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
/**
* This is a stack class
*
* @author Ching2 Huang
*
* @param <T>
*/
public class StackLL<T> implements Stack<T> {
// list stores the stack
private LinkedList<T> list = new LinkedList<T>();
/**
* Pushes an element onto the top of the stack.
*/
public void push(T data) {
list.insertFirst(data);
}
/**
* Removes the top of the stack and returns it.
*
* @return the popped data
*/
public T pop() {
T data = list.getFirst();
list.deleteFirst();
return data;
}
/**
* Gets the element at the top of the stack without removing it.
*
* @return the peeked data
*/
public T peek() {
return list.getFirst();
}
/**
* Checking if the list exists
*
* @return true if the list doesn't exist
*/
public boolean isEmpty() {
return list.isEmpty();
}
/**
* Returns a String representation of the stack.
*
* @return stack as String
*/
public String toString() {
return list.toString();
}
}