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