1. BQUEUE.h #pragma once #include class bqnode { public: int time; bqnode *prev, *next; }; class BQUEUE { public: BQUEUE(); ~BQUEUE(); BQUEUE(const BQUEUE &); void Enqueue(int); void Dequeue(); void Print(); private: bqnode * front; //use ONLY one pointer }; 2. BQUEUE.cpp #include "BQUEUE.h" using namespace std; BQUEUE::BQUEUE() { } BQUEUE::~BQUEUE() { } BQUEUE::BQUEUE(const BQUEUE & otherList) { if (otherList.front == NULL) return; front = new bqnode(); bqnode *curr = front; bqnode *oldnode = otherList.front; curr->time = oldnode->time; curr->next = NULL; while (oldnode->next != NULL) { bqnode *newnode = new bqnode(); newnode->time = oldnode->next->time; curr->next = newnode; newnode->prev = curr; curr = newnode; oldnode = oldnode->next; } } void BQUEUE::Enqueue(int newdata) { bqnode* newnode = new bqnode(); newnode->time = newdata; newnode->prev = NULL; newnode->next = front; if (front != NULL) front->prev = newnode; front = newnode; } void BQUEUE::Dequeue() { // If queue is empty, return NULL. if (front == NULL) return; bqnode* ptr; ptr = front; while (ptr->next != NULL) { ptr = ptr->next; } bqnode * prev = ptr->prev; if (prev != NULL) prev->next = NULL; ptr = NULL; } void BQUEUE::Print() { bqnode* ptr; ptr = front; cout << "Print array => " ; while (ptr != NULL) { cout << ptr->time << " "; ptr = ptr->next; } cout << endl; } 3. Driver class #include #include "BQUEUE.h" using namespace std; int main() { BQUEUE k; k.Enqueue(60); k.Print(); k.Enqueue(20); k.Enqueue(30); k.Print(); k.Enqueue(10); k.Print(); k.Enqueue(50); k.Enqueue(40); k.Print(); BQUEUE j = k; j.Dequeue(); j.Print(); j.Dequeue(); j.Dequeue(); j.Dequeue(); j.Print(); j.Dequeue(); j.Dequeue(); j.Print(); j.Dequeue(); j.Dequeue(); cout << "Press any key to exit"; getchar(); return 0; }
BQUEUE.H
#pragma once
class bqnode
{ public:
int time;
bqnode *prev, *next;
};
class BQUEUE {
public:
BQUEUE();
~BQUEUE();
BQUEUE(const BQUEUE &);
void Enqueue(int);
void Dequeue();
void Print();
private:
bqnode * front; //use ONLY one pointer
};
BQUEUE.CPP
#include "BQUEUE.h"
#include <iostream>
using namespace std;
BQUEUE::BQUEUE() {
front = NULL;
}
BQUEUE::~BQUEUE() {
}
BQUEUE::BQUEUE(const BQUEUE & otherList) {
if (otherList.front == NULL)
return;
front = new bqnode();
bqnode *curr = front;
bqnode *oldnode = otherList.front;
curr->time = oldnode->time; curr->next = NULL;
while (oldnode->next != NULL) {
bqnode *newnode = new bqnode();
newnode->time = oldnode->next->time;
curr->next = newnode; newnode->prev = curr;
curr = newnode; oldnode = oldnode->next;
}
}
void BQUEUE::Enqueue(int newdata) {
bqnode* newnode = new bqnode();
newnode->time = newdata;
newnode->prev = NULL;
newnode->next = front;
if (front != NULL){
front->prev = newnode;
}
front = newnode;
}
void BQUEUE::Dequeue() {
// If queue is empty, return NULL.
if (front == NULL)
return;
bqnode* ptr;
ptr = front;
while (ptr->next != NULL) {
ptr = ptr->next;
}
bqnode * prev = ptr->prev;
if (prev != NULL)
prev->next = NULL;
if(front == ptr)
front = NULL;
delete ptr;
}
void BQUEUE::Print() {
bqnode* ptr;
ptr = front;
if(ptr == NULL){
cout<<"Empty array"<<endl;
return;
}
cout << "Print array => " ;
while (ptr != NULL) {
cout << ptr->time << " ";
ptr = ptr->next;
}
cout << endl;
}
DRIVER.CPP
#include "BQUEUE.h"
#include "BQUEUE.CPP"
#include <stdio.h>
using namespace std;
int main() {
BQUEUE k;
k.Enqueue(60);
k.Print();
k.Enqueue(20);
k.Enqueue(30);
k.Print();
k.Enqueue(10);
k.Print();
k.Enqueue(50);
k.Enqueue(40);
k.Print();
BQUEUE j = k;
j.Dequeue();
j.Print();
j.Dequeue();
j.Dequeue();
j.Dequeue();
j.Print();
j.Dequeue();
j.Dequeue();
j.Print();
j.Enqueue(30);
j.Print();
j.Dequeue();
cout << "Press any key to exit";
getchar();
return 0;
}
Output
Get Answers For Free
Most questions answered within 1 hours.