Question

Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below:

To complete and verify the following features: is Empty? Add-push front Append push back Remove --pop front remove-pop back L

Dont forget to add a destructor that destroys the list. The following example AS9 application main and IntLinkedQueue.h star

cout << \nUse menu to test a Queue class instance, \n; menu; // main menu while while(stay) { // main menu while starts cou

E - is Empty? A - to Add -- push front a - to append - push back R- to Remove-pop front r - to Remove-pop back L - Length of

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();
~IntLinkedQueue();
bool isEmpty();
int getSize(); // size of the queue
void clear(); // clear the entire queue
string toString(); // return a string of a comma(,) delimited items e.g. "1, 2, 3, 4"
bool add(int &); // Front add
bool append(int &); // Rear append
bool fremove(int &); // Front remove
bool rremove(int &); // Rear remove
int& operator[] (const int &sub);
};
#endif
//end of Linked.h

// Linked.cpp
#include <sstream>
#include <stdexcept>
#include "Linked.h"

// constructor to create an empty queue
IntLinkedQueue::IntLinkedQueue()
{
front = nullptr;
rear = nullptr;
p = nullptr;
pp = nullptr;
size = 0;
}

// destructor to delete all the nodes of the queue
IntLinkedQueue::~IntLinkedQueue()
{
clear();
}

// function to return true if the queue is empty else false
bool IntLinkedQueue::isEmpty()
{
return(front == nullptr);
}

// function to return the number of elements in the queue
int IntLinkedQueue::getSize()
{
return size;
}

// function to delete the nodes of the queue
void IntLinkedQueue::clear()
{
// loop continues until front is null
while(front != nullptr)
{
p = front;
front = front->next;
delete(p);
}

rear = nullptr;
p = nullptr;
pp = nullptr;
size = 0;
}

// function to return string representation of the queue
string IntLinkedQueue::toString()
{
string str = "";
p = front;
while(p != rear)
{
stringstream ss;
ss << p->data;
str += ss.str()+", ";
p = p->next;
}

if(p != nullptr)
{
stringstream ss;
ss << p->data;
str += ss.str();
}

return str;
}

// function to add a new element at the front of queue
bool IntLinkedQueue:: add(int &n)
{
// create a new node
Node *node = new Node;
node->data = n;
node->next = front;
front = node;
// if empty queue, set rear to node
if(rear == nullptr)
rear = node;
size++;
return true;
}

// function to add a new element at the end of queue
bool IntLinkedQueue:: append(int &n)
{
// create a new node
Node *node = new Node;
node->data = n;
node->next = nullptr;
// if empty queue, set front to node
if(front == nullptr)
front = node;
else // else set next of rear to node
rear->next = node;
rear = node; // set rear to node
size++;
return true;
}

// function to remove the front element of queue
bool IntLinkedQueue:: fremove(int &n)
{
if(!isEmpty()) // queue is not empty
{
n = front->data; // get the front data
// set front to node next to front
Node *temp = front;
front = front->next;
if(front == nullptr) // after removal, if front is null, set rear to null
rear = nullptr;
delete(temp);
size--;
return true;
}

return false; // queue is empty
}

// function to remove the last element of queue
bool IntLinkedQueue::rremove(int &n)
{
if(!isEmpty()) // queue is not empty
{
n = rear->data; // get the last data
// if queue contains one element,set front and rear to null
if(front == rear)
{
p = front;
front = nullptr;
rear = nullptr;
delete(p);
}else
{

p = front;
// loop to get the second last node of the
while(p->next != rear)
p = p->next;

rear = p; // set rear to curr
p = p->next;
rear->next = nullptr; // set next to rear to null
delete(p);
}
size--;
return true;
}

return false; // queue is empty
}

// function to return the element at index sub
int& IntLinkedQueue:: operator[] (const int &sub)
{
// validate index
if(sub >= 0 && sub < size)
{
if(sub == 0) // return front data
return front->data;
else if(sub == size-1) // return last data
return rear->data;
else
{
int index = 0;
p = front;
// loop to get the node at index
while(index < sub)
{
p = p->next;
index++;
}

return p->data; // return the data
}
}else
throw out_of_range("Index out of range");
}

