Stack / Queue ADT Stack ADT Implementations Array resizing Queue - - PowerPoint PPT Presentation

stack queue adt
SMART_READER_LITE
LIVE PREVIEW

Stack / Queue ADT Stack ADT Implementations Array resizing Queue - - PowerPoint PPT Presentation

Stack / Queue ADT Stack ADT Implementations Array resizing Queue ADT January 27, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1 Announcements PA1 GradeScope being configured, will be available soon January 27, 2020 Cinda Heeren / Andy


slide-1
SLIDE 1

Stack / Queue ADT

Stack ADT Implementations Array resizing Queue ADT

January 27, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1

slide-2
SLIDE 2

Announcements

  • PA1 GradeScope being configured, will be available soon

January 27, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 2

slide-3
SLIDE 3

Stacks in computing

  • Program execution & function calling
  • Postfix evaluation

– e.g. 5 1 2 + 4 * + 3 – – Place numbers in a stack, apply operator to last two numbers

  • My e-mail inbox?

January 27, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 3

slide-4
SLIDE 4

The stack ADT

  • A stack only allows items to be inserted and removed at one

end

– We call this end the top of the stack – The other end is called the bottom

  • Access to other items in the stack is not allowed
  • A stack can be used to naturally store data for postfix notation

– Operands are stored at the top of the stack – And removed from the top of the stack

  • Notice that we have not (yet) discussed how a stack should be

implemented

– Just what it does

  • An example of an Abstract Data Type

January 27, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 4

slide-5
SLIDE 5

Stack behaviour

  • A stack ADT should support at least the first two of these
  • perations:

– push – insert an item at the top of the stack – pop – remove and return the top item – peek – return the top item – is_empty – does the stack contain any items

  • ADT operations should be performed efficiently

– The definition of efficiency varies from ADT to ADT – The order of the items in a stack is based solely on the order in which they arrive

January 27, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 5

Must also have constructor(s) and destructor

slide-6
SLIDE 6

Stack implementation

  • The stack ADT can be implemented using a variety of data

structures, e.g.

– Linked lists – Arrays

  • Both implementations must implement all the stack operations

– In constant time (time that is independent of the number of items in the stack)

January 27, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 6

slide-7
SLIDE 7

Stack implementation

  • Recall linked list construction from previous lessons

– New nodes added at the “null” end of the list – Or inserted anywhere in the list

  • Implement a linked-list stack by adding/removing from the

front of the list

January 27, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 7

Using a linked list

34 a 52 27 18 34 52 27 18 a

slide-8
SLIDE 8

Stack implementation

  • We need to keep track of the index that represents the top of

the stack

– When we insert an item increment this index – When we delete an item decrement this index

  • Insertion or deletion time is independent of the number of

items in the stack

January 27, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 8

Using arrays

slide-9
SLIDE 9

Array stack example

January 27, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 9

1 2 3 4 5

index of top is current size – 1

Stack st(); st.push(6); //top = 0 st.push(1); //top = 1 st.push(7); //top = 2 st.push(8); //top = 3 st.pop(); //top = 2

6 1 7 8

slide-10
SLIDE 10

Array stack implementation summary

  • Easy to implement a stack with a (dynamic) array

– And push and pop can be performed in constant time

  • Once the array is full

– No new values can be inserted

  • r

– A new, larger, array can be created

  • And the existing items copied to this new array
  • This will take linear time, but should occur only rarely

January 27, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 10

slide-11
SLIDE 11

Running time considerations

  • Linked list implementation

– push and pop simply call insert or remove from front of list – All operations 𝑃 1

  • Array implementation

– push to full array requires 𝑃 𝑜 resize – resize by a constant factor (e.g. capacity = 2 * capacity;) leads to 𝑃 1 average cost per operation – resize by a constant amount (e.g. capacity = capacity + 500;) leads to 𝑃 𝑜 average cost per operation

  • Cache performance

– array implementation has superior cache performance

January 27, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 11

slide-12
SLIDE 12

Queues

January 27, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 12

slide-13
SLIDE 13

Queues

  • In a queue items are inserted at the back (end/tail) and

removed from the front (head)

  • Queues are FIFO (First In First Out) data structures – fair data

structures

  • Applications include:

– Server requests

  • Instant messaging servers queue up incoming messages
  • Database requests

– Print queues – Operating systems often use queues to schedule CPU jobs – The waiting list for this course! (it’s presumably fair) – Various algorithm implementations

Cinda Heeren / Andy Roth / Geoffrey Tien 13 January 27, 2020

slide-14
SLIDE 14

Queue operations

  • A queue should implement at least the first two of these
  • perations:

