点击查看代码
//inserting a node at beginning,局部变量头指针版本1
#include<iostream>
using namespace std;
struct node {
int data;
node* next;
};
node* insert(int x,node* A) {
node* temp = new node;//创建新节点
temp->data = x;
temp->next = A;//新节点尾巴指向1节点(无则NULL)
A = temp;//头指针指向新节点
return A;
}//返回作为新的头指针
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) {
A = insert(x, A);
print(A);
}
}