[英]linked list not printing output
我必須為學校項目動態分配機器人列表。 在實際程序中,會有其他成員函數需要名稱列表才能執行某些功能。
截至目前,我才剛剛了解到這個概念,並非常努力地嘗試將我在網上看到的一些東西放在一起。 目前的問題是我無法確定我的列表是否已正確存儲——當我嘗試調用列表 function 的顯示時,我也變得不穩定 output。
如果可以的話請幫忙。 另外,我很高興聽到任何關於任何東西的提示,因為我對編程還很陌生。
class Node{
public:
std::string name_;
Node* next;
};
class linkedBotList{
public:
linkedBotList() {head = nullptr;} //constructor
~linkedBotList(){}; // destructure
void addNode();
void display();
private:
Node* head;
};
int main(int argc, const char * argv[]) {
linkedBotList* list = new linkedBotList();
int siz;
std::cout << "How many Robots?" << std::endl;
std::cout << "What are the names?" << std::endl;
std::cin >> siz;
for(int i = 0; i < siz; i++){
list->addNode();
}
delete list;
return 0;
}
void linkedBotList::addNode(){
std::string botName;
Node* newNode = new Node();
newNode->name_ = botName;
newNode->next = nullptr;
std::cin >> botName;
if(head == nullptr){
head = newNode;
}
else {
Node* temp = head; // head is not null
while(temp->next != nullptr){ // go until at the end of the list
temp = temp->next;
}
temp->next = new Node; // linking to new node
}
}
void linkedBotList::display() {
if (head == NULL) {
std::cout << "List is empty!" << std::endl;
}
else {
Node* temp = head;
while (temp != NULL) {
std::cout << "Made it to display funct.\n";
std::cout << temp->name_ << " ";
temp = temp->next;
}
std::cout << std::endl;
}
}
我確實嘗試了一些事情,比如切換我的temp
變量,以及其他一些重新分配。 也許有人可以快速發現問題並提供幫助?
您的顯示 function 沒問題。
問題是您在addNode()
中有 2 個邏輯缺陷:
您沒有在列表中正確存儲字符串。 在為botName
分配值之前,您將botName
分配給newNode->name_
。 所以你所有的節點都有空字符串。 之后分配botName
將不會更新newNode->name_
。 1個
如果列表不為空,則您正確地迭代到列表的末尾2 ,但是隨后您將一個新的空白節點分配給temp->next
而不是分配您已經填充的newNode
。 並且您的Node
構造函數沒有將next
成員初始化為nullptr
,因此您正在創建一個損壞的列表,這將導致隨后通過列表循環調用undefined behavior 。
試試這個:
void linkedBotList::addNode(){
std::string botName;
std::cin >> botName; // <-- move up here
Node* newNode = new Node{botName, nullptr};
if (!head){
head = newNode;
}
else {
Node* temp = head;
while (temp->next){
temp = temp->next;
}
temp->next = newNode; // <-- linking to new node
}
}
或者,您可以像這樣消除if
:
void linkedBotList::addNode(){
std::string botName;
std::cin >> botName;
Node** temp = &head;
while (*temp){
temp = &((*temp)->next);
}
*temp = new Node{botName, nullptr};
}
1 :更好的設計是讓addNode()
接受一個string
作為輸入參數,然后將cin
調用移到main()
的循環中。
2 :考慮將tail
成員添加到您的列表中,以避免每次添加都必須循環。
試試這個替代設計:
class Node{
public:
std::string name;
Node* next = nullptr;
};
class linkedBotList{
public:
linkedBotList() = default;
~linkedBotList();
void addNode(std::string name);
void display() const;
private:
Node* head = nullptr;
Node* tail = nullptr;
};
int main() {
linkedBotList list;
int siz;
std::string botName;
std::cout << "How many Robots?" << std::endl;
std::cin >> siz;
std::cout << "What are the names?" << std::endl;
for(int i = 0; i < siz; i++){
std::cin >> botName;
list.addNode(botName);
}
list.display();
return 0;
}
linkedBotList::~linkedBotList(){
Node *temp = head, *next;
while (temp) {
next = temp->next;
delete temp;
temp = next;
}
}
void linkedBotList::addNode(std::string name){
Node* newNode = new Node{name};
if (tail)
tail->next = newNode;
else
head = newNode;
tail = newNode;
}
void linkedBotList::display() const {
if (!head) {
std::cout << "List is empty!" << std::endl;
}
else {
Node* temp = head;
do {
std::cout << temp->name << " ";
temp = temp->next;
}
while (temp);
std::cout << std::endl;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.