25
scis.regis.edu [email protected] CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Scis.regis.edu ● [email protected] CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Embed Size (px)

Citation preview

Page 1: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

scis.regis.edu ● [email protected]

CS-362: Data StructuresWeek 8

Dr. Jesús Borrego

1

Page 2: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Topics

•Stacks•Queues•Final Exam

•Pointers = Punteros

2

Page 3: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Key Terms

•Stack – Pilas▫Push – Insertar▫Pop - Quitar

•Queue – Colas▫Enque – Insertar▫Dequeue - Eliminar

3

Page 4: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Stacks

4

Page 5: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Stack with Arrays

•Element 1 can go in first array position, the second in the second position, etc.

•The top of the stack is the index of the last element added to the stack

•Stack elements are stored in an array•Stack element is accessed only through

top•To keep track of the •top position, use a variable called Top

5

Page 6: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Stack using an Array

6

Page 7: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Stacks

•If Top = 0, the stack is empty•If Top = MAX, stack is full•Store the newItem in the array component

indicated by Top•Increment Top•Must avoid an overflow (or underflow)

7

Page 8: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Queue

•Queue: list of homogeneous elements•Elements are:

▫Added at one end (the back or rear)▫Deleted from the other end (the front)

•First In First Out (FIFO) data structure▫Middle elements are inaccessible

•Example:▫Waiting line in a bank

8

Page 9: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Queue Operations

▫initializeQueue▫isEmptyQueue▫isFullQueue▫front▫back▫addQueue▫deleteQueue

9

Page 10: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Queue implementation

•You need at least four variables:▫An array to store the queue elements▫queueFront and queueRear

To keep track of first and last elements▫maxQueueSize

To specify the maximum size of the queue

10

Page 11: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Adding elements

11

•To add an element to the queue:▫Advance queueRear to next array position ▫Add element to position pointed by queueRear

•Example: array size is 100; originally empty

Page 12: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Deleting Elements

12

•To delete an element from the queue:▫Retrieve element pointed to by queueFront▫Advance queueFront to next queue element

Page 13: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Implementation of Queues as Arrays (cont'd.)•Will this queue design work?

▫Suppose A stands for adding an element to the queue

▫And D stands for deleting an element from the queue

▫Consider the following sequence of operations: AAADADADADADADADA...

Page 14: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Implementation of Queues as Arrays (cont'd.)•The sequence AAADADADADADADADA...

would eventually set queueRear to point to the last array position▫Giving the impression that the queue is full

Page 15: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Implementation of Queues as Arrays (cont'd.)•Solution 1:

▫When the queue overflows to the rear (i.e., queueRear points to the last array position): Check value of queueFront If value of queueFront indicates that there is room in

the front of the array, slide all of the queue elements toward the first array position

•Problem: too slow for large queues•Solution 2: assume that the array is

circular

Page 16: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Implementation of Queues as Arrays (cont'd.)•To advance the index in a (logically)

circular array:

Page 17: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Implementation of Queues as Arrays (cont'd.)

17

Page 18: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Implementation of Queues as Arrays (cont'd.)•Case 1:

Page 19: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Implementation of Queues as Arrays (cont'd.)•Case 2:

Page 20: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Implementation of Queues as Arrays (cont'd.)

•Problem:▫Figures 19-32b and 19-33b have

identical values for queueFront and queueRear

▫However, the former represents an empty queue, whereas the latter shows a full queue

•Solution?

Page 21: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Implementation of Queues as Arrays (cont'd.)•Solution 1: keep a count

▫Incremented when a new element is added to the queue

▫Decremented when an element is removed▫Initially, set to 0▫Very useful if user (of queue) frequently

needs to know the number of elements in the queue

•We will implement this solution

Page 22: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Implementation of Queues as Arrays (cont'd.)•Solution 2: let queueFront indicate index

of the array position preceding the first element▫queueRear still indicates index of last one▫Queue empty if:

queueFront == queueRear

▫Slot indicated by queueFront is reserved Queue can hold 99 (not 100) elements

▫Queue full if the next available space is the reserved slot indicated by queueFront

Page 23: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Implementation of Queues as Arrays (cont'd.)

23

Page 24: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Summary• Stacks and Queues are important data

structures• Can be implemented as arrays or linked

lists• We will see in OOP that we can create

Abstract Data Types to protect the integrity of the data

24

Page 25: Scis.regis.edu ● scis@regis.edu CS-362: Data Structures Week 8 Dr. Jesús Borrego 1

Final Exam

• 8 questions from material covered since the mid term• Answer all 8 questions; equal weight• Submit to WorldClass week 8• Due by Monday midnight (12/16/13)• Must have all late submissions by

Sunday midnight to get credit

25