Upload
damian-copeland
View
220
Download
0
Embed Size (px)
DESCRIPTION
Linear Interface
Citation preview
CSCI 62Data Structures
Dr. Joshua StoughOctober 7, 2008
Today• Linear Structures
– Grow and Shrink in predetermined manner.
– Stacks – Last In – First Out – Queues – First In – First Out
• http://www.cs.williams.edu/JavaStructures/Documentation.html
Linear Interface
AbstractLinear
Stacks• Like a stack of paper (or cafeteria trays)
– “last-in first-out” (LIFO) • can only add to the top - push• can only remove from the top - pop
• Why?– Often used to keep track of execution in a
program• when returning from a method call, the computer
has to remember where it last was
AbstractStack• push()• pop()• peek()• That’s it!
– All the rest is complication that can be abstracted away.
Stack
Stack
topNodeNodeNodeNodeNode
Node Only need access to the top of the stack
Everything O(1)How about with Vector?
May be implemented as a linked list
push – addFirst()pop – removeFirst()peek – getFirst()
Stacks• Interesting idea
– Vector O(1) add time, sometimes O(1) sometimes O(n)• If that lack of reliability is offputing, then
need list implementation
StacksExample
10 public shuffle() {11 int ind1 = nextInt(NUM_CARDS); }
1 public static void main (String[] args) {2 deck = new Deck();3 deck.shuffle();4 System.out.println (deck); }
20 public int nextInt (int num) {21 return 0; }
Call Stack33
11top
Stacks used to simulate recursion• CallFrame• CallStack to organize the CallFrames
• CallFrame contains– Local variables– Method parameters– Program counter – declaring location
within routine
Recursive QuickSort
Iterative QuickSort CallFrame
Iterative QuickSort
Stack Question• Suppose you wish to fill a stack
with a copy of another, maintaining the order of elements. Using only Stack operations, describe how this would be done. How many additional stacks are necessary?
• public static void copy(Stack s, Stack t)
Stack copy answer
Stack Question• Suppose you wish to reverse the
order of elements of a stack. Using only Stack operations, describe how this would be done. Assuming you place the result in the original stack, how many additional stacks are necessary?
• public static void reverse(Stack s)
Stack: as List or Vector.• What’s required of a Stack?
– Add to top, remove from top.
• As a List– Complexity of push, peek, pop?
• As a Vector– Complexity of push, peek, pop?
Post-Fix Notation• 6 4 + (10)• 3 2 – 4 + (5)• 1 2 3 4 5 6 + + + + + (21)
• Quick: Backus-Naur Form• E := L | E E O• O := + | - | / | * | ^ | %• L := some number
Solving Postfix Notation using a Stack• The expression to evaluate is
either a L or a E. • Push all the tokens onto the stack:• Take the top:
– If L, then we know the value.– If O,
• Get two more E off the stack.• Perform the appropriate operation and
return that value.
Queues• Standing in line
– “first-in first-out” (FIFO)• add to the “tail” of the list (back of line) - enqueue• remove from the “head” (head of line) - dequeue
• Why?– often used to keep things in the order that
they arrived– processes to be scheduled on the CPU– packets arriving to a router
Queue – as Linked List – using a Linked
ListQueue
headNodeNodeNodeNodeNode
Node Only have access to the head and tail of the queue
May be implemented as a linked list
-add to tail-remove from head-SinglyLinked with tail would be fine.
tail
Queue – as a Vector• Add elements to the back
– Complexity?
• Get elements from the front– Complexity?
• Cost of adding elements?
Queue – as Array• Keep ints for head and count• Use % to keep track of head upon
remove
Stack vs Queues• Stacks depth-first,
• Queues breadth-first.