简单的链表——C语言

/*
	内容:综合应用——创建简单链表
	目的:通过创建简单的单链表,熟悉指针
	基础:
		1.单链表又名线性链表。包含数据域和指针域。
		2.首节点通过头指针可以找到,其他通过前驱节点的link域找到
		3.最后的节点没有后继,一般为空指针NULL
*/
# include "stdlib.h"
# include "stdio.h"

struct list
{
	int data;
	struct list* next;
};

typedef struct list node;
typedef node * link;
int main()
{
	link ptr, head;
	int num, i;
	ptr = (link)malloc(sizeof(node));
	head = ptr;
	printf("please input 5 numbers ==>\n");
	for (i=0; i<=4; i++)
	{
		scanf_s("%d", &num);
		ptr->data = num;
		ptr->next = (link)malloc(sizeof(node));
		if (i == 4)
			ptr->next = NULL;
		else
			ptr = ptr->next;
	}

	ptr = head;
	while (ptr != NULL)
	{
		printf("The value is ==>%d\n", ptr->data);
		ptr = ptr->next;
	}


}

/*
--------------------------------------------------------
在VS中运行结果为:
please input 5 numbers ==>
2
5
9
8
6
The value is ==>2
The value is ==>5
The value is ==>9
The value is ==>8
The value is ==>6
---------------------------------------------------------
*/

你可能感兴趣的:(链表,指针,单链表)