c++ - 链表插入和打印 C++
问题描述
所以我试图使用双指针来创建插入函数,然后打印链接列表。
我设法用单指针做到了,但是这个双指针让我发疯了。
#include <iostream>
#include <string>
using namespace std;
class Node {
public:
string name;
int ID;
int marks[10];
Node *next;
};
void printOptions() {
cout << endl;
cout << "1.Insert New Node" << endl;
cout << "2.Print List" << endl;
cout << "3.Exit" << endl;
}
void insertAtBack(string inputName, Node **headref) {
Node **currentNodeRef;
currentNodeRef = headref;
while ((*currentNodeRef)->next != NULL) {
(*currentNodeRef) = (*currentNodeRef)->next;
}
(*currentNodeRef)->next = new Node();
(*currentNodeRef)->next->name = inputName;
(*currentNodeRef)->next->next = NULL;
}
void printList(Node *head) {
Node *indexNode;
indexNode = head;
while (indexNode != NULL) {
cout << (indexNode)->name << endl;
(indexNode) = (indexNode)->next;
}
}
int main() {
cout << "This implements a linked list" << endl;
int option;
bool infinite = true;
Node *head = NULL;
string testName;
while (infinite == true) {
printOptions();
std::cin >> option;
switch (option) {
case 1:
cout << "Enter student name" << endl;
std::cin >> testName;
if (head == NULL) {
head = new Node();
head->name = testName;
}
else {
insertAtBack(testName, &head);
}
break;
case 2:
printList(head);
break;
case 3:
exit(1);
break;
default:
exit(1);
break;
}
}
return 0;
}
所以没有编译错误或段错误,而是代码运行它接受 2 个值并很好地打印它们。当输入另一个值时,它不再打印 2 个值。我认为打印功能很好,因为它以前使用单指针工作,但我不是 100% 确定。我认为问题出在插入功能中,但我不知道在哪里。
解决方案
void insertAtBack(string inputName, Node **headref) { Node **currentNodeRef; currentNodeRef = headref; ...
Node **currentNodeRef = headref;
是一个错误。请记住,您传递的是指针的地址。你的意思是写:
Node *currentNodeRef = *headref;
并像这样更改功能:
void insertAtBack(string inputName, Node **head)
{
Node *tail = *head;
while(tail->next != NULL)
tail = tail->next;
tail->next = new Node();
tail->next->name = inputName;
tail->next->next = NULL;
}
也不要忘记初始化head->next = nullptr;
if (head == NULL) { head = new Node(); head->name = testName; head->next = nullptr; <--- add }
但是,如果insertAtBack
准备在ishead
时处理会更好。你传递的全部原因是你想要一个对指针的引用,以便你可以初始化它。因此,您可以将代码修改为:head
NULL
Node **head
void insertAtBack(string inputName, Node **head)
{
Node *new_node = new Node();
new_node->name = inputName;
new_node->next = nullptr;
if(*head)
{
Node *tail = *head;
while(tail->next)
tail = tail->next;
tail->next = new_node;
}
else
{
*head = new_node;
}
}
void printList(Node *head)
{
Node *node = head;
while(node)
{
cout << node->name << endl;
node = node->next;
}
}
int main()
{
cout << "This implements a linked list" << endl;
Node *head = NULL;
string testName;
while(true)
{
printOptions();
int option;
std::cin >> option;
switch(option)
{
case 1:
cout << "Enter student name" << endl;
std::cin >> testName;
insertAtBack(testName, &head);
break;
case 2: printList(head); break;
case 3: exit(1); break;
default: exit(1); break;
}
}
return 0;
}
推荐阅读
- java - 使用默认值转换为 ISO 字符串
- java - 骆驼解组jsonpath到pojo
- javascript - 如何在javascript中使用正则表达式在数组中进行搜索
- php - 在具有旧值的刀片 laravel 中显示数据库中不同输入字段的用户现有数据
- mysql - 如何验证是否选择了特定值并将其存储在 Laravel
- angular - 如何在 Angular 库的 umdModuleIds 中添加 angular-redux/store 的条目
- aws-lambda - 尝试通过 api 写入 AWS dynamo db
- c# - WPF矩形标签模糊
- tomcat - 无法在 Jersey-HTTP 415 Unsupported Media Type 中实现简单文件上传
- java - 错误,无法在此处访问无效/释放的位图!在使用包含位图的模型类时