CS 1704 Introduction to Data Structures and Software
- Slides: 13
CS 1704 Introduction to Data Structures and Software Engineering
Queues Restricted (two-tailed) list structure n Dynamic FIFO Storage Structure n – Size and Contents can change during execution of program – First in First Out – Elements are inserted (enqueue) into the rear and retrieved (dequeue) from front. n Think of waiting in line to check-out of a store. In I 3 Rear I 2 I 1 Out Front
Queue Implementation n n Queue ( ) ; † set queue to be empty bool Empty ( ) ; † check if queue is empty bool Full ( ) ; † check if queue is full Enqueue (const Item& item ) ; † Insert item into the queue Item Dequeue ( ) ; † Remove & return the item at the front of the queue
What about a Front()? n Some implementations define: – Item Front( ) ; n Returns first item in the queue, but does not remove it. – bool Dequeue() ; n In this case removes the first item in the queue, but does not return it. n What about a Clear()?
Implementation Details n Linear Array: not as easy to implement as it seems. – Front or Rear must be fixed at one end of the array • Enqueing or Dequeing requires inefficient array shifting. – OR if not fixed • The head and tail move causing problems.
Linear Array Solution n Make the queue circular. – The problem now becomes when is the queue empty and full? n Solution – Leave one cell empty. – The trade-off is one empty cell for processing time.
Whaaaaaaat? n Code operations to force array indicies to ‘wrap-around’ † front = (front + 1) % MAXQUE ; † rear = (rear + 1) % MAXQUE ; MAXQUE-1 0 1 que. front queue que. rear
States of the Queue front and rear indicies delimit the bounds of the queue contents n Enqueue n † Move the que. rear pointer 1 position clockwise & write the element in that position. n Dequeue † Return element at que. front and move que. front one position clockwise n Count (queue size) is stored and maintained or boolean full status flag maintained.
Array Interface const int MAXQUE = 100; //typedef arbitrary Itemtype; #include "Item. h" class Queue { private: int Front; int Rear; Items[MAXQUE]; public: Queue(); bool Empty(); bool Full(); void Enqueue (const Item& item); Item Dequeue (); };
Array Math n Distinct States † Full Queue: (que. rear + 1) % MAXQUE == que. front † Empty Queue: (que. rear == que. front ) † One-element Queue: (que. front + 1) % MAXQUE == que. rear
#include "Queue. h" Queue: : Queue() { Front = 0; Rear = 0; } bool Queue: : Empty ( ) { return ( Front == Rear ); } bool Queue: : Full ( ) { return ( ((Rear+1) % MAXQUE) == Front ); } void Queue: : Enqueue(const Item& item ) { Rear = (Rear + 1) % MAXQUE; Items[Rear] = item; } Item Queue: : Dequeue( ) { Front = (Front + 1) % MAXQUE; return( Items[Front] ); }
Linked-List Representation n Queue is a structure containing two pointers: † front: † rear: points to the head of the list points to the end of the list (last node) Enque operates upon the rear pointer, inserting after (before) the last (first) node. n Deque operates upon the front pointer, always removing the head (tail) of the list. n Empty queue is represented by NULL front & rear pointers n
Linked List Interface #include "Link. List. h" //typedef arbitrary Item #include "Item. h" class Queue { private: Link. List que; public: Queue(); //Link. List constructor bool Empty(); bool Full(); void Enqueue (const Item& Item); Item Dequeue (); };
- Poolse koning 1704
- 1-800-242-1704
- Homologous structures
- Introduction to data structures
- Introduction to data structures
- Introduction to data mining and data warehousing
- Data structures and algorithms iit bombay
- Amit agarwal princeton
- Data structures and algorithms tutorial
- Recursive macro expansion
- Assembler data structures
- Information retrieval data structures and algorithms
- Data structures and abstractions with java
- Data structures and algorithms bits pilani