Question

Objective: In this assignment you will do the following: (1) manipulate pointers, (2) allocate me...

Objective:

In this assignment you will do the following: (1) manipulate pointers, (2) allocate memory dynamically,

(3) implement a default constructor, copy constructor and destructor, (4) use only one pointer to add to

the back and to dequeue from the front of the queue.

Assignment Description:

You will implement a doubly-linked circular queue of integers. Consider the following class declarations

when implementing BQUEUE. As always, you must comment your declaration and implementation files,

"BQUEUE.h" and "BQUEUE.cpp", respectively.

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

};

Use the following driver called "BQUEUE_driver.cpp" to test your code:

#inlcude <iostream>

#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();

return 0;

}

  

  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. BQUEUE.h

#pragma once
#include <iostream>
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 <iostream>

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

}

Add a comment
Know the answer?
Add Answer to:
Objective: In this assignment you will do the following: (1) manipulate pointers, (2) allocate me...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How do I pass values to this function? class DynIntQueue { struct QueueNode { int value;...

    How do I pass values to this function? class DynIntQueue { struct QueueNode { int value; QueueNode *next; QueueNode(int value1, QueueNode *next1 = nullptr) { value = value1; next = next1; } }; // These track the front and rear of the queue QueueNode *front; QueueNode *rear; public: // Constructor and Destructor DynIntQueue(); ~DynIntQueue(); // Member functions void enqueue(int); void dequeue(int &); bool isEmpty() const; void clear(); }; main #include <iostream> #include "DynIntQueue.h" using namespace std; int main() {DynIntQueue list;...

  • Write a function that takes a string parameter and determines whether the string contains matching grouping...

    Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }. For example, the string {a(b+ac)d[xy]g} and kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Your function must use...

  • Using the below files, Write a program that reads a document containing endnotes indicated in this...

    Using the below files, Write a program that reads a document containing endnotes indicated in this manner, collects them in a queue, and prints them on the screen. For this lab, you will create a text file called sample.txt and put the following paragraph in it. This part is the beginning. {This part is the footnote.} This part is the end. /* Queue.h contains the declaration of class Queue. Basic operations: Constructor: Constructs an empty queue empty: Checks if a...

  • C++ problem to use dynamic memory allocation (of arrays) and pointer manipulation in order to implement...

    C++ problem to use dynamic memory allocation (of arrays) and pointer manipulation in order to implement the Inner and Outer classes (Circular Buffer of circular buffers using Queues). No need of any classes from the Standard Template Library (STL), not even vector. Add the member functions of those classes by following the codes of InnerCB.h and CBofCB.h below: // file: InnerCB.h // Header file for Inner Circular Buffer. // See project description for details. // #ifndef _INNERCB_H_ #define _INNERCB_H_ class...

  • In this assignment, you are given several classes in the cpp file “DList.cpp”. Your task is...

    In this assignment, you are given several classes in the cpp file “DList.cpp”. Your task is to complete the implementation of the classes specified as below. Y 1 Your Task You are given a class “Item” that contains one integer value, and two pointers. You are going to build a doubly linked list class DLinkedList. I describe the tasks below. Task 1: Implement the constructors (default and copy) of DLinkedList. You need to make sure that the copy constructor makes...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • ~DecayList() – The destructor de-allocates any dynamically allocated memory. How do I implement ~decaylist()? #ifndef DECAYLIST_H...

    ~DecayList() – The destructor de-allocates any dynamically allocated memory. How do I implement ~decaylist()? #ifndef DECAYLIST_H #define DECAYLIST_H #include <iostream> #include "Node.h" using namespace std; const int NUM_CONSECUTIVE = 3; class DecayList{ public:   // Constructor   // Preconditions: None   // Postconditions: New list is created   DecayList(); and here is the node class.   // Destructor   // Preconditions: None   // Postconditions: Dynamically allocated memory freed   ~DecayList(); #include <iostream> #include "Node.h" using namespace std; Node::Node(){     m_next = NULL;     m_value = NULL; } Node::~Node(){     //delete...

  • (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed...

    (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we cannot insert the next element even if there is a space in front of queue. Efficiently implement a queue class using a circular...

  • I need help fixing my code.   My output should be the following. Hello, world! : false...

    I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

  • QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities....

    QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities. enqueue (inserts element to the end) dequeue (removes the front element and provides content) isEmpty (checks whether the Queue is empty or not) makeEmpty () peek (provides the element sitting at the top/front, but does not remove) print (prints all the elements from front to the end) reversePrint(prints all the elements from end to the front with the help of back pointers inside your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT