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()...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
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<<...
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 ";...
- 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;       ...
Using SLLStack.java, implement an application, LineReverser.java, that reads a line from keyboard (user enters one line),...
Using SLLStack.java, implement an application, LineReverser.java, that reads a line from keyboard (user enters one line), insert each separate word into the stack, and read/print on the screen. So, if the user enters: This is a test the application output should be: test a is This. This is SLLSTACK.java below package linked_lists; public class SLLStack {    //instance variabels    private SLLNode top;    private int numOfItems;       //constructors    public SLLStack() {        top = null;   ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT