queues - queue(dequeue • offer – adds a new item to the...

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

View Full Document Right Arrow Icon
Queues A queue is a data structure of ordered items where items are added to the rear (in the order that they are entered and removed from the front.) Remember in Class - we lined people up into a queue and the first person in line received their project back first! (FIFO)
Background image of page 1

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

View Full Document Right Arrow Icon
Queue Example The first person in line at a bank is served first and so on. People are served on in respect to their position in line. At the bakery, you pull a number out of the machine that distributes numbers in order on a first-come, first serve basis. When your number in the queue is called – it is your turn!
Background image of page 2
FIFO – First-In/First-Out A queue is a FIFO data structure Adding an item is called “enqueue” (Entering the queue) Removing an item is called “dequeue” (Removing from the queue) First-In/First-Out
Background image of page 3

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

View Full Document Right Arrow Icon
Comparing a Stack and Queue A A A A Push A Push B Push C B B B C Pop – Removes C Front Back Insert A A getFront – Removes A B Insert C A B B C Insert B A C
Background image of page 4
Enqueue/Dequeue remove – removes the front item from the
Background image of page 5

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

View Full Document Right Arrow Icon
Background image of page 6
Background image of page 7

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

View Full Document Right Arrow Icon
Background image of page 8
Background image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: queue (dequeue) • offer – adds a new item to the rear of the queue (enqueue) Palindrome Evaluation Using a Stack and Queue • A palindrome is a word that reads identical forwards and backwards • Examples – Radar – Sees – Bib • Whatever we read in and out of our stack and queue should output identical results from both the stack and queue! Palindrome Example with a Stack and Queue • Assume that we have push and inserted the characters onto our stack and queue • while(!q.empty()){ if(q.remove() != s.pop() ) mismatches ++; } if(mismatches == 0) System.out.println(“Yeah palindrome!”); else System.out.println(“That’s not a palindrome.”); Queue Linked List Implementation • There are “manyNodes” representing the number of nodes • The head is the front of the queue (where items are removed) • The rear (tail) is the end of the queue where the items are added) First-In/First-Out Data Structure...
View Full Document

{[ snackBarMessage ]}

Page1 / 9

queues - queue(dequeue • offer – adds a new item to the...

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