CMSC 132, Object-Oriented Programming II
Summer 2016
Lecture 10:
Lecturer: Anwar Mamat
Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor.
10.1
STACK
A stack is a Last-In-First-Out (LIFO) data structure. It supports push, pop operations which only operations
occur only at one end of the structure, referred to as the top of the stack, as shown in Figure 10.1.
Figure 10.1: Stack
Listing 1: Stack Interface
1
2
3
4
5
6
7
public i n t e r f a c e Stack<T> extends I t e r a b l e <T> {
void push (T t ) ;
T pop ( ) ; // t h r o w s EmptyStackException ;
T peek ( ) ; // t h r o w s EmptyStackException ;
boolean isEmpty ( ) ;
int s i z e ( ) ;
}
If we have a stack S:
1
S . push ( 1 0 ) ;
1
2
[
]
[ 1 0 ] <−− top
If we add numbers 20,30
1
2
S . push ( 2 0 ) ;
S . push ( 3 0 ) ;
10-1
10-2
1
2
3
4
Lecture 10:
[
]
[ 3 0 ] <−− top
[20]
[10]
If we pop a number:
1
S . pop ( ) ;
1
2
3
[
]
[20]<−− top
[10]
Peek returns the top item, but it does not remove the item. If we pop a number:
1
int i = S . peek ( ) ; // r e t u r n s t h e t o p item ;
1
2
3
[
]
[20]<−− top
[10]
10.1.1
Array based implementation of Stack
In this section, we use a resizable array to implement stack. Array “Items” holds the elements of the stack.
items[N] always points to an empty cel in the “Items” array. When an item is pushed, the item is stored in
items[N], and N is increments. Pop does the opposite.
Listing 2: Array Based Stack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import j a v a . u t i l . I t e r a t o r ;
import j a v a . u t i l . NoSuchElementException ;
public c l a s s ArrayStack<T> implements Stack<T> {
private T [ ] i t e m s ;
private int N; // number o f e l e m e n t s i n t h e s t a c k
public ArrayStack ( ) {
i t e m s = (T [ ] ) new Object [ 2 ] ;
N = 0;
}
private void r e s i z e ( int c a p a c i t y ) {
T [ ] temp = (T [ ] ) new Object [ c a p a c i t y ] ;
f o r ( int i =0; i < N; i ++){
temp [ i ] = i t e m s [ i ] ;
}
i t e m s = temp ;
}
public void push (T item ) {
i f (N == i t e m s . l e n g t h ) {
r e s i z e (2 ∗ items . length ) ;
Lecture 10:
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
}
i t e m s [N++] = item ;
}
public boolean isEmpty ( ) {
return N == 0 ;
}
public T pop ( ) {
i f ( isEmpty ( ) ) throw new NoSuchElementException ( ) ;
T item = i t e m s [−−N ] ;
i t e m s [N] = null ;
return item ;
}
public T peek ( ) // a l i a s t o p
{
i f ( isEmpty ( ) ) throw new NoSuchElementException ( ” Stack Underflow ” ) ;
return i t e m s [ N− 1 ] ;
}
@Override
public I t e r a t o r <T> i t e r a t o r ( ) {
return new S t a c k I t e r a t o r ( ) ;
}
@Override
public int s i z e ( ) {
return N;
}
/∗
∗
Stack i t e r a t o r
∗/
private c l a s s S t a c k I t e r a t o r implements I t e r a t o r <T>{
private int i n d e x ;
StackIterator (){
i n d e x = N;
}
@Override
public boolean hasNext ( ) {
return i n d e x > 0 ;
}
@Override
public T next ( ) {
i f ( ! hasNext ( ) ) {
throw new NoSuchElementException ( ) ;
}
return i t e m s [−− i n d e x ] ;
}
@Override
10-3
10-4
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Lecture 10:
public void remove ( ) {
throw new U ns up po rt ed Op er at i on Ex ce pt io n ( ”Not s u p p o r t e d y e t . ” ) ;
}
}
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
Stack<I n t e g e r > a s = new ArrayStack ( ) ;
f o r ( int i = 1 ; i <= 5 ; i ++){
a s . push ( i ) ;
}
while ( ! a s . isEmpty ( ) ) {
System . out . p r i n t l n ( a s . peek ( ) ) ;
System . out . p r i n t l n ( a s . peek ( ) ) ;
// System . o u t . p r i n t l n ( as . pop ( ) ) ;
}
for ( I n t e g e r i : as ){
System . out . p r i n t l n ( i ) ;
}
}
}
10.1.2
Linked List based implementation of Stack
In this section, we implement the stack using singly linked list. We always add and remove the nodes at the
head of the linked list.
Listing 3: Linked List Based Stack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import j a v a . u t i l . I t e r a t o r ;
import j a v a . u t i l . NoSuchElementException ;
public c l a s s LinkedStack<T> implements Stack<T> {
private int N;
private Node f i r s t ;
@Override
public I t e r a t o r <T> i t e r a t o r ( ) {
//TO DO
}
private c l a s s Node{
private T data ;
private Node next ;
Node (T item ) {
data = item ;
next = null ;
}
}
L in k e dS t a ck ( ) {
f i r s t = null ;
Lecture 10:
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
N = 0;
}
public void push (T item ) {
Node t = new Node ( item ) ;
Node o l d = f i r s t ;
first = t;
f i r s t . next = o l d ;
N++;
}
public boolean isEmpty ( ) {
return f i r s t == null ;
}
public T pop ( ) {
i f ( isEmpty ( ) ) {
throw new NoSuchElementException ( ) ;
}
T item = f i r s t . data ;
f i r s t = f i r s t . next ;
N−−;
return item ;
}
public int s i z e ( ) {
return N;
}
public T peek ( ) {
i f ( isEmpty ( ) ) {
throw new NoSuchElementException ( ) ;
}
return f i r s t . data ;
}
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
LinkedStack<I n t e g e r > l s = new L i nk e d St a c k ( ) ;
f o r ( int i = 1 ; i <= 7 ; i ++){
l s . push ( i ) ;
}
System . out . p r i n t l n ( ” s i z e : ” + l s . s i z e ( ) ) ;
System . out . p r i n t l n ( ”\n” ) ;
while ( ! l s . isEmpty ( ) ) {
System . out . p r i n t ( l s . peek ()+ ” , ” ) ;
System . out . p r i n t ( l s . pop ()+ ” , ” ) ;
}
}
}
10-5
© Copyright 2026 Paperzz