Question

1. BQUEUE.h #pragma once #include class bqnode { public: int time; bqnode *prev, *next; }; class...

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; }

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
Convert this C++ to JavaScript and run in browser. #include <cstdlib> #include <ctime> #include <sstream> #include...
Convert this C++ to JavaScript and run in browser. #include <cstdlib> #include <ctime> #include <sstream> #include <iostream> using namespace std; /* * */ class Dice{ private: static const int MAXDICE=6; static const int MINDICE=1; int faceVal; public: Dice(int); void setFace(int); int getFace(); string toString(); }; Dice::Dice(int faceVal) { if(faceVal<MINDICE&&faceVal>MAXDICE) { setFace(1); } else { this->faceVal=faceVal; } } void Dice::setFace(int faceVal) { this->faceVal=faceVal; } int Dice::getFace() { return faceVal; } string Dice::toString() { stringstream ss; ss<<faceVal; string str="face value is ";...
How do I fix my error of binding on line 74? #include <iostream> #include <fstream> #include...
How do I fix my error of binding on line 74? #include <iostream> #include <fstream> #include <cctype> #include <cstring> #include <iomanip> using namespace std; #include "AvlTree.h" class WordCount { public:     char *word;     int *lines;     int count;     int size;     bool operator<(const WordCount &rhs) const {return strcmp(word, rhs.word) < 0;}     bool operator!= (const WordCount &rhs) const {return strcmp(word, rhs.word) != 0;}     WordCount():lines(NULL), count(0), size(0) {word = new char[1]; word[0] = '\0';}     friend ostream& operator<<...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {     int value; public:     Test(int v); };    Test::Test(int v) {     value = v; }    int main() {     Test t[100];     return 0; } _______________ #include <iostream> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { for(j=1; j<=i; j++ ) { cout<<"*"; } cout << "\n";   } return 0; }
Please fill in the blank bolded below that would be the best choice for this program....
Please fill in the blank bolded below that would be the best choice for this program. #include <iostream> using namespace std; const int size = 100000; class TheBig { public: double operator[](int index) const {return (theData[index]);} private: double theData[size]; }; void firstToBeThe( ______________________________________________________) { for (int i = 0; i <size; i++) cout << value[i] <<endl; } int main() { TheBig one; firstToBeThe(one); }
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class is empty. Complete the destructor so the memory allocated for each node in the BST is freed. Make a couple of different trees in your main method or in a function to test the destructor (the program should not crash upon exiting). bst.zip (includes the following files below in c++): bst.h: #pragma once #include #include "node.cpp" using namespace std; template class BST { public:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT