简单链表

// NodeTest.cpp : 定义控制台应用程序的入口点。
//简单链表

#include "stdafx.h"
#include "stdlib.h"
struct node {
	int data;
	struct node *next;
};
int _tmain(int argc, _TCHAR* argv[])
{
	struct node *head, *p, *q, *t;
	int i, n, a;
	q = NULL;//先初始化,否则报错
	scanf_s("%d", &n);//输入输入几个数
	head = NULL;//头指针初始化为空
	for(i=1;i<=n;i++) {
		scanf_s("%d", &a);
		p = (struct node *)malloc(sizeof(struct node));
		p->data = a;//将数据存储到当前节点中
		p->next = NULL;//将当前节点的后继指针指向空
		if(head == NULL) {
			head = p;//如果是第一个节点,则将头指针指向当前节点
		} else {
			q->next = p;//如果不是第一个节点,则将上一个节点的后继指针指向当前节点
		}
		q = p;

	}
	t = head;
	while(t != NULL) {
		printf("%d ", t->data);
		t = t->next;
	}
	getchar();
	getchar();
	return 0;
}

  

原文地址:https://www.cnblogs.com/fancing/p/8634179.html