I'd like to create a program that combines two consolidated lists (one consolidated list is connected to the other) and displays the combined list.
combined list.I've done what I want to do, but it doesn't end normally (doesn't return zero).Apparently, the append method is not working well.After debugging, it seems that the setPrev method is looping forever.
However, if this is really a problem, I don't know how to solve it.When I displayed the combined consolidated list in main.cpp, it seems to be connected.
main.cpp
#include<iostream>
# include "TextClass.h"
using namespace std;
int main() {
const int APPEND1 = 6;
const int APPEND2 = 7;
int counter = 0;
char appendVals [APPEND1+APPEND2] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'};
TextClass first;
for (inti=0;i<APPEND1;i++)
{
first.addTail (appendVals [counter]);
counter++;
}
TextClass second;
for (inti=0;i<APPEND2;i++)
{
second.addTail(appendVals[counter]);
counter++;
}
std::cout<<"First list is"<<first.displayList()<<std::endl;
std::cout<<"Second list is"<<second.displayList()<<std::endl;
first.append(second);
std::cout<<"First should now be a bc de fghijklm"<<std::endl;
std::cout<<"and it actually is"<<first.displayList()<<std::endl;
std::cout<<"Done applying a list\n"<<std::endl;
return 0;
}
TextClass.cpp
#include "TextClass.h"
# include <sstream>
// constructor
Link:: Link (charletter, Link*next, Link*prev)
{
value = letter;
this ->next=next;
this ->prev=prev;
}
// destructor
Link::~Link(){}
// return the value in the link
char Link::getValue() {return value;}
// set a new next address
void Link::setNext(Link*next) {this->next=next;}
// set a new previous address
void Link::setPrev(Link*prev) {this->prev=prev;}
// return the next address
Link*Link::getNext() {return this ->next;}
// return the previous address
Link*Link::getPrev() {return this ->prev;}
// constructor
TextClass:: TextClass()
{
// head and tail are set to nullptr
head = nullptr;
tail = nullptr;
}
// destructor
TextClass::~TextClass()
{
// call removeHead until head = nullptr;
while(head!=nullptr)
{
removeHead();
}
}
// add value at tail
voidTextClass::addTail(charletter)
{
// if head == nullptr
if(tail==nullptr)
{
// head and tail = new link
head=tail=newLink(letter);
}
// else
else
{
// create new link and insert it at tail
Link*temp = new Link (letter, nullptr, tail);
tail->setNext(temp);
// change tail
tail = temp;
}
}
// return the contents of list
US>string TextClass::displayList()
{
// create a variable that stores contents for string and stringstream
string output;
std::stringstreams;
// create a link that walks down the list and it starts from head
Link* temp=head;
// while the link!= nullptr
while(temp!=nullptr)
{
// copy the value in the link to stringstream
ss<<temp->getValue()<
// move the link forward
temp=temp->getNext();
}
// copy the value in stringstream to string
output =ss.str();
// return the string
return output;
}
// connect two lists
voidTextClass::append(TextClass otherList)
{
// create a link that stores the head of otherList
Link*temp=otherList.head;
// connect tworinks
// tail next should point to head of otherLink
while(temp!=nullptr)
{
addTail(temp->getValue());
temp=temp->getNext();
}
}
// remove head
voidTextClass::removeHead()
{
// save the link at head to delete later
Link* temp=head;
// update head
head=head->getNext();
// if head is null after update
if(head==nullptr)
{
//tail=nullptr
tail = nullptr;
}
// else update preview of new link
else
{
head->setPrev(nullptr);
}
// now delete the old link
delete temp;
}
TextClass.h
#include<iostream>
using std::string;
classLink {
private:
char value; // stores chat type value
Link * next; // stores the address of next link
Link*prev;//stores the address of previous link
public:
Link(charletter, Link*next=nullptr, Link*prev=nullptr); // constructor
~Link();//destructor
chargeValue(); // return the value in the link
void setNext(Link*next); // set a new next address
void setPrev (Link*prev); // set a new previous address
Link*getNext(); // return the next address
Link* getPrev(); // return the previous address
};
classTextClass{
private:
Link*head;//track the head link of queue
Link*tail;//track the tail link of queue
public:
TextClass(); // constructor
~TextClass();//destructor
void addTail (charletter); // add value at tail
string displayList(); // return the contents of list
void append (TextClass otherList); // connect two lists
void removeHead(); // remove head
};
It doesn't say a word about what makes it "normal," so the reader doesn't know how to fix it.Your estimation is not always what you expect...
Well, for the time being, I think I can fix this problem next time.
Wrong:voidTextClass::append(TextClass otherList)
positive:voidTextClass::append(TextClass const&otherList)
( Modify both TextClass.h
and TextClass.cpp
).
Incorrectly, the otherList
argument is copied (copy construct) and discarded (destruct) when calling append()
, but the proposed source does not provide a copy constructor for TextClass
.That's probably why it's not working as expected (I don't know what's right, so I guess)
If it is positive, there will be no copying, so there will be no discard, so there will be no problem.
If you explain this area in detail in N, it will be quite long.Deep copy and Shallow copy.If you are interested, please give me another question.
© 2024 OneMinuteCode. All rights reserved.