05 Stacksandqueues PDF
05 Stacksandqueues PDF
An Abstract Data Type (ADT) represents a particular Some common ADTs that all trained programmers
set of behaviours. know about:
! You can formally define (i.e., using mathematical ! stack, queue, priority queue, dictionary, sequence,
logic) what an ADT is/does. set
e.g., a Stack is a list implements a LIFO policy on Some common data structures used to implement those
additions/deletions. ADTS:
A data structure is more concrete. Typically, it is a
! array, linked list, hash table (open, closed, circular
technique or strategy for implementing an ADT.
hashing)
! Use a linked list or an array to implement a stack ! trees (binary search trees, heaps, AVL trees, 2-3
class.
trees, tries, red/black trees, B-trees)
& %& %
Going one level lower, we get into particulars of
programming languages and libraries We’ll discuss these is some detail.
' $' $
CS211 — DATA S TRUCTURES A NDADT S 1 CS211 — DATA S TRUCTURES A NDADT S 2
& %& %
System.err.println ("Sorry, stack is full.");
g lifetime.
g
.
.
Yes, all resources are ultimately finite. But we would
.
like a little more flexibility and reasonable use of
g
resources.
! Sure we will run out of space eventually if we keep The stack is a set of nodes linked together.
adding elements, but only if we really have to, i.e.,
! Each node has a “value” plus a link variable (a
only if the stack gets really huge.
reference to another node).
! What are the tradeoffs? ! The “value” could be a simple value (e.g., an int),
Are there any disadvantages? a set of values, or a reference to another object
Dyanamically allocated structures can grow and shrink (e.g., an employee record).
as needed throughout their lifetime. When you want to push a new value:
& %& %
1. Create a new node via new.
allocation. 2. Set the value of the new node.
3. Set its link field to point to the previous top node.
4. Reset top to point to the new node.
' $' $
CS211 — S TACKS A ND Q UEUES 3 CS211 — S TACKS A ND Q UEUES 4
& %& %
return numElements;
g g
& %& %
g else f
System.err.println ("Sorry, stack is full.");
g
g
' $' $
CS211 — S TACKS A ND Q UEUES 7 CS211 — S TACKS A ND Q UEUES 8
& %& %
g
top
Keep a linked list of arrays (NodeChunks):
! Each NodeChunk has a reference to the one before
it.
! Keep a reference to the “top chunk”, plus a top
integer that points to the top element in the top
& %& %
chunk.
' $' $
CS211 — S TACKS A ND Q UEUES 11 CS211 — S TACKS A ND Q UEUES 12
& %& %
public abstract int size ();
g ! Of course, this static approach means you are limited to
at most N elements in your queue at any given moment.
far
3 public ArrayQueue () f
this.maxSize = DefaultMaxSize;
4
?? store = new Object[maxSize];
g
...
& %& %
System.err.println ("Sorry, queue is full.");
g
[Assumes there is space, i.e., element 0 is empty]
g
! Items in the range first ... last are full.
' $' $
CS211 — S TACKS A ND Q UEUES 15 CS211 — S TACKS A ND Q UEUES 16
& %& %
g q1.enter ("far");
q1.enter ("sew");
public boolean isEmpty () f q1.print();
return size() == 0; System.out.println ("Removed " + q1.leave());
g g
g
& %& %
}
! Otherwise reset last.next to new node.
return returnVal;
}
Can also implement using a NodeChunk
[Each NodeChunk contains an array of values.] // Obvious definitions for isEmpty and size omitted.
}
' $' $
CS211 — S TACKS A ND Q UEUES 19 CS211 — S TACKS A ND Q UEUES 20
This program:
class FigureQueueTest {
public static void main (String[] args) {
Queue q = new LinkedQueue();
Circle c1, c2;
Misc. Notes c1 = new Circle();
c1.setSize(50);
c1.setLoc(25,30);
We have decided to declare the Node fields value and c2 = new Circle();
Square s1 = new Square();
next as package-level visible. s1.setSize (75);
q.enter(c1);
! This means that other classes in the same package, q.enter(c2);
q.enter(s1);
such as LinkedQueue, can directly manipulate the
while (!q.isEmpty()) {
instance variables of each Node instance. Figure f = (Figure) q.leave();
f.draw();
Also, we can use more interesting objects than Strings }
and Integers in our stacks and queues. }
}
& %& %
Circle at x = 25 y = 30 with radius = 50
Circle at x = 0 y = 0 with radius = 0
Square at x = 0 y = 0 with width = 75 and height = 75