//end of Linked.cpp

// main.cpp
#include "Linked.cpp"
#include <vector>
#include <string>
#include <sstream>
using namespace std;
void menu()
{

std::cout<<"---\tAS9 Linked List Test Menu\t---"<<endl
<<"\t"<<"N - to bulk create New Queue"<<endl
<<"\t"<<"I - Index access"<<endl
<<"\t"<<"D - to Display"<<endl
<<"\t"<<"E - isEmpty?"<<endl
<<"\t"<<"A - to Add ~ pushfront"<<endl
<<"\t"<<"a - to append ~ pushback"<<endl
<<"\t"<<"R - to Remove ~ popfront"<<endl
<<"\t"<<"r - to Remove ~ popback"<<endl
<<"\t"<<"L - length of Queue"<<endl
<<"\t"<<"C - to Clear"<<endl
<<"\t"<<"Q - to Quit this program"<<endl
<<"\t"<<"H - this menu"<<endl;
}

int main()
{

bool stay = true;
// create one instance for each of the test classes
cout << "\nInstanciate an object of Queue\n";
string catchVar;
// To hold values popped off the Queue
IntLinkedQueue q;
cout << "\nFirst, test with hard-wired data sets!\n" << "For example, \nyou may use one set of tokens for all test classes: \n";
string choice; // user input for choices
string str; // user input for list, delimiter, ...
int s[] = {1, 3, 5, 7, 9};
vector<int> input(s, end(s));
// show the initial item list
int input_size = input.size();
for (auto item : input )
{
cout << item << ((input_size <= 1) ? " " : ", ");
input_size--;
}

cout << endl; // initialize the Queue class instances
for (auto i: input) q.append( i );
cout << "\nUse menu to test a Queue class instance.\n";
menu();
// main menu while
while(stay)
{
// main menu while starts
cout << "\n Enter your command: ";
stay = true;
cin >> choice;
cin.ignore();
int pos;
string input;
string token;
int item;
stringstream sst;
stringstream ss;
int Queue_size;
if(choice.size() == 1)
{
char ch = choice[0];
//vector dump;
string value;
switch(ch) {
// main menu switch starts
case 'N':
case 'n':
{
if(!q.isEmpty())
q.clear();
cout << "Enter a line of comma (,) delmited data set: ";
getline (cin, input); // user input -> string
ss << input; // string -> stream
while ( getline(ss, token, ',') )
{
// stream -> string token
stringstream sst(token);
sst >> item;
q.append(item);
}
break;
}

case 'i':
case 'I':
{

std::cout<<"Enter the 0-based index: ";
cin>>pos;
if(pos >=0 && pos< q.getSize()){
char temp;
std::cout<<"The Queue["<<pos<<"] is "<<q[pos];
std::cout<<endl<<"To modify y/n? ";
cin>>temp;
switch(temp){
case'Y':
case 'y':
std::cout<<"What is the new number? ";
cin>>item;
q[pos] = item;
break;
}
}

break;
}

case 'd':
case 'D':
cout<<q.toString()<<endl;
break;

case 'e':
case 'E':
if(q.isEmpty())
cout<<"Queue is empty"<<endl;
else
cout<<"Queue is not empty"<<endl;
break;

case 'A':
std::cout<<"Enter a number:";
cin>>item;
std::cout<<"You entered: "<<item<<endl;
q.add(item);
cout<<"Queue: "<<q.toString()<<endl;
break;

case 'a':
std::cout<<"Enter a number:";
cin>>item;
std::cout<<"You entered: "<<item<<endl;
q.append(item);
cout<<"Queue: "<<q.toString()<<endl;
break;

case 'R':
if(q.fremove(item)){
cout<<"Removed "<<item<<endl;
cout<<"Queue: "<<q.toString()<<endl;
}else
cout<<"Queue is empty"<<endl;

break;

case 'r':
if(q.rremove(item))
{
cout<<"Removed "<<item<<endl;
cout<<"Queue: "<<q.toString()<<endl;
}else
cout<<"Queue is empty"<<endl;

break;
case 'l':
case 'L':
cout<<"Number of Entries in Queue: "<<q.getSize()<<endl;
break;

case 'c':
case 'C':
q.clear();
cout<<"Queue is cleared"<<endl;
break;

case 'q':
case 'Q':
stay = false;
break;

case 'h':
case 'H':
menu();
break;
}
}
}

cout<<"Program exit!"<<endl;
return 0;
}

