forked from matemaynard99/Project_2A
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
69 lines (67 loc) · 2.03 KB
/
Copy pathStack.java
File metadata and controls
69 lines (67 loc) · 2.03 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
/** A linked-list stack
@param <T>: Object type
*/
public class Stack<T> {
/** A singly-linked list node */
private class Node {
// Data fields
T data;
Node next;
// Constructor
Node(T value) { data = value; }
}
// Data fields
private Node top;
private int numOfItems;
// Constructors
public Stack() {} // Default constructor
public Stack(Stack<T> other) { // Copy constructor
numOfItems = other.numOfItems;
if (numOfItems != 0) {
top = new Node(other.top.data);
Node p = top, q = other.top.next;
while (q != null) {
p.next = new Node(q.data);
p = p.next;
q = q.next;
}
}
}
// Methods
/** Returns the size of the stack.
@return: number of items stored in the stack
*/
public int size() { return numOfItems; } // Time complexity: O(1)
/** Tests whether the stack is empty.
@return: {true} if the stack is empty; {false} otherwise
*/
public boolean isEmpty() { return size() == 0; } // Time complexity: O(1)
/** Returns the element on top of the stack.
@return: the top element
@throws IllegalArgumentException: stack is empty.
*/
public T peek() {
if (isEmpty()) { throw new IllegalArgumentException("Attempt to access item in empty stack."); }
return top.data;
} // Time complexity: O(1)
/** Removes and returns the element on top of the stack.
@return: the top element removed
@throws IllegalArgumentException: stack is empty.
*/
public T pop() {
if (isEmpty()) { throw new IllegalArgumentException("Attempt to remove item from empty stack."); }
T toBeRemoved = top.data;
top = top.next;
numOfItems--;
return toBeRemoved;
} // Time complexity: O(1)
/** Adds a new item onto the top of the stack.
@param item: the item to push
*/
public void push(T item) {
Node newTop = new Node(item);
newTop.next = top;
top = newTop;
numOfItems++;
} // Time complexity: O(1)
}