Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7: Queues QUEUE IMPLEMENTATIONS QUEUE APPLICATIONS CS 240 44.

Similar presentations


Presentation on theme: "Chapter 7: Queues QUEUE IMPLEMENTATIONS QUEUE APPLICATIONS CS 240 44."— Presentation transcript:

1

2 Chapter 7: Queues QUEUE IMPLEMENTATIONS QUEUE APPLICATIONS CS 240 44

3 CS 240 45 The queue abstract data type is essentially a list using the FIFO (first-in-first-out) policy for adding and removing elements. The principal queue operations: Create an empty queue. Copy an existing queue. Destroy a queue. Determine whether a queue is empty. Add a new element to a queue. Remove the least recently added element from a queue.

4 CS 240 46 Queue Implementation Alternatives An Array Implementation Positives Avoids pointers (uses front & rear indices)  Negatives × Wraparound is needed to avoid false overflows × Size must be declared in advance A Linked List Implementation Positives Dynamically allocates exactly the right amount of memory Wraparound is circumvented  Negatives × Our friendly neighborhood pointers           

5 CS 240 47 Array Implementation of Queue // Class declaration file: Queue.h // Array implementation of the queue ADT. #ifndef QUEUE_H typedef int elementType; const MAX_QUEUE_SIZE = 200; class queue { public: // Class constructors queue(); queue(const queue &q); // Member functions bool isEmpty(); void enqueue(const elementType &item); elementType dequeue(); protected: // Data members int front, rear, length; elementType list[MAX_QUEUE_SIZE]; // Member function bool isFull(); }; #define QUEUE_H #endif // Class declaration file: Queue.h // Array implementation of the queue ADT. #ifndef QUEUE_H typedef int elementType; const MAX_QUEUE_SIZE = 200; class queue { public: // Class constructors queue(); queue(const queue &q); // Member functions bool isEmpty(); void enqueue(const elementType &item); elementType dequeue(); protected: // Data members int front, rear, length; elementType list[MAX_QUEUE_SIZE]; // Member function bool isFull(); }; #define QUEUE_H #endif // Class implementation file: queue.cpp // Array implementation of the queue ADT. #include "Queue.h" #include // Default constructor: Make empty queue // queue:: queue() { front = 0; rear = MAX_QUEUE_SIZE – 1; length = 0; } // Copy constructor: Make copy of queue. // queue:: queue(const queue &q) { int index; front = q.front; rear = q.rear; length = q.length; for (int i = 0; i < length; i++) { index = (i + q.front) % MAX_QUEUE_SIZE; list[index] = q.list[index]; } // Class implementation file: queue.cpp // Array implementation of the queue ADT. #include "Queue.h" #include // Default constructor: Make empty queue // queue:: queue() { front = 0; rear = MAX_QUEUE_SIZE – 1; length = 0; } // Copy constructor: Make copy of queue. // queue:: queue(const queue &q) { int index; front = q.front; rear = q.rear; length = q.length; for (int i = 0; i < length; i++) { index = (i + q.front) % MAX_QUEUE_SIZE; list[index] = q.list[index]; }

6 CS 240 48 // isEmpty function: signals if *this is empty queue. // bool queue:: isEmpty() { return (length == 0); } // Enqueue function; inserts a new item into the // // rear of queue *this (if there's enough room). // void queue:: enqueue(const elementType &item) { assert(!isFull()); rear = (rear+1) % MAX_QUEUE_SIZE; list[rear] = item; length++; } // Dequeue function; remove the item at the // // front of queue *this (if there's one there). // elementType queue:: dequeue() { elementType item; assert(!isEmpty()); item = list[front]; front = (front+1) % MAX_QUEUE_SIZE; length--; return item; } // isFull function; returns boolean value indicating // // if *this is a full queue (w.r.t. the array). // bool queue:: isFull() { return (length == MAX_QUEUE_SIZE); } // isEmpty function: signals if *this is empty queue. // bool queue:: isEmpty() { return (length == 0); } // Enqueue function; inserts a new item into the // // rear of queue *this (if there's enough room). // void queue:: enqueue(const elementType &item) { assert(!isFull()); rear = (rear+1) % MAX_QUEUE_SIZE; list[rear] = item; length++; } // Dequeue function; remove the item at the // // front of queue *this (if there's one there). // elementType queue:: dequeue() { elementType item; assert(!isEmpty()); item = list[front]; front = (front+1) % MAX_QUEUE_SIZE; length--; return item; } // isFull function; returns boolean value indicating // // if *this is a full queue (w.r.t. the array). // bool queue:: isFull() { return (length == MAX_QUEUE_SIZE); } Insert from the rear (with wraparound) Insert from the rear (with wraparound) Remove from the front (with wraparound) Remove from the front (with wraparound)

