我正在尝试在 c++ 中实现链表,但由于某种原因数据未插入列表中。
struct Node
{
int number;
Node* next;
};
Node* getNewNode();
void printList(Node*);
void append(Node* , int);
int main()
{
Node* head = new Node;
Node* looper = head;
std::cout << "ttCREATING A LINKED LIST!nnn";
while (true)
{
int number = 0;
std::cout << "Enter a number : ";
std::cin >> number;
if (number == -1)
{
break;
delete looper;
}
looper->number = number;
looper->next = getNewNode();
looper = looper->next;
}
looper = head;
printList(looper);
printf("What operation would you like to perform on linked list?n1. Append data.n2. Delete datan3. Push datann");
int choice = 0;
std::cout << "Choice : ";
std::cin >> choice;
switch (choice)
{
case 1:
{
int data = 0;
std::cout << "Enter data to be appended : ";
std::cin >> data;
looper = head;
append(looper, data);
break;
}
default:
std::cout << "Invalid Input!";
}
printList(looper);
return 0;
}
Node* getNewNode()
{
Node* node = new Node;
node->number = 0;
node->next = NULL;
return node;
}
void printList(Node* head)
{
std::cout << "nn[";
while (head->next != NULL)
{
std::cout << head->number ;
head = head->next;
if (head->next != NULL)
std::cout << ", ";
}
std::cout << "]n" << std::endl;
}
void append(Node* head, int data)
{
while (true)
{
if (head->next == NULL)
{
head->next = getNewNode();
head = head->next;
head->number = data;
head->next = NULL;
break;
}
else
head = head->next;
}
}
当我使用 printList 方法打印列表时,使用追加函数插入数据后,它显示 0 而不是我插入的数字。我相信这与通过引用传递有关,因为我在互联网上看到的代码发送一个指向结构指针的指针到追加方法。提前致谢
在此行中创建带有0
的项目
looper->next = getNewNode(); // default value is 0
当您输入-1
中断循环时,您应该删除上次创建的 Node,并在最后一个节点之前为 element 设置next = 0
。添加温度变量prev
Node* prev = 0; // new
while (true)
{
int number = 0;
std::cout << "Enter a number : ";
std::cin >> number;
if (number == -1)
{
prev->next = 0; // new
delete looper; // delete last node
break;
}
looper->number = number;
looper->next = getNewNode();
prev = looper; // new
looper = looper->next;
}
您还应该在printList函数中更改while循环,如果列表只有一个元素会发生什么?不会打印任何内容。
while (head != NULL) // test head != 0 instead of head->next
{
std::cout << head->number ;
if (head->next) std::cout << ", ";
head = head->next;
}
除了 @rafix07 提供的解决方案外,您还可以针对您的问题使用以下解决方案。下面我重写了你的整个代码。我希望它有所帮助。
#include<iostream>
#include<stdio.h>
struct Node
{
int number;
Node* next;
};
Node* head = NULL;
void printList();
void append();
int main()
{
std::cout << "ttCREATING A LINKED LIST!nnn";
bool loop_breaker = true;
while(loop_breaker)
{
printf("What operation would you like to perform on linked list?n1. Append data.n2. Delete datan3. Push datan4. Exitnn");
int choice = 0;
std::cout << "Choice : ";
std::cin >> choice;
switch (choice)
{
case 1:
append();
break;
case 4:
loop_breaker = false;
break;
default:
std::cout << "Invalid Input!";
break;
}
printList();
}
return 0;
}
void printList()
{
Node *looper = head;
std::cout << "nn[";
do
{
std::cout << looper->number;
if (looper->next != NULL)
std::cout << ", ";
looper = looper->next;
}while (looper != NULL);
std::cout << "]n" << std::endl;
}
void append()
{
Node *temp = new Node;
std::cout << "Enter data to be appended : ";
std::cin >> temp->number;
temp->next = NULL;
if(head == NULL)
head = temp;
else if(head->next == NULL)
head->next = temp;
else
{
Node *looper = head;
while (looper->next != NULL)
{
looper = looper->next;
}
looper->next = temp;
}
}