如何在 C++ 中创建带输入的单向链表
How do you create a singly linked list with input in C++
我们的任务是创建一个具有以下输出的链表:
Enter number of nodes: 5
12 4 5 44 45
The linked list:
12 4 5 44 45
我对链表很困惑,我的代码如下
#include <iostream>
using namespace std;
struct Node{
int data;
int nodeNumber;
Node *next;
}*nodePtr=NULL,*nodeTemp=NULL,*nodeHead=NULL;
void addNode (int num, int nodeSize);
void displayNode();
int main(){
int size, value;
cout<<"Enter number of Nodes: ";
cin>>size;
for (int i=0; i<size; i++){
cin>>value;
addNode(value,size);
//These are just to check if the data is being stored correctly in the structs
cout<<nodeTemp->data;
cout<<nodePtr->data;
cout<<nodeHead->data;
}
displayNode();
system("pause>0");
}
void addNode(int num, int nodeSize){
int i = 0;
nodePtr = new Node;
nodePtr->data = num;
nodePtr->next = NULL;
if (nodePtr->next == NULL){
nodeHead = nodePtr;
nodeTemp = nodePtr;
nodePtr->next = nodeTemp;
}
else
while(i<nodeSize){
nodeTemp->next = nodeTemp;
}
nodeTemp->next = NULL;
}
void displayNode(){
nodeTemp = new Node;
nodeTemp = nodeHead;
cout<<"The linked list: ";
while(nodeTemp != NULL){
cout<<nodeTemp->data<<" ";
nodeTemp = nodeTemp->next;
}
}
然而,代码只输出最后输入的值。当我检查数据时(见第 22、23、24 行),节点指针、temp 和 head 似乎总是具有相同的值。我对我的错误可能是什么感到困惑,任何帮助都会非常好。
谢谢。
在列表末尾插入节点时不需要 size
变量。
像下面这样更改您的函数:
void addNode(int num){
nodePtr = new Node;
nodePtr->data = num;
nodePtr->next = NULL;
/* if head is null, or list is empty! */
if (nodeHead == NULL){
nodeHead = nodePtr;
}
else{
/* traverse the list to the end and add node at the NULL position */
/* create a chain of nodeHead to nodeTemp */
nodeTemp = nodeHead;
while(nodeTemp-> next != NULL){
nodeTemp = nodeTemp->next;
}
nodeTemp->next = nodePtr;
}
}
您不必将 size 变量传递给 addNode 函数。只需在主函数中获取您想要的值,然后通过循环直到大小将它们传递给 addNode 函数,然后像往常一样将节点添加到列表中。
您的代码将如下所示:
#include <iostream>
using namespace std;
struct Node{
int data;
int nodeNumber;
Node *next;
};
Node *nodeHead=NULL;
void addNode (int num);
void displayNode();
int main(){
int size, value;
cout<<"Enter number of Nodes: ";
cin>>size;
for (int i=0; i<size; i++){
cin>>value;
addNode(value);
}
displayNode();
system("pause>0");
}
void addNode(int num){
Node *tail = nodeHead;
Node *nodePtr = new Node;
nodePtr->data = num;
nodePtr->next = NULL;
if (nodeHead == NULL){
nodeHead = nodePtr;
}
else{
while(tail->next != NULL){
tail = tail->next;
}
tail->next = nodePtr;
}
}
void displayNode(){
Node *nodeTemp = new Node;
nodeTemp = nodeHead;
cout<<"The linked list: ";
while(nodeTemp != NULL){
cout<<nodeTemp->data<<" ";
nodeTemp = nodeTemp->next;
}
}
我们的任务是创建一个具有以下输出的链表:
Enter number of nodes: 5
12 4 5 44 45
The linked list:
12 4 5 44 45
我对链表很困惑,我的代码如下
#include <iostream>
using namespace std;
struct Node{
int data;
int nodeNumber;
Node *next;
}*nodePtr=NULL,*nodeTemp=NULL,*nodeHead=NULL;
void addNode (int num, int nodeSize);
void displayNode();
int main(){
int size, value;
cout<<"Enter number of Nodes: ";
cin>>size;
for (int i=0; i<size; i++){
cin>>value;
addNode(value,size);
//These are just to check if the data is being stored correctly in the structs
cout<<nodeTemp->data;
cout<<nodePtr->data;
cout<<nodeHead->data;
}
displayNode();
system("pause>0");
}
void addNode(int num, int nodeSize){
int i = 0;
nodePtr = new Node;
nodePtr->data = num;
nodePtr->next = NULL;
if (nodePtr->next == NULL){
nodeHead = nodePtr;
nodeTemp = nodePtr;
nodePtr->next = nodeTemp;
}
else
while(i<nodeSize){
nodeTemp->next = nodeTemp;
}
nodeTemp->next = NULL;
}
void displayNode(){
nodeTemp = new Node;
nodeTemp = nodeHead;
cout<<"The linked list: ";
while(nodeTemp != NULL){
cout<<nodeTemp->data<<" ";
nodeTemp = nodeTemp->next;
}
}
然而,代码只输出最后输入的值。当我检查数据时(见第 22、23、24 行),节点指针、temp 和 head 似乎总是具有相同的值。我对我的错误可能是什么感到困惑,任何帮助都会非常好。
谢谢。
在列表末尾插入节点时不需要 size
变量。
像下面这样更改您的函数:
void addNode(int num){
nodePtr = new Node;
nodePtr->data = num;
nodePtr->next = NULL;
/* if head is null, or list is empty! */
if (nodeHead == NULL){
nodeHead = nodePtr;
}
else{
/* traverse the list to the end and add node at the NULL position */
/* create a chain of nodeHead to nodeTemp */
nodeTemp = nodeHead;
while(nodeTemp-> next != NULL){
nodeTemp = nodeTemp->next;
}
nodeTemp->next = nodePtr;
}
}
您不必将 size 变量传递给 addNode 函数。只需在主函数中获取您想要的值,然后通过循环直到大小将它们传递给 addNode 函数,然后像往常一样将节点添加到列表中。 您的代码将如下所示:
#include <iostream>
using namespace std;
struct Node{
int data;
int nodeNumber;
Node *next;
};
Node *nodeHead=NULL;
void addNode (int num);
void displayNode();
int main(){
int size, value;
cout<<"Enter number of Nodes: ";
cin>>size;
for (int i=0; i<size; i++){
cin>>value;
addNode(value);
}
displayNode();
system("pause>0");
}
void addNode(int num){
Node *tail = nodeHead;
Node *nodePtr = new Node;
nodePtr->data = num;
nodePtr->next = NULL;
if (nodeHead == NULL){
nodeHead = nodePtr;
}
else{
while(tail->next != NULL){
tail = tail->next;
}
tail->next = nodePtr;
}
}
void displayNode(){
Node *nodeTemp = new Node;
nodeTemp = nodeHead;
cout<<"The linked list: ";
while(nodeTemp != NULL){
cout<<nodeTemp->data<<" ";
nodeTemp = nodeTemp->next;
}
}