CS200 - Data Structures
Download
Report
Transcript CS200 - Data Structures
CS2006 - Data Structures I
Chapter 7
Stacks II
Topics
Implementation of ADT Stack
2
Using Linked lists
Using ADT List
Linked List Stack
Implementation
Stacks can grow and shrink dynamically
No particular linked list variations are required:
3
The head pointer can also serve as the top of the
stack.
Pushing becomes inserting the first node in a linked
list.
Popping becomes removing the first node.
Linked List Stack
Implementation
The head of List is the top of the stack
head
1
top
3
7
1
3
7
9
4
9
LL Stack Implementation
// Assumes that the classes StackInterface and Node are available
public class StackRefereenceBased implements StackInterface {
private Node top;
// class Node is the same used in the LinkedList class except it stores
// Objects rather than Comparables
public StackRefereenceBased (){
top = null;
} // end default constructor
public boolean isEmpty() {
return (top == null);
} // end isEmpty
5
LL Stack Implementation
// Assumes that the classes StackInterface and Node are available
public class StackRefereenceBased implements StackInterface {
private Node top;
// class Node is the same used in the LinkedList class except it stores
// Objects rather than Comparables
public StackRefereenceBased (){
top = null;
} // end default constructor
public boolean isEmpty() {
return (top == null);
} // end isEmpty
6
LL Stack Implementation (2)
public void push(Object newItem) {
top = new Node(newItem, top);
} // end push
public Object pop() throws StackException {
if (!isEmpty()) {
Object temp = top.getItem();
top = top.getNext();
return temp;
}
else {
throw new StackException("StackException on " + "pop: stack empty");
} // end if
} // end pop
7
LL Stack Implementation (2)
public void push(Object newItem) {
top = new Node(newItem, top);
} // end push
public Object pop() throws StackException {
if (!isEmpty()) {
Object temp = top.getItem();
top = top.getNext();
return temp;
}
else {
throw new StackException("StackException on " + "pop: stack empty");
} // end if
} // end pop
8
LL Stack Implementation (3)
public void popAll() {
top=null;
} // end popAll
public Object peek() throws StackException {
if (!isEmpty()) {
return top.getItem();
}
else {
throw new StackException("StackException on " +
"peek: stack empty");
} // end else
} // end peek
} // end StackReferencedBased
9
LL Stack Implementation (3)
public void popAll() {
top=null;
} // end popAll
public Object peek() throws StackException {
if (!isEmpty()) {
return top.getItem();
}
else {
throw new StackException("StackException on " +
"peek: stack empty");
} // end else
} // end peek
} // end StackReferencedBased
10
ADT Stack ADT List Implementation
11
All stack operations can be implemented
using the ADT List's operations
The implementation is simpler to write
than the array-based and makes used of
previously-written code (code reuse)
Position 1 of the list represents the top
position of the stack
ADT Stack ADT List Implementation
List position 1
10
2
80
3
60
List site()
12
.
.
5
top
ADT List Stack Implementation
// Assumes that the classes ListInterface and Node are available
public class StackListBased implements StackInterface {
private ListInterface list;
public StackListBased() {
list = new ListReferenceBased();
} // end default constructor
public boolean isEmpty() {
return list.isEmpty ();
} // end isEmpty
13
ADT List Stack Implementation
// Assumes that the classes ListInterface and Node are available
public class StackListBased implements StackInterface {
private ListInterface list;
public StackListBased() {
list = new ListReferenceBased();
} // end default constructor
public boolean isEmpty() {
return list.isEmpty ();
} // end isEmpty
14
List Stack Implementation (2)
public void push(Object newItem) {
list.add (1, newItem);
} // end push
public Object pop() throws StackException {
if (!list.isEmpty()) {
Object temp = list.get(1);
list.remove(1);
return temp;
}
else {
throw new StackException("StackException on " + "pop: stack empty");
} // end if
} // end pop
15
List Stack Implementation (2)
public void push(Object newItem) {
list.add (1, newItem);
} // end push
public Object pop() throws StackException {
if (!list.isEmpty()) {
Object temp = list.get(1);
list.remove(1);
return temp;
}
else {
throw new StackException("StackException on " + "pop: stack empty");
} // end if
} // end pop
16
List Stack Implementation (3)
public void popAll() {
list.removeAll();
} // end popAll
public Object peek() throws StackException {
if (!list.isEmpty()) {
return list.get(1);
}
else {
throw new StackException("StackException on " +
"peek: stack empty");
} // end else
} // end peek
} // end StackListBased
17
ADT Stack Implementation Comparison
Array-Based
LL-Based
More complicated than ADT List
Most flexible, No size restrictions
ADT List-Based
18
No overhead of reference manipulation
Prevents adding elements if the array is full
Simpler to write
Not as efficient as using a linked list directly
Review
The ______ method of the ADT stack
retrieves and then removes the top of the
stack.
19
createStack
push
pop
peek
Review
The ______ method of the ADT stack
retrieves the top of the stack, but does not
change the stack.
20
createStack
push
pop
peek
Review
Which of the following methods of the ADT
stack accepts a parameter?
21
push
pop
createStack
peek
Review
If a stack is used by an algorithm to check
for balanced braces, which of the following
is true once the end of the string is
reached?
22
the stack is empty
the stack has one “{”
the stack has one “}”
the stack has one “{” and one “}”
Review
Which of the following operations of the
ADT stack does not throw a
StackException?
23
push
pop
popAll
peek
Review
The pop operation throws a
StackException when it tries to ______.
24
add an item to an empty stack
add an item to an array-based implementation
of a stack that is already full
delete an item from an array-based
implementation of a stack that is already full
delete an item from an empty stack
Review
The push operation throws a
StackException when it tries to ______.
25
add an item to an empty stack
add an item to an array-based implementation
of a stack that is already full
delete an item from an array-based
implementation of a stack that is already full
delete an item from an empty stack
Review
In the StackInterface class, the pop
method returns an item that is an instance
of ______.
26
Integer
Double
String
Object