一步一步复习数据结构和算法基础-链表(1)

本文介绍了链表的基础概念及其实现方式,并通过一个逆序打印链表的示例程序详细解析了链表的工作原理。重点讲解了如何通过将新节点插入到链表头部来达到逆序打印的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据结构这门课程中,我首先接触的就是链表.

链表是一种相对简单的数据结构,但是我在编写程序的时候经常运行出错.

为什么?自己对链表的掌握不足.

自己温习了一下课本,写了最基础的链表逆序打印.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char elemtype;
typedef struct list
{
	elemtype ch;
	struct list *next;
}*linklist,listnode;
linklist creat_list(linklist head)
{
	linklist node,tmp;
	char temp;
	head = NULL;
	/*输入字符以'#'号结束*/
	while((temp = getchar())!='#')
	{
		node =  (linklist)malloc(sizeof(listnode));
		node->ch = temp;
		node->next = head;
		head = node;
	}
	return head;
}
/*逆序打印链表*/
void print(linklist head)
{
	linklist temp = head;
	while(temp)
	{
		printf("%c ",temp->ch);
		temp = temp->next;
	}
}
int main()
{
	linklist head;
	head = creat_list(head);
	print(head);
	return 0;
}
为什么是逆序打印链表呢?

原因是这一段程序

while((temp = getchar())!='#')
	{
		node =  (linklist)malloc(sizeof(listnode));
		node->ch = temp;
		node->next = head;
		head = node;
	}
这一段代码说明了每次加入新的节点,都会讲该节点加入链表的标头.


今天先写这一点.自己正忙着期末考试.静待暑假.....


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值