Topic 04 Queues
Topic 04 Queues
Topic 04 Queues
} 2
bool Queue::isFull() {return size == MaxSize;} 1
bool Queue::isEmpty() {return size == 0; }
MS-1 0
bool Queue::add(int n) bool Queue::remove(int &n)
{ {
if (! isFull() ) { if (! isEmpty() ) {
QueueArray[rear] = n; n = QueueArray[rear];
rear++; front++;
if (rear == maxSize) if (front == maxSize)
rear = 0; front = 0;
size++; size--;
return true; return true;
} }
else return false; else return false;
} }
Add: rear = (rear + 1) % maxSize;
1
MS-1 0
bool Queue::add(int n) bool Queue::remove(int &n)
{ {
if (! isFull() ) { if (! isEmpty() {
QueueArray[rear] = n; n = QueueArray[front];
rear++; front++;
if (rear == maxSize) if (front == maxSize)
rear = 0; front = 0;
size++; size--;
return true; return true;
} }
else return false; else return false;
} }
isEmpty: rear == front
2
isEmpty: size == 0
1
isFull: size == maxSize MS-1 0
bool Queue::add(int n) bool Queue::remove(int &n)
{ {
if (! isFull() ) { if (! isEmpty() {
QueueArray[rear] = n; n = QueueArray[front];
rear++; front++;
if (rear == maxSize) if (front == maxSize)
rear = 0; front = 0;
size++; size--;
return true; return true;
} }
else return false; else return false;
} }
Afsah Imtiaz Elahi, Data Structures 25
Introduction to Data Structures and Algorithm
Analysis With C++, by D. Samanta