Inserting a node at beginning,全局变量头指针【1月16日学习笔记】

点击查看代码
//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();
	}
 
}
posted @ 2024-01-17 09:23  bituion  阅读(8)  评论(0)    收藏  举报