– enqueue – insert item at the back of the queue – dequeue – remove an item from the front – peek – return the item at the front of the queue without removing it – isEmpty – check if the queue does not contain any items

  • Like stacks, it is assumed that these operations will be

implemented efficiently

– That is, in constant time

Cinda Heeren / Andy Roth / Geoffrey Tien 14 January 27, 2020

slide-15
SLIDE 15

Queue implementation

  • Consider using an array as the underlying structure for a

queue, we could

– Make the back of the queue the current size of the array, much like the stack implementation – Initially make the front of the queue index 0 – Inserting an item is easy

  • What to do when items are removed?

– Either move all remaining items down – slow – Or increment the front index – wastes space

Cinda Heeren / Andy Roth / Geoffrey Tien 15

Using an array

January 27, 2020

slide-16
SLIDE 16

Circular arrays

  • Trick: use a circular array to insert and remove items from a

queue in constant time

  • The idea of a circular array is that the end of the array “wraps

around” to the start of the array

Cinda Heeren / Andy Roth / Geoffrey Tien 16

1 3 2 4 5 6 7 1 2 3 4 5 6 7

January 27, 2020

slide-17
SLIDE 17

The modulo operator

  • The mod operator (%) calculates remainders:

– 1%5 = 1, 2%5 = 2, 5%5 = 0, 8%5 = 3

  • The mod operator can be used to calculate the front and back

positions in a circular array

– Thereby avoiding comparisons to the array size – The back of the queue is:

  • (front + num) % arrlength
  • where num is the number of items in the queue

– After removing an item, the front of the queue is:

  • (front + 1) % arrlength

Cinda Heeren / Andy Roth / Geoffrey Tien 17 January 27, 2020

Member attributes: int front; int arrlength; int* arr; int num;

slide-18
SLIDE 18

Array queue example

Cinda Heeren / Andy Roth / Geoffrey Tien 18

Queue q;

front num

q.enqueue(6);

Insert item at (front + num) % queue.length, then increment num 1 1 2 3 4 5

January 27, 2020

6

slide-19
SLIDE 19

Array queue example

Cinda Heeren / Andy Roth / Geoffrey Tien 19

Queue q;

front num

q.enqueue(6);

Insert item at (front + num) % queue.length, then increment num 6 5

q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8);

4 7 3 8

q.dequeue();

Remove item at front, then decrement num and make front = (front + 1) % queue.length 4 1

q.dequeue();

3 2 1 2 3 4 5

January 27, 2020

slide-20
SLIDE 20

Array queue example

Cinda Heeren / Andy Roth / Geoffrey Tien 20

front num 2 Insert item at (front + num) % queue.length, then increment num 5 3 7 3 8 Remove item at front, then decrement num and make front = (front + 1) % queue.length 1 2 3 4 5

q.enqueue(9);

9 4

q.enqueue(5);

5 Need to check that the back of the queue does not overtake the front

January 27, 2020

Queue q; q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8); q.dequeue(); q.dequeue();

slide-21
SLIDE 21

Array queue resizing

  • Suppose we have an array-based queue and we have

performed some enqueue and dequeue operations

– Then we perform more enqueues to fill the array – How should we resize the array to allow for more enqueue operations?

January 27, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 21

12 5 76 33 2 41 ? ? ? ? ? ? ? ? ? ? ? ? front

slide-22
SLIDE 22

Queue implementation

  • Removing items from the front of the queue is straightforward
  • Items should be inserted at the back of the queue in constant

time

– So we must avoid traversing through the list – Use a second node pointer to keep track of the node at the back of the queue

  • Requires a little extra administration

Cinda Heeren / Andy Roth / Geoffrey Tien 22

Using a Linked List

January 27, 2020

Member attributes: Node* front; Node* back; int num;

slide-23
SLIDE 23

List queue example

Cinda Heeren / Andy Roth / Geoffrey Tien 23

Queue q;

front back

q.enqueue(6);

6

q.enqueue(4);

4

q.enqueue(7);

7

q.enqueue(3);

3

q.dequeue();

January 27, 2020

slide-24
SLIDE 24

Deques

  • A deque combines stack and queue functionality

– insertions and removals can happen at both ends

January 27, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 24

Double-ended queues

. . .

  • What kind of structure, with what features, can produce 𝑃 1

performance for all insertions and removals?

slide-25
SLIDE 25

Readings for this lesson

  • Carrano & Henry

– Chapter 6 (Stack) – Chapter C1.3 (Templates) – Carrano & Henry: Chapter 13.1 – 13.2 (Queue)

  • Next class:

– Carrano & Henry: Chapter 2 (Recursion) – Carrano & Henry: Chapter 11.2.1 (Merge sort)

January 27, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 25