• No results found

Data Structures and Algorithms Stacks and Queues

N/A
N/A
Protected

Academic year: 2021

Share "Data Structures and Algorithms Stacks and Queues"

Copied!
23
0
0

Loading.... (view fulltext now)

Full text

(1)

Data Structures and Algorithms

Stacks and Queues

Chris Brooks

Department of Computer Science University of San Francisco

(2)

6-0:

Stacks and Queues

Stacks and queues are two of the most common data structures

Based on the List, but with more limited access to elements.

(3)

6-1:

Stack

A Stack is a Last-In, First-Out (LIFO) data structure. Like a stack of trays in a cafeteria.

Stack Operations:

Add an element to the top of the stack (push) Remove the top element (pop)

Look at the top element (peek) Check if the stack is empty

(4)

6-2:

Uses of a stack

Postfix notation calculator 3 4 + -> 7

3 4 + 5 - -> 2

Parenthesis checker Recursion

(5)

6-3:

Stack Implementation

Array:

Like the list, we allocate memory in advance. Keep a pointer for the ’top’ of the stack.

(6)

6-4:

Stack Implementation

Array:

Stack elements are stored in an array Top of the stack is the end of the array

If the top of the stack was the beginning of the array, a push or pop would require moving all elements in the array

Push: data[top++] = elem Pop: elem = data[--top]

(7)

6-5:

Θ()

For Stack Operations

Array Implementation: push

pop

empty()

(8)

6-6:

Θ()

For Stack Operations

Array Implementation: push Θ(1)

pop Θ(1) empty() Θ(1)

Pros: fast, easy to implement

(9)

6-7:

Stack Implementation

Linked List:

(10)

6-8:

Stack Implementation

Linked List:

Stack elements are stored in a linked list Top of the stack is the front of the linked list push: top = new Link(elem, top)

(11)

6-9:

Θ()

For Stack Operations

Linked List Implementation: push

pop

empty()

(12)

6-10:

Θ()

For Stack Operations

Linked List Implementation: push Θ(1)

pop Θ(1) empty() Θ(1)

Pros: Fast, can grow/shrink dynamically,

(13)

6-11:

Queue

A Queue is a Last-In, First-Out (FIFO) data structure. Like a line at the post office or grocery store.

Queue Operations:

Add an element to the end (tail) of the Queue (enqueue) Remove an element from the front (head) of the Queue (dequeue)

Check the item at the front of the queue (peek) Check if the Queue is empty

(14)

6-12:

Uses of a queue

Scheduling

You’ll get lots of experience with queues in OS Searching (web crawling)

Handling requests Print queues

(15)

6-13:

Queue Implementation

Linked List:

(16)

6-14:

Queue Implementation

Linked List:

Maintain a pointer to the first and last element in the Linked List

Add elements to the back of the Linked List

Remove elements from the front of the linked list Enqueue: tail.setNext(new link(elem,null));

tail = tail.next()

(17)

6-15:

Θ()

For Queue Operations

Linked List Implementation: enqueue Θ(1)

dequeue Θ(1) empty() Θ(1)

Pros: Fast, can grow/shrink dynamically,

Cons: Allocating/freeing memory may be costly.

(18)

6-16:

Queue Implementation

Array:

Can’t just use a flat array, as with a stack. Queue will wind up ’sliding up’ the array.

(19)

6-17:

Queue Implementation

Array:

Store queue elements in a circular array

Maintain the index of the first element (head) and the next location to be inserted (tail)

Enqueue: data[tail] = elem;

tail = (tail + 1) % size Dequeue: elem = data[head];

head = (head + 1) % size

(20)

6-18:

Queue Implementation

Array Issues:

What do head and tail look like when the queue is empty? What about when there is one element in the queue?

(21)

6-19:

Θ()

For Queue Operations

Array Implementation: enqueue Θ(1) dequeue Θ(1) empty() Θ(1) Pros: Fast

Cons: Issues with full vs. empty, fixed size

(22)

6-20:

Modifying Stacks

“Minimum Stacks” have one additional operation:

minimum: return the minimum value stored in the stack Can you implement a O(n) minimum?

(23)

6-21:

Modifying Stacks

“Minimum Stacks” have one additional operation:

minimum: return the minimum value stored in the stack Can you implement a O(n) minimum?

Can you implement a Θ(1) minimum?

push, pop must remain Θ(1) as well!

References

Related documents

The key segments in the mattress industry in India are; Natural latex foam, Memory foam, PU foam, Inner spring and Rubberized coir.. Natural Latex mattresses are

4.1 The Select Committee is asked to consider the proposed development of the Customer Service Function, the recommended service delivery option and the investment required8. It

National Conference on Technical Vocational Education, Training and Skills Development: A Roadmap for Empowerment (Dec. 2008): Ministry of Human Resource Development, Department

However, obtaining bacterial genomic information is not always trivial: the target bacteria may be difficult-to-culture or uncultured, and may be found within samples containing

Based on this new expression for Ca c , a three-regime theory is formulated to describe the interface (in)stability: (i) in Regime I, the growth rate is always negative, thus the

The corona radiata consists of one or more layers of follicular cells that surround the zona pellucida, the polar body, and the secondary oocyte.. The corona radiata is dispersed

– Acid activation forms chlorine dioxide in batch Acid activation forms chlorine dioxide in

The threshold into the stadium is through a series of layers which delaminate from the geometry of the field to the geometry of the city and creates zones of separation,