Question

lab 11 Do not change main.cpp, i need c++ code for queue.h and queue.cpp Given the...

lab 11

Do not change main.cpp, i need c++ code for queue.h and queue.cpp

Given the complete main() function, partial queue class header queue.h, and queue.cpp, you will complete the class declaration and class implementation. The following member functions are required:

  • constructor
  • enqueue()
  • dequeue()

You may elect to create the following helper functions:

  • isFull()
  • isEmpty()

A description of these ADT operations are available in this Zybook and in the textbook's chapter 17.

Example: If the input is:

3
Led Zepplin
Linda Ronstadt
Elton John

where 3 is the number of phrases, and each subsequent phrase, the output is:

Led Zepplin Linda Ronstadt Elton John

The queue is a fixed size and is implemented as an array. You may NOT use any STL features.

main.cpp:

#include <iostream>
#include "queue.h"
using namespace std;

int main()
{
queue q;
string item;
int num = 0;

cin >> num >> ws;
for (int i=0; i<num; i++)
{
   getline(cin, item);
   q.enqueue(item);
}

for (int i=0; i<num; i++)
   cout << q.dequeue() << " ";

cout << endl;

return 0;
}

queue.h

#ifndef QUEUE_H
#define QUEUE_H

#include <string>

/*
* QUEUESIZE should be one more than the desired queue size
* The queue will hold QUEUESIZE-1 elements
* The unused element is used to distinguish full versus empty
*/
const int QUEUESIZE = 11;

class queue
{
public:

// TODO: implement the public members as described in the specification

private:

// TODO: implement the private member as described in the specification

};

#endif

queue.cpp

#include "queue.h"

// TODO: implement the class member functions

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

Summary :

Provided the solution with following (i) Details of appraoch (ii) Code for queue.h & queue.cpp & (iii) Sample Output

Appraoch :

Implemented the queue using teh array of strings ( this can be easily templatized as well by replacing string with a template declaration ).

The private memebers list holds the queue members , front & back hold the pointers to front and rear end of the queue membes and count tracks the number of elements in the queue .

The rationale to utilize front and back members allows the queue to be circular queue within a linear array . Other way to implment same by copying the elements of array every time they are dequeued .

###### Code ####

#ifndef QUEUE_H
#define QUEUE_H

#include <string>

using namespace std;

/*
* QUEUESIZE should be one more than the desired queue size
* The queue will hold QUEUESIZE-1 elements
* The unused element is used to distinguish full versus empty
*/
const int QUEUESIZE = 11;

class queue
{
public:


queue();
void enqueue(string item);
string dequeue();
bool isFull() const;
bool isEmpty() const;

private:

// TODO: implement the private member as described in the specification
int front;
int back;
int count ;
string list[QUEUESIZE];
};

#endif

### queue.cpp

#include "queue.h"
#include <iostream>

// Default constructor initialize membes to zeros
queue::queue()
{
this->front = 0;
this->back = 0;
this->count = 0 ;
}

void queue::enqueue( string item )
{

// add the element only if current count is < queuesize
if ( count < QUEUESIZE)
{

// add the element at back and increment the back counter and count
    list[back] = item;
    back = (back + 1) % QUEUESIZE;
    this->count += 1;
    std::cout << " Inserted " << item << " Count " << this->count << " F : " << front << " back " << back << " < " << QUEUESIZE << "\n";
}
}


string queue::dequeue()
{
//cout << " retrieving :: ";

// Retrieve the element only if count is > 0
if ( count > 0 )
{
    int idx = front ;

    // increment the front
    front = (front + 1 )% QUEUESIZE ;
    count -= 1;
    //cout << list[idx] << "Count : " << count << " Idx : " << idx << " F: " << front << " back : " << back << "\n";

    return list[idx];
}
return "";
}

bool queue::isEmpty() const
{
return (count == 0);
}

bool queue::isFull() const
{
   return ( count == QUEUESIZE);
}

##### Output #####

Add a comment
Know the answer?
Add Answer to:
lab 11 Do not change main.cpp, i need c++ code for queue.h and queue.cpp Given the...
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
  • I need c++ code Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp,...

    I need c++ code Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor copy constructor destructor addSong(song tune) adds a single node to the front of the linked list no return value displayList() displays the linked list as formatted in the example below no return value overloaded assignment operator A description of all of these functions is available in the textbook's...

  • 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...

  • 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...

  • Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQ...

    Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQueue { private final int INITIAL_SIZE = 10;   private int [] A = new int[INITIAL_SIZE]; // array to hold queue: front is always at Q[0] // and rear at A[next-1] int next = 0; // location of next available unused slot // interface methods public void enqueue(int key) { //TODO: push the key onto the back of the queue // YOUR CODE HERE! }...

  • 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...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string;...

    Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string; class Account { public: Account(); Account(string, double); void deposit(double); bool withdraw(double); string getName() const; double getBalance() const; private: string name; double balance; }; #endif ////////////////////////////////////////////// //AccountManager.hpp #ifndef _ACCOUNT_MANAGER_HPP_ #define _ACCOUNT_MANAGER_HPP_ #include "Account.hpp" #include <string> using std::string; class AccountManager { public: AccountManager(); AccountManager(const AccountManager&); //copy constructor void open(string); void close(string); void depositByName(string,double); bool withdrawByName(string,double); double getBalanceByName(string); Account getAccountByName(string); void openSuperVipAccount(Account&); void closeSuperVipAccount(); bool getBalanceOfSuperVipAccount(double&) const;...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • C++ program please provide code only for class counterType.h, counterTypeImp.cpp, and main.cpp. Separately code it. Define...

    C++ program please provide code only for class counterType.h, counterTypeImp.cpp, and main.cpp. Separately code it. Define a class counterType to implement a counter. Your class must have a private data member counter of type int. Define a constructor that accepts a parameter of type int and initializes the counter data member. Add functions to: Set counter to the integer value specified by the user. Initialize counter to 0. Return the value of counter with a function named getCounter. Increment and...

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