Presentation is loading. Please wait.

Presentation is loading. Please wait.

LINKED QUEUES P. 412 - 425. LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.

Similar presentations


Presentation on theme: "LINKED QUEUES P. 412 - 425. LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working."— Presentation transcript:

1 LINKED QUEUES P

2 LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working on a list, which is limiting in its size again, the problem with the previous example of queues is that we are working on a list, which is limiting in its size using circular array can be inefficient if too many adds made before deletes (items overwritten) using circular array can be inefficient if too many adds made before deletes (items overwritten) just as with stacks and lists, we can link the queue to take advantage of virtual unlimited memory just as with stacks and lists, we can link the queue to take advantage of virtual unlimited memory but we lose dynamic access of queue elements but we lose dynamic access of queue elements 1) still need two indexes (represented as pointers now): 1) still need two indexes (represented as pointers now): front: where we take from the list front: where we take from the list back: where we add to the list back: where we add to the list 2) also, a number to keep track of the current size of list 2) also, a number to keep track of the current size of list

3 INTRO TO QUEUES (cont.) 4) when first value added, front and rear point to same place 4) when first value added, front and rear point to same place 5) each additional add: 5) each additional add: rear marker points to next item rear marker points to next item 6) each remove: 6) each remove: front marker points to previous item front marker points to previous item

4 Conceptual Design of a Queue so, if we design the Linked Queue as an object: so, if we design the Linked Queue as an object: LinkedQueue Object: Knows: pointer to space in memory containing the list the front the front the rear the rear Can Do: Retrieve an item (take out of front of queue) Add an item (add to end of queue) Add an item (add to end of queue) Tell which item is in front (without removing) Tell which item is in front (without removing) Clear the queue Clear the queue Tell if the queue is empty Tell if the queue is empty

5 Linked Queue Implementation typedef int Item; typedef Item * ItemPtr; class LinkedQueue { public:LinkedQueue(); Item Remove(); Item Remove(); Item First(); Item First(); int Size(); int Size(); void Insert(const Item &anItem); void Insert(const Item &anItem); bool IsEmpty() const; bool IsEmpty() const; void Clear(); void Clear();private: Node *Front; Node *Front; Node *Rear; Node *Rear; int total; int total;};

6 MORE MEMBER FUNCTIONS - The Default Constructor: - sets the Front and Rear pointers to point to the null address (nothing in the queue), and sets total to 0 LinkedQueue :: LinkedQueue() { Front = 0; Front = 0; Rear = 0; Rear = 0; total = 0; total = 0; } The IsFull Function - similar to linked stack, will return false always (dynamic memory) bool LinkedQueue :: IsFull() const { bool LinkedQueue :: IsFull() const { return (false); return (false);} The IsEmpty Function - will return true if the queue is empty (no items in it); bool LinkedQueue :: IsEmpty() const { bool LinkedQueue :: IsEmpty() const { return (Total == 0); return (Total == 0);}

7 MORE MEMBER FUNCTIONS The Size Function - will return the number of items currently in queue The Size Function - will return the number of items currently in queue int LinkedQueue :: Size() const { return(Total); return(Total);} The Insert Function - takes an item as input and adds it to the queue; then Front and Rear pointers are reset accordingly void LinkedQueue :: Insert(const Item &anItem) { Node *temp; temp = new Node; Node *temp; temp = new Node; temp->data = anItem; temp->link = 0; temp->data = anItem; temp->link = 0; if (isempty()) { if (isempty()) { Front = temp; Front = temp; Rear = temp; Rear = temp; } else { else { Rear->link = temp; Rear->link = temp; Rear = temp; Rear = temp; } total++; total++;}

8 MORE MEMBER FUNCTIONS Item LinkedQueue :: Remove() { Item value; Node *Front_Item; Item value; Node *Front_Item; if (IsEmpty()) { if (IsEmpty()) { cout << "Cannot remove - queue empty" << endl; cout << "Cannot remove - queue empty" << endl; return –999; return –999; } else { else { Front_Item = Front; Front_Item = Front; value = Front->data; value = Front->data; Front = Front->link; Front = Front->link; if (Front == 0) if (Front == 0) Rear = 0; Rear = 0; delete Front_Item; delete Front_Item; } total--; total--; return value; return value;}

9 AN EXAMPLE OF USING THIS LINKED QUEUE int main() { LinkedQueue TheQueue; int number; int count = 1; do { cout << "enter a number to add to the queue: "; cin >> number; TheQueue.Insert(number); } while (count++ < 5); int number_in_queue = TheQueue.Size(); int value_in_queue, evencout = 0; for (int i = 1; i <= number_in_queue; i++) { value_in_queue = TheQueue.Remove(); if (value_in_queue % 2 == 0) evencout++;} cout << "Total even numbers in queue: " << evencount << endl; }

10 QUESTIONS? Practice Test Posted Today Practice Test Posted Today TEST #2 – Fri. July 14 TEST #2 – Fri. July 14 Covers: Covers: Linked lists Linked lists Stacks Stacks Linked stacks Linked stacks Queues Queues Linked queues Linked queues STUDY SESSION ONLINE (Fri. July 12, 7 – 9 am) STUDY SESSION ONLINE (Fri. July 12, 7 – 9 am)


Download ppt "LINKED QUEUES P. 412 - 425. LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working."

Similar presentations


Ads by Google