1 22 13
play

1/22/13 CS200 Algorithms and Data Structures Colorado State - PDF document

1/22/13 CS200 Algorithms and Data Structures Colorado State University CS200 Algorithms and Data Structures Colorado State University Ou ut tl li in ne e Queue? Implementing Queue Comparison implementations Pa ar rt t


  1. 1/22/13 ¡ CS200 Algorithms and Data Structures Colorado State University CS200 Algorithms and Data Structures Colorado State University Ou ut tl li in ne e � Queue? � Implementing Queue � Comparison implementations Pa ar rt t 2 2. . Qu Q ue eu ue es s CS 200 Algorithms and Data Structures 8 CS200 Algorithms and Data Structures Colorado State University CS200 Algorithms and Data Structures Colorado State University "G Gr ri il ll l t th he e B Bu uf ff fs s" " e ev ve en nt t Photo ¡by ¡David ¡Jump ¡ 9 10 CS200 Algorithms and Data Structures Colorado State University CS200 Algorithms and Data Structures Colorado State University Qu ue eu ue e Op pe er ra at ti io on ns s � A queue is like a line of people . � Create an empty queue � New item enters a queue at its back. � Determine whether a queue is empty � Items leave a queue from its front. � Add a new item to the queue � First-in, first-out (FIFO) behavior � Remove item from the queue (that was � Removing and adding are done from added the earliest ) opposite ends of structure � Remove all items from the queue � Useful for scheduling (e.g. print queue, job queue) � Retrieve item from queue that was added earliest 1 ¡ 2 ¡ 3 ¡ 4 ¡ Adding ¡ Removing ¡ 11 12 1 ¡

  2. 1/22/13 ¡ CS200 Algorithms and Data Structures Colorado State University CS200 Algorithms and Data Structures Colorado State University Qu ue eu ue e O Op pe er ra at ti io on ns s Ou ut tl li in ne e � enqueue (in newItem: QueueItemType ) � Queue? – Add new item at the back of a queue � Implementing Queue � dequeue ();QueueItemType � � Comparison implementations – Retrieves and removes the item at the front of a queue � peek (): queueItemType {query} � – Retrieve item from the front of the queue. Retrieve the item that was added earliest. � isEmpty ():boolean{query} � � createQueue () � � dequeueAll () � 13 14 CS200 Algorithms and Data Structures Colorado State University CS200 Algorithms and Data Structures Colorado State University Im mp pl le em me en nt ta at ti io on ns s o of f Q Qu ue eu ue e Re ef fe er re en nc ce e- -b ba as se ed d I Im mp pl le em me en nt ta at ti io on n( (1 1) ) � Reference-Based Implementation � Needs – Nodes with the item and a reference to the next item � Array-based Implementation – two external references pointing to the first node and the last node . � List based Implementation � java.util.queue interface 60 ¡ . ¡ 70 ¡ . ¡ 55 ¡ Item ¡ Next ¡ . ¡ . ¡ Reference ¡of ¡the ¡First ¡Node ¡ Reference ¡of ¡the ¡Last ¡Node ¡ 15 16 CS200 Algorithms and Data Structures Colorado State University CS200 Algorithms and Data Structures Colorado State University Re ef fe er re en nc ce e- -b ba as se ed d I Im mp pl le em me en nt ta at ti io on n( (2 2) ) In ns se er rt ti in ng g a an n i it te em m i in nt to o a a n no on ne em mp pt ty y q qu ue eu ue e � Single external references – Circular linked list represents a queue – The node at the back of the queue references the node at the front 60 ¡ . ¡ 70 ¡ . ¡ 55 ¡ 60 ¡ . ¡ 70 ¡ . ¡ 55 ¡ 85 ¡ � Step 1. newNode.next = lastNode.next; � Step 2. lastNode.next = newNode; . ¡ . ¡ . ¡ . ¡ � Step 3. lastNode = newNode; New ¡node ¡ Reference ¡of ¡the ¡First ¡Node ¡ Reference ¡of ¡the ¡Last ¡Node ¡ Reference ¡of ¡the ¡Last ¡Node ¡ 17 18 2 ¡

  3. 1/22/13 ¡ CS200 Algorithms and Data Structures Colorado State University CS200 Algorithms and Data Structures Colorado State University In ns se er rt t n ne ew w i it te em m i in nt to o t th he e q qu ue eu ue e isEmpty() � public void enqueue (Object newItem){ � public class QueuerReferenceBased implements QueueInterface { � Node newNode = new Node(newItem); � private Node lastNode ; � if (isEmpty()){ � A. ¡Empty ¡queue ¡ public QueueReferenceBased(){ � newNode.next = newNode; � lastNode = null; � } else { � B. ¡More ¡than ¡1 ¡item ¡ } � newNode.next = lastNode.next; � lastNode.next = newNode; � public boolean isEmpty (){ � return lastNode == null; � } � } � lastNode = newNode; � } � } � 20 19 CS200 Algorithms and Data Structures Colorado State University CS200 Algorithms and Data Structures Colorado State University In ns se er rt ti in ng g a a N Ne ew w I It te em m In ns se er rt ti in ng g a a N Ne ew w I It te em m � Insert a new item into the empty queue � Insert a new item into a queue that has more than 1 items. 60 ¡ . ¡ 60 ¡ . ¡ 70 ¡ . ¡ 80 ¡ . ¡ . ¡ . ¡ . ¡ Reference ¡of ¡the ¡Last ¡Node ¡ Reference ¡of ¡New ¡Node ¡ Reference ¡of ¡the ¡Last ¡Node ¡ 21 22 CS200 Algorithms and Data Structures Colorado State University CS200 Algorithms and Data Structures Colorado State University Re em mo ov vi in ng g a an n i it te em m f fr ro om m q qu ue eu ue e Pe ee ek k? ? public Object dequeue() throws QueueException{ � if ( !isEmpty ()){ � public Object peek() throws QueueException { � Node firstNode = lastNode.next ; � Why? ¡ if (FirstNode == lastnode) { � if (!isEmpty()){ � lastNode = null; � Node firstNode = lastNode.next ; � } � return firstNode.item ; � else{ � }else { � lastNode.next = firstNode.next ; � } � throw new QueueException(your_message); � return firstNode.item ; � } � } � } � else { exception handling.. � } � } � 23 24 3 ¡

  4. 1/22/13 ¡ CS200 Algorithms and Data Structures Colorado State University CS200 Algorithms and Data Structures Colorado State University Im mp pl le em me en nt ti in ng g q qu ue eu ue e w wi it th h A Ar rr ra ay y Im mp pl le em me en nt ti in ng g q qu ue eu ue e w wi it th h A Ar rr ra ay y � We need, � Queue is empty if back is less than front – An array of objects to store items � Inserting an item – variables to point to the “ front ” and “ back ” index of – Initially fron t is 0, back is -1 the array – Increment back – Place new item in items [ back ] items ¡ � Queue will be full if back equals MAX_QUEUE - 1 0 ¡ 1 ¡ 2 ¡ 4 ¡ Front ¡ Back ¡ 0 ¡ 1 ¡ 2 ¡ 3 ¡ MAX_QUEUE ¡-­‑1 ¡ 25 26 CS200 Algorithms and Data Structures Colorado State University CS200 Algorithms and Data Structures Colorado State University Im mp pl le em me en nt ti in ng g q qu ue eu ue e w wi it th h A Ar rr ra ay y So ol lv vi in ng g R Ri ig gh ht tw wa ar rd d D Dr ri if ft t P Pr ro ob bl le em m ( (1 1) ) � Removing an item � Shifting array elements to the left . Therefore front is always pointing to – Remove item from items [ front ] items [0]. – Increment front � Cost of implementation � Rightward drift – After a sequence of additions and removals, items in the queue will drift toward the end of the array – back can reach MAX_QUEUE-1 even when the queue contains only few items. 27 28 CS200 Algorithms and Data Structures Colorado State University CS200 Algorithms and Data Structures Colorado State University So ol lv vi in ng g R Ri ig gh ht tw wa ar rd d D Dr ri if ft t P Pr ro ob bl le em m ( (2 2) ) So ol lv vi in ng g R Ri ig gh ht tw wa ar rd d D Dr ri if ft t P Pr ro ob bl le em m ( (2 2) ) � Circular implementation of a queue � Delete ¡ ¡ T T N N O O R R F F MAX_QUEUE-­‑1 ¡ 0 ¡ MAX_QUEUE-­‑1 ¡ 0 ¡ 2 ¡ 2 ¡ 6 ¡ 1 ¡ 6 ¡ 1 ¡ 4 ¡ 4 ¡ 1 ¡ 1 ¡ 5 ¡ 2 ¡ 5 ¡ 2 ¡ 7 ¡ 7 ¡ 4 ¡ 3 ¡ 4 ¡ 3 ¡ BACK ¡ BACK ¡ 29 30 4 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend