Linear Structures - Lecture Slides | CS 200, Study notes of Computer Science

Material Type: Notes; Professor: Howe; Class: Algorithms and Data Structures; Subject: Computer Science; University: Colorado State University; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 03/18/2009

koofers-user-05u
koofers-user-05u 🇺🇸

10 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS200: Linear Structures
Stacks: Walls Ch. 7
Queues: Walls Ch. 8
Linear, time-ordered structures
Data structures that reflect a temporal
relationship
order of removal based on order of insertion
e.g.
“first come,first serve”
first in first out - FIFO
“take from the top of the pile”
last in first out - LIFO
Stacks
Last In First Out (LIFO) structure
e.g.
a pile of papers
a stack of plates in a cafe
Add/Remove done from same end of
structure
both from top – head of list in a typical
implementation
5
4
3
2
1
top
Stack ADT
Create
Query
Empty?
Retrieve -- get first item
Add
Push -- add with constraint on position
Remove
Pop -- remove with constraints
Remove all
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Linear Structures - Lecture Slides | CS 200 and more Study notes Computer Science in PDF only on Docsity!

CS200: Linear Structures

 Stacks: Walls Ch. 7

 Queues: Walls Ch. 8

Linear, time-ordered structures

 Data structures that reflect a temporal

relationship

 order of removal based on order of insertion

 e.g.

 “first come,first serve”  first in first out - FIFO  “take from the top of the pile”  last in first out - LIFO

Stacks

 Last In First Out (LIFO) structure

 e.g.

 a pile of papers  a stack of plates in a cafe

 Add/Remove done from same end of

structure

 both from top – head of list in a typical implementation

top

Stack ADT

 Create

 Query

 Empty?  Retrieve -- get first item

 Add

 Push -- add with constraint on position

 Remove

 Pop -- remove with constraints  Remove all

Stack Methods

push(in newItem:StackItemType) throws StackException

 adds a new item to the top of the stack  Exception when insertion fails

pop():StackItemType throws StackException

 deletes the top item from the stack and returns it  Exception when deletion fails

peek():StackItemType {query} throws StackException

 returns the top item from the stack, but does not remove it  Exception when retrieval fails Stack Application - Runtime Stack

 Recursive invocations tracked on call stack

 First method that returns is the last one invoked which returns to the second to the last one…

 Element of call stack - call frame aka

activation record

 parameters and return values  local variables  return address: pointer to next instruction to be executed in calling method Stack Application - Depth First Search

 Consider looking for

a path in a maze

 Strategy: keep

turning right, straight

or left (if others

aren’t options) until

get to dead end or

goal, backtrack at

dead ends and try

different turn

Stack application - Prefix expressions  How is this implemented?

 In our recursive descent parser?

 In Java, stacks are used in Java Virtual

Machine for call mechanism and

expression evaluation

Stack Implementation - Linked List (cont.) public Object pop() throws StackException { if (!isEmpty()) { Node temp = top; top = top.getNext(); return temp.getItem(); } else { throw new StackException(…); } } Stack Implementation – Singly Linked List

 Push

 use add(1) [ADT List]  worst case complexity: O(1)

 Pop

 use remove(1) [ADT List]  worst case complexity: O(1)

 Peek

 use get(1) [ADT List]

 Why not use add/remove data from the tail?

Stack API in Java public class Stack extends Vector The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top. [from java.sun.com]  Problem? Queues

 First In First Out (FIFO) structure

 Imagine a checkout line

 So removing and adding are done from opposite

ends of structure.

 add to tail, remove from head (typical)

 Used in operating systems for jobs done in one

go (e.g., print jobs, batch jobs). Which structure

can be used for time-sliced jobs?

Queue Methods

enqueue(in newItem:QueueItemType)

 adds a new item to the tail of the queue  Throws exception if not successful

dequeue():QueueItemType

 deletes the first item from the head of the queue and returns it  Throws exception if not successful

peek():QueueItemType

 returns the first item from the head of the queue, but does not remove it  Throws exception if not successful Queue Implementation - Using ArrayList

 Enqueue

 use add - adds to the end of the ArrayList

 Dequeue

 use get(0) to get return value  use remove(0) to destructively modify the queue

 Peek

 use get(0)

 Problem - how much time is required?

Naïve Array-Based Implementation Drift can cause the queue to appear full How do we initialize front and back? (Hint: how does a queue with a single element look like) Queue Implementation - Using Arrays

 Use wrap-around array structure

 number of elements in queue  head index

 enqueue

 add to next open position  increment count

 dequeue

 remove from head by incrementing head  decrement count

 peek

 use head index

Determining Full in Array-Based Queue back catches up to front when the queue becomes full

Solution: count # of items

Other Approaches

 Use a flag full to distinguish between the

full and empty conditions

 Declare MAX_QUEUE + 1 locations for the

array items, but use only MAX_QUEUE of them

for queue items

Queue Implementation - Using DLL  DLL is symmetric two choices: enqueue at head or tail  Enqueue  use add(0) method  Dequeue  use remove method at size  Peek  use get method  How can we use SLL without losing on order of complexity? Reference-Based Queue Implementation A linked list with two external references

 A reference to the front

 A reference to the back

At which end do we dequeue?

Reference-Based Queue Implementation II A circular linked list with one external reference

 lastNode references the back of the queue

 lastNode.getNext() references the front

Reference-Based Queue II: Enqueue Enqueueing an item into a non-empty queue Reference-Based Queue II: Enqueue (cont.) Enqueueing an item into an empty queue Reference-Based Implementation II: Dequeue

Dequeueing an item from a queue of more than one item