/*
* C++ 用指针的引用,好多了! 考虑得更简单。
*/
#include <iostream>
#include <cstdlib>
#include <cstring>
#define BUG cout << "here\n";
#define STOP system("pause");
using namespace std;
struct node {
int value;
node* next;
node() {
value = 0;
next = NULL;
}
};
void addToTail(node* &phead, int value) {
node* pn = new node();
pn->value = value;
if(phead == NULL) { // 考虑要全面。
phead = pn;
}
else {
node* p = phead;
while(p->next != NULL) {
p = p->next;
}
p->next = pn;
}
}
void addToTail(node** phead, int value) { // 之所以用指向指针的指针 -- 理解 复杂化的简单对比思想来理解
node* pn = (node*)malloc(sizeof(node));
pn->next = NULL;
pn->value = value;
if(*phead == NULL) { // 考虑要全面。
*phead = pn;
}
else {
node* p = *phead;
while(p->next != NULL) {
p = p->next;
}
p->next = pn;
}
}
int main() {
node* head = NULL;
addToTail(&head, 10);
addToTail(&head, 20);
node* p = head;
while(p != NULL) {
printf("%d->", p->value);
p = p->next;
}
printf("NULL");
STOP
return 0;
}