-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStacks.java
More file actions
118 lines (95 loc) · 3.51 KB
/
Stacks.java
File metadata and controls
118 lines (95 loc) · 3.51 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
public class Stacks{
/**
* A collection of objects that are addeded and removed according to the last-in
* first-out principle. Although similar in purpose, this interface differs from
* java.util.Stack.
*
* @author Michael T. Goodrich
* @author Roberto Tamassia
* @author Michael H. Goldwasser
*
*
*/
public interface Stack<E> {
/**
* Returns the number of elements in the stack.
* @return number of elements in the stack
*/
int size();
/**
* Tests whether the stack is empty.
* @return true if the stack is empty, false otherwise
*/
boolean isEmpty();
/**
* Adds an element at the top of the stack.
* @param e the element to be added
*/
void push(E e);
/**
* Returns, but does not remove, the element at the top of the stack.
* @return top element in the stack (or null if empty)
*/
E top();
/**
* Removes and returns the top element from the stack.
* @return element removed (or null if empty)
*/
E pop();
}
public static class ArrayStack<E> implements Stack<E> {
// instance variables
public static final int CAPACITY=1000; // default array capacity
private E[] data; // generic array used for storage
private int t = -1; // index of the top element in stack
// constructors
public ArrayStack() { this(CAPACITY); } // constructs stack with default capacity
public ArrayStack(int capacity) { // constructs stack with given capacity
data = (E[]) new Object[capacity]; // safe cast; compiler may give warning
}
// methods
/** Returns the number of elements in the stack. */
public int size() { return (t + 1); }
/** Tests whether the stack is empty. */
public boolean isEmpty() { return (t == -1); }
/** Adds an element at the top of the stack. */
public void push(E e) throws IllegalStateException {
if (size() == data.length) throw new IllegalStateException("Stack is full");
data[++t] = e; // increment t before storing new item
}
/** Returns, but does not remove, the element at the top of the stack. */
public E top() {
if (isEmpty()) return null;
return data[t];
}
/** Removes and returns the top element from the stack. */
public E pop() {
if (isEmpty()) return null;
E answer = data[t];
data[t] = null; // dereference to help garbage collection
t--;
return answer;
}
public void removeAll(E e) {
//checks for the end of recursion
if (size() < 1) {
System.out.println("All elements have been removed.\n");
}
else {
//update
System.out.println("Element \""+pop()+"\" has been removed.\n");
//recursive call
removeAll(top());
}
}
}
public static void main(String[] args) {
//intialization
ArrayStack c;
c = new ArrayStack<>();
for( int i = 1; i < 5; i++)
c.push("Element: " + i);
//initial call of the function
c.removeAll(c.top());
}
}