-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedStack.java
63 lines (57 loc) · 1.23 KB
/
LinkedStack.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
58
59
60
61
62
63
/**
* Class implementing Stack with Doubly Linked List.
*
* @param <E> type of the stored elements.
*/
class LinkedStack<E> implements Stack<E> {
private LinkedList<E> list;
/**
* Constructs empty linked stack.
*/
LinkedStack() {
list = new LinkedList<>();
}
/**
* Return size of the stack.
*
* @return size
*/
public int size() {
return this.list.size();
}
/**
* Check whether the stack is empty.
*
* @return empty or not
*/
public boolean isEmpty() {
return this.list.size() == 0;
}
/**
* Get the element from the top.
*
* @return top element
* @throws IndexOutOfBoundsException
* if the stack is empty
*/
public E top() throws IndexOutOfBoundsException {
return this.list.get(0);
}
/**
* Insert an element on the top of the stack.
*
* @param e element to be inserted
*/
public void push(E e) {
this.list.add(0, e);
}
/**
* Remove the element from the top.
*
* @throws IndexOutOfBoundsException
* if the stack is empty
*/
public E pop() throws IndexOutOfBoundsException {
return this.list.remove(0);
}
}