7 CS 240 49 Linked List Implementation of Queue // Class declaration file: Queue.h // Linked List implementation of queue ADT. #ifndef QUEUE_H #include "LinkedList.h" class queue: protected LinkedList { public: // Class constructors queue(); queue(const queue &q); // Member functions bool isEmpty(); void enqueue(const elementType &item); elementType dequeue(); protected: // Data members nodePtr tail; }; #define QUEUE_H #endif // Class declaration file: Queue.h // Linked List implementation of queue ADT. #ifndef QUEUE_H #include "LinkedList.h" class queue: protected LinkedList { public: // Class constructors queue(); queue(const queue &q); // Member functions bool isEmpty(); void enqueue(const elementType &item); elementType dequeue(); protected: // Data members nodePtr tail; }; #define QUEUE_H #endif // Class implementation file: Queue.cpp // Linked List implementation of the queue ADT. #include "Queue.h" #include // Default constructor: Makes an empty queue // queue:: queue(): LinkedList() { tail = NULL; } // Class implementation file: Queue.cpp // Linked List implementation of the queue ADT. #include "Queue.h" #include // Default constructor: Makes an empty queue // queue:: queue(): LinkedList() { tail = NULL; } Let’s assume that the getNode and head members in LinkedList were declared protected, not private! Let’s also assume that the elementType typedef occurred in the LinkedList definition! Let’s assume that the getNode and head members in LinkedList were declared protected, not private! Let’s also assume that the elementType typedef occurred in the LinkedList definition! The queue class “inherits” from the LinkedList class, so all LinkedList members are accessible to any queue. With its “protected” access specifier, the public and protected members of LinkedList are considered protected in the queue class. The queue class “inherits” from the LinkedList class, so all LinkedList members are accessible to any queue. With its “protected” access specifier, the public and protected members of LinkedList are considered protected in the queue class.

8 CS 240 50 // Copy constructor: Makes a deep // // copy of the *this queue. // queue:: queue(const queue &q) { nodePtr copyPreviousPtr, copyPtr, origPtr; if (q.head == NULL) tail = head = NULL; else { head = getNode(q.head->item); copyPreviousPtr = head; origPtr = q.head->next; while (origPtr != NULL) { copyPtr = getNode(origPtr->item); copyPreviousPtr->next = copyPtr; copyPreviousPtr = copyPtr; origPtr = origPtr->next; } tail = copyPreviousPtr; } // isEmpty function; Determines // // if the *this queue is empty. // bool queue:: isEmpty() { return (head == NULL); } // Copy constructor: Makes a deep // // copy of the *this queue. // queue:: queue(const queue &q) { nodePtr copyPreviousPtr, copyPtr, origPtr; if (q.head == NULL) tail = head = NULL; else { head = getNode(q.head->item); copyPreviousPtr = head; origPtr = q.head->next; while (origPtr != NULL) { copyPtr = getNode(origPtr->item); copyPreviousPtr->next = copyPtr; copyPreviousPtr = copyPtr; origPtr = origPtr->next; } tail = copyPreviousPtr; } // isEmpty function; Determines // // if the *this queue is empty. // bool queue:: isEmpty() { return (head == NULL); } // Enqueue function; Inserts item // // into the back of the *this queue. // void queue:: enqueue(const elementType &elt) { nodePtr newPtr = getNode(elt); assert (newPtr != NULL); if (head == NULL) head = tail = newPtr; else { tail->next = newPtr; tail = newPtr; } // Dequeue function; Removes item // // from the front of the *this queue // // (assuming such an item exists). // elementType queue:: dequeue() { elementType elt; nodePtr oldHead; assert(head != NULL); oldHead = head; elt = head->item; head = head->next; if (head == NULL) tail = NULL; delete oldHead; return elt; } // Enqueue function; Inserts item // // into the back of the *this queue. // void queue:: enqueue(const elementType &elt) { nodePtr newPtr = getNode(elt); assert (newPtr != NULL); if (head == NULL) head = tail = newPtr; else { tail->next = newPtr; tail = newPtr; } // Dequeue function; Removes item // // from the front of the *this queue // // (assuming such an item exists). // elementType queue:: dequeue() { elementType elt; nodePtr oldHead; assert(head != NULL); oldHead = head; elt = head->item; head = head->next; if (head == NULL) tail = NULL; delete oldHead; return elt; }