//end of main.cpp

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

Note :-

I'm using tdm-gcc , g++ version is 5.1.0

---Code Fix---

On building the code the following error show's up

2020>g++ -std=C++11 -o main main.cpp Linked.cpp CCPEAfB8.o:Linked.cpp:(.text+0x0): multiple definition of IntLinkedQueue:: In

This error happens since Linked.cpp has a definition of all the defined functions , and then when it is included in main.cpp - during compilation the functions defined in Linked.cpp is added(included) to main.cpp

V/ main.cpp #include Linked.cpp #include <vectors #include <string> #include <sstream> using namespace std; void menu()

This confuses the linker since it doesn't understand which function is to be called - To fix this include the header file in main.cpp and then build the executable

Linked.h X Linked.cpp X main.cpp X V/ main.cpp #include Linked.h #include <vector #include <string> #include <stream> using

Chegg\.. \27-84-2020>g++ -std=C++11 -o main main.cpp Linked.cpp Chegg \27-04-2020/main Instanciate an object of Queue First,

Output :-

Use menu to test a Queue class instance. AS9 Linked List Test Menu N - to bulk create New Queue I - Index access D - to Displ

Add a comment
Know the answer?
Add Answer to:
Hello, I have some errors in my C++ code when I try to debug it. I...
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
  • To solve real world problem, the programer uses ADT like list, stack, queue, set, map, ... etc. t...

    To solve real world problem, the programer uses ADT like list, stack, queue, set, map, ... etc. to build our data model. In AS8, we pick and choose STL queue and stack to build a postfix calculator. In this assignment you are to Design your own linked-list, a series of integer nodes. The private attributes of this IntLinked Queue including a integer node struct and private control variables and methods: private: struct Node { int data; Node *next; }; Node...

  • C++ - I have a doubly linked list, but I haven't been able to get the...

    C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...

  • I am having trouble with my C++ program.... Directions: In this lab assignment, you are to...

    I am having trouble with my C++ program.... Directions: In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node), but it need not be sorted. The IntegerSet class should have two data fields: the cardinality (size) of the set, and the head reference to the...

  • PROBLEM: string CBQueue::dequeue( ) This method should remove and return the item at the front of...

    PROBLEM: string CBQueue::dequeue( ) This method should remove and return the item at the front of the queue- please add comments EXISTING CODE: #include // this allows you to declare and use strings #include using namespace std; struct qNode {   string data;   qNode* next;   qNode* prev; }; class CBQueue {   public:     CBQueue(); int CBQueue::getSize( ); bool CBQueue::isEmpty( );   private:     qNode* front;     qNode* rear;     int size; }; #include "CBQueue.h" CBQueue::CBQueue() { front = NULL; rear = NULL; size = 0; }...

  • C++: Need help debugging my code I am writing this and using some other examples as reference code while rewriting it with my own understanding of the material but am having trouble making it finally...

    C++: Need help debugging my code I am writing this and using some other examples as reference code while rewriting it with my own understanding of the material but am having trouble making it finally compile. Below is a picture of the error messages. //main.cpp //Semester Project //Created by J---on 5/6/2019 #include <iostream> #include <fstream> #include <string> #include <sstream> #include <bits/stdc++.h> using namespace std; void instructions(); //displays program details and instructions void openFile(); void takeInput(int*); void switchBoard(int*); struct price {...

  • Are based on the following Queue class code segment class QueueFull {/* Empty exception class */};...

    Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...

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

  • What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

    What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...

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

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

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