-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.java
51 lines (40 loc) · 1002 Bytes
/
Stack.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
package evalExpr;
import java.util.Iterator;
import java.util.NoSuchElementException;
public interface Stack<T> extends Iterable<T>{
default int size() {
int c=0;
for( Iterator<T> it= iterator(); it.hasNext(); it.next(), c++ );
return c;
}//size
default void clear() {
Iterator<T> it=iterator();
while( it.hasNext() ) {
it.next(); it.remove();
}
}//clear
default boolean contains( T e ) {
for( T x: this )
if( x.equals(e) ) return true;
return false;
}//contains
void push( T e );
default T pop() {
Iterator<T> it=iterator();
if( !it.hasNext() ) throw new NoSuchElementException();
T x=it.next();
it.remove();
return x;
}//pop
default T top() {
Iterator<T> it=iterator();
if( !it.hasNext() ) throw new NoSuchElementException();
return it.next();
}//top
default T peek() {
return top();
}//peek
default boolean isEmpty() {
return !iterator().hasNext();
}//isEmpty
}//Stack