显示列表只显示节点的头部
display list is only displaying the head of the node
我写了一段代码,要求用户输入自定义列表,我想显示它,但它只显示头节点。
我想知道问题出在显示功能还是输入功能上。我想稍后删除显示功能,但我希望创建我的列表。
这是我的代码:
#pragma once
#include <cstddef>
#include <iostream>
using namespace std;
struct node {
char data;
node* next;
};
node* inputList();
void displayList(node*);
int exercice1() {
node* head;
head = inputList();
displayList(head);
return 0;
}
node* inputList() {
node* tmp;
node* cur, *head;
int n;
do {
cout << "enter the number of nodes: ";
cin >> n;
} while (n <= 0);
tmp = new node;
cout << "enter the values inside each node: ";
cin >> tmp->data;
head = tmp;
cur = tmp;
for (int i = 1; i < n; i++) {
tmp = new node;
cur = cur->next;
cout << "enter a node: ";
cin >> tmp->data;
cur = tmp;
}
cur->next = NULL;
return head;
}
void displayList(node* head) {
if (head != NULL) {
cout << head->data;
displayList(head->next);
}
}
inputList
无法将节点 link 在一起。我们可能可以逃脱
tmp = new node;
cur->next = tmp; // point current node's next at new node
cur = cur->next;
但这里有一个更简洁的方法:
node* inputList() {
node * head; // storage for the start of the list. Keeps track so we
// know what to return when we're done.
node ** insert = &head; // store where we're going to insert a node rather than
// tracking the current node with another variable.
// this is both tmp and cur by always pointing at where
// the next node is going to go.
int n;
do {
cout << "enter the number of nodes: ";
cin >> n;
} while (n <= 0);
*insert = new node; //node goes right into head
cout << "enter the values inside each node: ";
cin >> (*insert)->data;
insert = &(*insert)->next; // now the insertion point points at head's next
for (int i = 1; i < n; i++) {
*insert = new node; // new node gets linked right in next
cout << "enter a node: ";
cin >> (*insert)->data;
insert = &(*insert)->next; // advance to next of the node we just added
}
*insert = NULL; // terminate list by pointing the insertion point at NULL
return head;
}
现在他已经将 head
抽象为另一个插入点,如 next
,我们可以消除一些重复的代码:
node* inputList() {
node * head;
node ** insert = &head; // head is no different from a next. It just
// has a different name
int n;
do {
cout << "enter the number of nodes: ";
cin >> n;
} while (n <= 0);
// no special handling for head.
for (int i = 0; i < n; i++) { // iteration now starts at 0. The loop
// builds all of the nodes.
*insert = new node;
cout << "enter a node: ";
cin >> (*insert)->data;
insert = &(*insert)->next;
}
*insert = NULL;
return head;
}
我写了一段代码,要求用户输入自定义列表,我想显示它,但它只显示头节点。
我想知道问题出在显示功能还是输入功能上。我想稍后删除显示功能,但我希望创建我的列表。
这是我的代码:
#pragma once
#include <cstddef>
#include <iostream>
using namespace std;
struct node {
char data;
node* next;
};
node* inputList();
void displayList(node*);
int exercice1() {
node* head;
head = inputList();
displayList(head);
return 0;
}
node* inputList() {
node* tmp;
node* cur, *head;
int n;
do {
cout << "enter the number of nodes: ";
cin >> n;
} while (n <= 0);
tmp = new node;
cout << "enter the values inside each node: ";
cin >> tmp->data;
head = tmp;
cur = tmp;
for (int i = 1; i < n; i++) {
tmp = new node;
cur = cur->next;
cout << "enter a node: ";
cin >> tmp->data;
cur = tmp;
}
cur->next = NULL;
return head;
}
void displayList(node* head) {
if (head != NULL) {
cout << head->data;
displayList(head->next);
}
}
inputList
无法将节点 link 在一起。我们可能可以逃脱
tmp = new node;
cur->next = tmp; // point current node's next at new node
cur = cur->next;
但这里有一个更简洁的方法:
node* inputList() {
node * head; // storage for the start of the list. Keeps track so we
// know what to return when we're done.
node ** insert = &head; // store where we're going to insert a node rather than
// tracking the current node with another variable.
// this is both tmp and cur by always pointing at where
// the next node is going to go.
int n;
do {
cout << "enter the number of nodes: ";
cin >> n;
} while (n <= 0);
*insert = new node; //node goes right into head
cout << "enter the values inside each node: ";
cin >> (*insert)->data;
insert = &(*insert)->next; // now the insertion point points at head's next
for (int i = 1; i < n; i++) {
*insert = new node; // new node gets linked right in next
cout << "enter a node: ";
cin >> (*insert)->data;
insert = &(*insert)->next; // advance to next of the node we just added
}
*insert = NULL; // terminate list by pointing the insertion point at NULL
return head;
}
现在他已经将 head
抽象为另一个插入点,如 next
,我们可以消除一些重复的代码:
node* inputList() {
node * head;
node ** insert = &head; // head is no different from a next. It just
// has a different name
int n;
do {
cout << "enter the number of nodes: ";
cin >> n;
} while (n <= 0);
// no special handling for head.
for (int i = 0; i < n; i++) { // iteration now starts at 0. The loop
// builds all of the nodes.
*insert = new node;
cout << "enter a node: ";
cin >> (*insert)->data;
insert = &(*insert)->next;
}
*insert = NULL;
return head;
}