Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Data Structures CSCI 132, Spring 2014 Lecture 8 Implementing Queues.

Similar presentations


Presentation on theme: "1 Data Structures CSCI 132, Spring 2014 Lecture 8 Implementing Queues."— Presentation transcript:

1 1 Data Structures CSCI 132, Spring 2014 Lecture 8 Implementing Queues

2 2 The Circular Model The front and rear are the same as the linear model, except: The queue wraps around when the end of the array is reached. append: add 1 to rear, if off the end of array, rear = 0; assign new item to entry[rear] retrieve: return entry[front] serve: increment front by 1, if off the end of array, front = 0;

3 3 Detecting an Empty Queue The idea: in an empty queue, the front is behind the rear: front == rear + 1. The problem: this is also true for a full array! The possible solutions: 1) Always leave an empty position. Then the queue is full when front == rear + 2 2) Use a boolean flag that is set to true when the queue becomes full. 3) Use a counter to keep track of number of items in the queue.

4 4 Queue Operations A Queue class should have the following methods: append(item)//Add an item to the rear of the queue serve( )//Delete an item from the front of the queue retrieve( )//Return value of front item empty()//Return true if queue is empty.

5 5 The Queue Specification typedef int Queue_entry; const int maxqueue =10 ;//small value for testing class Queue { public: Queue(); bool empty()const; Error_code serve(); Error_code retrieve(Queue_entry &item) const; Error_code append(const Queue_entry &item); private: int count, front, rear; Queue_entry entry [maxqueue ]; };

6 6 Implementing append( ) Error_code Queue ::append(const Queue_entry &item) { }

7 7 Implementing append( ) Error_code Queue ::append(const Queue_entry &item) { Error_code outcome =success ; if (count >=maxqueue) { outcome =overflow ; } else { count++; if ((rear + 1) == maxqueue) { rear = 0; } else { rear = rear + 1; } entry[rear] = item; } return outcome ; }

8 8 Implementing serve( ) Error_code Queue ::serve() { }

9 9 Implementing serve( ) Error_code Queue ::serve() { Error_code outcome =success ; if (count <=0) { outcome =underflow ; } else { count-- ; front = ((front + 1) == maxqueue) ? 0 : (front + 1); } return outcome ; }

10 10 Implementing retrieve( ) Error_code Queue ::retrieve(Queue_entry &item) const { }

11 11 Implementing retrieve( ) Error_code Queue ::retrieve(Queue_entry &item) const { if (count <=0){ return underflow ; } else { item =entry [front]; return success; }

12 12 Implementing empty( ) bool Queue ::empty() const { }

13 13 Implementing empty( ) bool Queue ::empty() const { return count == 0; }

14 14 Implementing Queue( ) Constructor Queue ::Queue() { }

15 15 Implementing Queue( ) Constructor Queue ::Queue() { count =0; front = 0; rear = maxqueue -1; }

16 16 Inheritance is a mechanism by which one class acquires (inherits) the properties (both data and operations) of another class. The class being inherited from is the Base Class (Parent). The class that inherits is the Derived Class (Child). The derived class is then specialized by adding properties specific to it.

17 17 Inheritance Hierarchy among vehicles vehicle wheeled vehicleboat bicyclecar four-door two-door Every car is a wheeled vehicle.

18 18 An extended queue class class Extended_queue : public Queue { public: bool full()const;//return true if queue is full int size() const;//return number of items in queue void clear();//make queue empty Error_code serve_and_retrieve(Queue_entry &item); //return value at front of queue and //delete the item from the queue };

19 19 class Extended_queue:public Queue says class Queue is a public base class of the derived class Extended_queue. As a result, all public members of Queue (except constructors) are also public members of Extended_queue Extended_queue adds some functions that are not available in the Queue class.

20 20 Extended_queue inherits from Queue

21 21 private vs. protected The derived class does not have access to private data members of the parent class. The derived class does have access to protected data members of the parent class. The client code does not have access to either private or protected data members.

22 22 A new Queue specification const int maxqueue =10 ;//small value for testing class Queue { public: Queue(); bool empty()const; Error_code serve(); Error_code retrieve(Queue_entry &item) const; Error_code append(const Queue_entry &item); protected: int count, front, rear; Queue_entry entry [maxqueue ]; };

23 23 Implementing Extended_queue::size( ) int Extended_queue :: size ( ) { return count; }


Download ppt "1 Data Structures CSCI 132, Spring 2014 Lecture 8 Implementing Queues."

Similar presentations


Ads by Google