9 CS 240 51 // Program file: carwash.cpp // // This program simulates the operation of a // // car wash over 10 hours (600 minutes) of // // operation. The variables timeForWash and // // probOfArrival represent the time it takes // // to run one car through the car wash and // // the probability that a car arrives in any // // given minute. // #include #include "Queue.h" using namespace std; float random(); void initializeRandomSeed(); // The main function simulates the arrival of // // 600 cars at the car wash, queueing those // // which have to wait, and keeping a running // // tally of how long the cars are queued up. // void main() { int timeForWash, minute, timeEnteredQueue, carsWashed, totalQueueMin, timeLeftOnCar; float probOfArrival; queue carQueue; // Program file: carwash.cpp // // This program simulates the operation of a // // car wash over 10 hours (600 minutes) of // // operation. The variables timeForWash and // // probOfArrival represent the time it takes // // to run one car through the car wash and // // the probability that a car arrives in any // // given minute. // #include #include "Queue.h" using namespace std; float random(); void initializeRandomSeed(); // The main function simulates the arrival of // // 600 cars at the car wash, queueing those // // which have to wait, and keeping a running // // tally of how long the cars are queued up. // void main() { int timeForWash, minute, timeEnteredQueue, carsWashed, totalQueueMin, timeLeftOnCar; float probOfArrival; queue carQueue; Example Queue Application cout << "Enter time (in minutes)" << " to wash one car: "; cin >> timeForWash; cout << "Enter probability of " << "arrival in any minute: "; cin >> probOfArrival; carsWashed = 0; totalQueueMin = 0; timeLeftOnCar = 0; for (minute = 1; minute <= 600; minute++) { if (random() < probOfArrival) carQueue.enqueue(minute); if ((timeLeftOnCar == 0) && !carQueue.isEmpty()) { timeEnteredQueue = carQueue.dequeue(); totalQueueMin += (minute – timeEnteredQueue); carsWashed++; timeLeftOnCar = timeForWash; } if (timeLeftOnCar != 0) timeLeftOnCar--; } cout << "Enter time (in minutes)" << " to wash one car: "; cin >> timeForWash; cout << "Enter probability of " << "arrival in any minute: "; cin >> probOfArrival; carsWashed = 0; totalQueueMin = 0; timeLeftOnCar = 0; for (minute = 1; minute <= 600; minute++) { if (random() < probOfArrival) carQueue.enqueue(minute); if ((timeLeftOnCar == 0) && !carQueue.isEmpty()) { timeEnteredQueue = carQueue.dequeue(); totalQueueMin += (minute – timeEnteredQueue); carsWashed++; timeLeftOnCar = timeForWash; } if (timeLeftOnCar != 0) timeLeftOnCar--; }

10 CS 240 52 cout << endl << carsWashed << " cars were washed" << endl; cout.setf(ios::fixed); cout << setprecision(2); cout << "Average wait in queue: " << float(totalQueueMin)/carsWashed << " minutes." << endl << endl; } // Function random produces random floating-point number between 0 and 1. // float random() { // Generate random number seed the first time the function is called. static int iteration = 0; iteration++; if (iteration == 1) initializeRandomSeed(); return (float(rand()) / RAND_MAX); } // Function initializeRandomSeed uses the ctime library function // // time() to seed the rand() function via the srand() function. // void initializeRandomSeed() { // Time-elapsed value, used to seed the random number generator. time_t randomNumberSeed; time(&randomNumberSeed); srand(int(randomNumberSeed)); return; } cout << endl << carsWashed << " cars were washed" << endl; cout.setf(ios::fixed); cout << setprecision(2); cout << "Average wait in queue: " << float(totalQueueMin)/carsWashed << " minutes." << endl << endl; } // Function random produces random floating-point number between 0 and 1. // float random() { // Generate random number seed the first time the function is called. static int iteration = 0; iteration++; if (iteration == 1) initializeRandomSeed(); return (float(rand()) / RAND_MAX); } // Function initializeRandomSeed uses the ctime library function // // time() to seed the rand() function via the srand() function. // void initializeRandomSeed() { // Time-elapsed value, used to seed the random number generator. time_t randomNumberSeed; time(&randomNumberSeed); srand(int(randomNumberSeed)); return; }


Download ppt "Chapter 7: Queues QUEUE IMPLEMENTATIONS QUEUE APPLICATIONS CS 240 44."

Similar presentations


Ads by Google