点击查看代码
//inserting a node at beginning,局部变量头指针版本2
#include<iostream>
using namespace std;
struct node {
int data;
node* next;
};
void insert(int x, node** A) {
node* temp = new node;//创建新节点
temp->data = x;
temp->next = *A;//新节点尾巴指向1节点(无则NULL)
*A = temp;//头指针指向新节点
}
void print(node* run) {
while (run != NULL) {
cout << run->data << " ";
run = run->next;
} cout << endl;
}//注意没修改头指针
int main() {
node* A = NULL;//局部变量
int x;
while (cin >> x) {
insert(x, &A);//A地址传参
print(A);
}
}