【发布时间】:2015-08-18 20:31:27
【问题描述】:
我创建了一个单链表:
#include <iostream>
using namespace std;
struct Node{
int data;
Node *next;
};
bool isEmpty(Node *head){
if (head == NULL){
return true;
}
else{
return false;
}
}
void append(Node *head, Node *last, int data){
Node *newNode = new Node;
newNode->data = data;
newNode->next = NULL;
if (isEmpty(head)){
head = newNode;
last= newNode;
}
else{
last->next = newNode;
last= newNode;
}
}
void printList(Node *current){
if (isEmpty(current)){
cout << "List is empty." << endl;
}
else{
int i = 1;
while (current != NULL){
cout << i << ". Node: " << endl;
cout << current->data << endl;
current = current->next;
i++;
}
}
}
void main(){
Node *head = NULL;
Node *last = NULL;
append(head, last, 53);
append(head, last, 5512);
append(head, last, 13);
append(head, last, 522);
append(head, last, 55);
printList(head);
}
当我编译它时,输出是这样的:
列表为空。
但我不知道为什么。 “head”获取地址,因此“head”不应为 NULL。但显然它是NULL。 我不知道如何解决这个问题。
【问题讨论】:
-
您已标记 C++11:开始使用
nullptr代替NULL。 -
void append(Node*& head, Node*& last, int data).
标签: c++ list c++11 data-structures singly-linked-list