Todays announcements: PA1 due Sep 28, 23:59 Todays Plan Stacks and - - PowerPoint PPT Presentation

today s announcements
SMART_READER_LITE
LIVE PREVIEW

Todays announcements: PA1 due Sep 28, 23:59 Todays Plan Stacks and - - PowerPoint PPT Presentation

Todays announcements: PA1 due Sep 28, 23:59 Todays Plan Stacks and Queues F B C D E F E D C enqueue dequeue B G F E D C B A A A LIFO: Last In First Out FIFO: First In First Out Stack Queue 1 / 10 Stack: Array


slide-1
SLIDE 1

Today’s announcements:

◮ PA1 due Sep 28, 23:59

Today’s Plan

◮ Stacks and Queues A B C D E A F B C D E F F E D C B enqueue G dequeue A

LIFO: Last In First Out FIFO: First In First Out Stack Queue

1 / 10

slide-2
SLIDE 2

Stack: Array implementation

size − 1 a S b c d e top = 5 5 class Stack{ public: Stack(); void push(const LIT & e); LIT pop(); bool isEmpty() const; private: LIT * S; int size; int top; }; Runtimes? void Stack<LIT>:: push(const LIT & e) { if( isFull() ) resize(); S[top] = e; top++; } LIT Stack<LIT>::pop() { assert(!isEmpty()); top--; return S[top]; }

2 / 10

slide-3
SLIDE 3

Stack: Array implementation resize

Resize If array S is full, create a larger array and copy existing data into it.

a new S b c d e S a b c d e

How much time does it take to resize from size x to size y? How big should the new array be?

◮ The original size PLUS a constant? ◮ The original size TIMES a constant? ◮ Something else???

3 / 10

slide-4
SLIDE 4

Resize: Original PLUS a constant

Original size = c Increase size by +c when full How much time to perform n pushes into an empty stack? Number of resizes = ith resize cost = total resize cost =

4 / 10

slide-5
SLIDE 5

Resize: Original TIMES a constant

Original size = c Increase size by ×c when full How much time to perform n pushes into an empty stack? Number of resizes = ith resize cost = total resize cost =

5 / 10

slide-6
SLIDE 6

Stack Summary

Linked list implementation

Constant time push and pop

Array implementation

Constant time pop Constant time push (until full) Cost over n pushes is O(n) (average O(1) per push)

Why use an array?

6 / 10

slide-7
SLIDE 7

Queue Abstract Data Type

enqueue, dequeue, isEmpty, ... template<class LIT> class Queue{ public: Queue(); // {~Queue(), copy, op=}? void enqueue(const LIT & e); LIT dequeue(); bool isEmpty() const; private: ??? }; enqueue(1) enqueue(2) enqueue(3) dequeue() dequeue() enqueue(4) dequeue() enqueue(5) dequeue() dequeue() Why a queue?

◮ Hold jobs for a printer ◮ Store packets on network routers ◮ Make waitlists fair ◮ Depth first search

7 / 10

slide-8
SLIDE 8

Queue: Linked list implementation

b c d e f ∅

Which pointer is front and which is back?

class Queue{ public: Queue(); void enqueue(const LIT & e); LIT dequeue(); bool isEmpty() const; private: struct Node { LIT data; Node * next;}; Node * front, * back; int size; }; Runtimes? LIT Queue<LIT>::dequeue() { assert(!isEmpty()); LIT ret =

  • >data;

Node *p = ; =

  • >next;

delete p; return ret; } void Queue<LIT>:: enqueue(const LIT & e) { if (isEmpty()) front = back = new Node(e); else =

  • >next=new Node(e);

}

8 / 10

slide-9
SLIDE 9

Queue: Circular array implementation

size − 1 a Q b c d e front = 7 7 back = 12 12 class Queue{ public: Queue(); void enqueue(const LIT & e); LIT dequeue(); bool isEmpty() const; private: LIT * Q; int size; int front, back; }; Runtimes? LIT Queue<LIT>::dequeue() { LIT e = Q[front]; front = (front + 1) % size; return e; } void Queue<LIT>:: enqueue(const LIT & e) { if( isFull() ) resize(); Q[back] = e; back = (back + 1) % size; } bool Queue<LIT>::isFull() const { return( front == (back + 1) % size ); }

9 / 10

slide-10
SLIDE 10

Queue: Array implementation resize

Resize If array Q is full, create a larger array and copy existing data into it.

a b c d e 3 f g 2 7 15 b a c k = f r

  • n

t =

Where to copy?

10 / 10