Presentation is loading. Please wait.

Presentation is loading. Please wait.

18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.

Similar presentations


Presentation on theme: "18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front."— Presentation transcript:

1 18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front rear Chapter 18 slide 1

2 Implementation Data Structures
class QNode { char value; QNode *next; QNode(char ch, QNode *p = 0); {value = ch; next = p;} } QNode *front = NULL; QNode *rear = NULL; Chapter 18 slide 2

3 isEmpty Member Function
To check if queue is empty: bool isEmpty() { if (front == NULL) return true; else return false; } See DynIntQueue.h, DynIntQueue.cpp, pr18-06.cpp Chapter 18 slide 3

4 enqueue Member Function
To add item at rear of queue void enqueue(char x) { if (isEmpty()) { rear = new QNode(x); front = rear; return; } rear->next = new QNode(x); rear = rear->next; See DynIntQueue.h, DynIntQueue.cpp, pr18-06.cpp Chapter 18 slide 4

5 dequeue Member Function
To remove item from front of queue void dequeue(char &x) { if (!isEmpty()) x = front->value; QNode *oldfront = front; front = front->next; delete oldfront; } See DynIntQueue.h, DynIntQueue.cpp, pr18-06.cpp Chapter 18 slide 5


Download ppt "18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front."

Similar presentations


Ads by Google