This repository was archived by the owner on Dec 16, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
173 lines (149 loc) · 3.79 KB
/
Stack.java
File metadata and controls
173 lines (149 loc) · 3.79 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* The <tt>Stack</tt> class represents a last-in-first-out (LIFO) stack of
* generic items. It supports the usual <em>push</em> and <em>pop</em>
* operations, along with methods for peeking at the top item, testing if the
* stack is empty, and iterating through the items in LIFO order.
* <p>
* All stack operations except iteration are constant time.
* <p>
* For additional documentation, see <a href="/algs4/13stacks">Section 1.3</a>
* of <i>Algorithms in Java, 4th Edition</i> by Robert Sedgewick and Kevin
* Wayne.
*/
public class Stack<Item> implements Iterable<Item> {
private int N; // size of the stack
private Node first; // top of stack
// helper linked list class
private class Node {
private Item item;
private Node next;
}
/**
* Create an empty stack.
*/
public Stack() {
setFirst(null);
N = 0;
}
/**
* Is the stack empty?
*/
public boolean isEmpty() {
return getFirst() == null;
}
/**
* Return the number of items in the stack.
*/
public int size() {
return N;
}
/**
* Add the item to the stack.
*/
public void push(Item item) {
Node oldfirst = getFirst();
setFirst(new Node());
getFirst().item = item;
getFirst().next = oldfirst;
N++;
}
/**
* Delete and return the item most recently added to the stack. Throw an
* exception if no such item exists because the stack is empty.
*/
public Item pop() {
if (isEmpty())
throw new RuntimeException("Stack underflow");
Item item = getFirst().item; // save item to return
setFirst(getFirst().next); // delete first node
N--;
return item; // return the saved item
}
/**
* Return the item most recently added to the stack. Throw an exception if
* no such item exists because the stack is empty.
*/
public Item peek() {
if (isEmpty())
throw new RuntimeException("Stack underflow");
return getFirst().item;
}
/**
* Return string representation.
*/
public String toString() {
StringBuilder s = new StringBuilder();
for (Item item : this)
s.append(item + " ");
return s.toString();
}
/**
* Return an iterator to the stack that iterates through the items in LIFO
* order.
*/
public Iterator<Item> iterator() {
return new LIFOIterator();
}
// an iterator, doesn't implement remove() since it's optional
private class LIFOIterator implements Iterator<Item> {
private Node current = getFirst();
public boolean hasNext() {
return current != null;
}
public void remove() {
throw new UnsupportedOperationException();
}
public Item next() {
if (!hasNext())
throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
}
}
/**
* A test client.
*/
public static void main(String[] args) {
/***********************************************
* A stack of strings
***********************************************/
Stack<String> s1 = new Stack<String>();
s1.push("Vertigo");
s1.push("Just Lose It");
s1.push("Pieces of Me");
System.out.println(s1.pop());
s1.push("Drop It Like It's Hot");
while (!s1.isEmpty()) {
System.out.println(s1.pop());
}
System.out.println();
/*********************************************************
* A stack of integers. Illustrates autoboxing and auto-unboxing.
*********************************************************/
Stack<Integer> s2 = new Stack<Integer>();
for (int i = 0; i < 10; i++) {
s2.push(i);
}
// test out the iterator
for (int i : s2) {
System.out.print(i + " ");
}
System.out.println();
while (s2.size() >= 2) {
int a = s2.pop();
int b = s2.pop();
int c = a + b;
System.out.println(c);
s2.push(a + b);
}
}
public void setFirst(Node first) {
this.first = first;
}
public Node getFirst() {
return first;
}
}