跑骚时刻

#include "stdafx.h"
#include <stdlib.h>

//声明函数
//创建一个链表
struct Node * create_list(void);
//遍历链表中的内容
void traverse_list(struct Node *);

//链表的结构体
struct Node
{
	//数据域
	int data;
	char data2;
	//指针域
	struct Node * pNext;
};

//主函数
int main(int argc, _TCHAR* argv[])
{
	/*
	2014年5月20日 20:30:48		链表
	*/

	//声明一个链表指针
	struct Node * pHead = NULL;
	//获取链表
	pHead = create_list();
	//循环输出链表的内容
	traverse_list(pHead);



	
	printf("
");
	system("pause");
	return 0;
}

//创建一个链表
struct Node * create_list(void){
	int len;//用来存放节点的个数
	int val;//用于存放节点的值
	//头节点
	struct Node * pHead = (struct Node*)malloc(sizeof(struct Node));
	if (NULL == pHead)
	{
		printf("分配失败!程序终止");
		exit(-1);//终止程序
	}
	struct Node * pTail = pHead;//首节点
	pTail->pNext = NULL;

	printf("请输入需要生成的链表节点个数:len=");
	scanf_s("%d", &len);
	for (int i = 0; i < len; i++)
	{
		printf("请输入第%d个节点的值:", i + 1);
		scanf_s("%d", &val);

		struct Node * pNew = (struct Node*)malloc(sizeof(struct Node));
		if (NULL == pNew)
		{
			printf("分配失败!程序终止");
			exit(-1);
		}
		pNew->data = val;//为当前节点赋值
		pTail->pNext = pNew;//为上一个节点的指针域赋值
		pNew->pNext = NULL;//清空当前节点的指针域,如果当前为NULL。当前节点为链表的最后一个值
		pTail = pNew;//更新当前节点为:下一个节点的上一个节点;
	}
	return pHead;//返回的是头结点
}

//遍历链表中的内容
void traverse_list(struct Node * pHead){
	struct Node * p = pHead->pNext;
	printf("链表中的值为:");
	while (NULL != p)
	{
		printf("%d  ", p->data);
		p = p->pNext;
	}
}

  

原文地址:https://www.cnblogs.com/oncoy/p/3740972.html