COP3502_21_Queues - Queues Computer Science Department University of Central Florida COP 3502 Computer Science I Queues An Overview Queues Like

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Science Department University of Central Florida Queues COP 3502 – Computer Science I
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Queues page 2 Queues – An Overview Queues: Like stacks, Queues are an Abstract Data Type They are NOT built into C We must define them and their behaviors So what is a queue? A data structure that stores information in the form of a typical waiting line New items are added at the end of the queue Elements are removed from the front of the queue So unlike a stack A queue is accessible from both ends (front and end )
Background image of page 2
Queues page 3 Queues – An Overview Queues: Access Policy: The first element that is inserted into the queue is the first element that will leave the queue Therefore, in order for the last element to leave the queue, it must wait until all elements preceding it are removed Known as the “First in, First out” access policy FIFO for short Real life example: waiting in line to be served When a customer arrives, they enter the line at the back They wait their turn Finally, they get to the front, are served, and exit the line
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Queues page 4 Queues – An Overview Queues: Basic Operations: enqueue: Inserts and element at the rear of the queue O(1) time dequeue: Removes the element at the front of the queue O(1) time peek: Looks at the element at the front of the queue without actually removing it O(1) time
Background image of page 4
Queues page 5 Queues – An Overview Queues: Basic Operations: isEmpty: Checks to see if the queue is empty O(1) time isFull: Checks to see if the queue is full O(1) time clear: Clears the contents of the queue In “queue” order From front to back O(n) time
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Queues page 6 front START: 2 4 6 8 10 Q rear Sequence of operations Time Operation 1 insert 12 2 remove 3 insert 14 4 insert 16 5 remove time 1: 2 4 6 8 10 12 time 2: 4 6 8 10 12 time 3: 4 6 8 10 12 14 time 4: 4 6 8 10 12 14 16 time 5: 6 8 10 12 14 16 FIFO Nature of a Queue
Background image of page 6
Queues page 7 Queues: Array Implementation Queues: Array Implementation: How would you implement a queue using an array? Think of what “stuff” you would need… Other than the actual array, what else do you need? Remember, you need to enqueue and dequeue Meaning: You need to ALWAYS know where the front and back of the queue are.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
page 8 Sequence of operations Time Operation 1 remove 2 insert 12 3 remove 4 remove 5 remove 6 remove 7 remove front time 5: rear 10 12 front start: 2 4 6 8 10 Q rear front time 1: 4 6 8 10 rear front time 3: 6 8 10 12 rear front time 4: 8 10 12 rear Notice that the array now has room to add elements to the queue. time 2:
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 09/21/2011.

Page1 / 60

COP3502_21_Queues - Queues Computer Science Department University of Central Florida COP 3502 Computer Science I Queues An Overview Queues Like

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online