(十二)C语言入门,代码编程,初识链表【100行】【原创】

本文详细介绍了如何在Visual Studio 2019编译环境中使用C语言创建链表,包括结构定义、节点插入函数和主函数中链表的操作。重点讲解了`struct spy`的使用和`insert_spy`函数的应用。
摘要由CSDN通过智能技术生成

目录

十二篇文章汇总,独家吐大血整理

编译环境

运行结果

 test12.c


编译环境

编译环境:VS2019

运行结果

 test12.c

#include <stdio.h>

//struct spy//以下注释都是我写的错误代码
//{
//	char name;
//	struct spy *next;
//}spy1;
//
//struct spy
//{
//	char name;
//	struct spy* next;
//}spy2;
//
//struct spy
//{
//	char name;
//	struct spy* next;
//}spy3;
//
//int main() 
//{
//	struct spy1 = {'A',NULL};
//	struct spy2 = { 'B',NULL };
//	struct spy3 = { 'C',NULL };
//
//	struct spy* head = NULL;
//	head = &spy1;
//	while (1)
//	{
//
//	}
//
//	return 0;
//}

typedef struct spy
{
	char name;
	struct spy* next;
}spy, *p_spy;

spy A = { 'A', NULL };
spy B = { 'B', NULL };
spy C = { 'C', NULL };
spy D = { 'D', NULL };

p_spy head = NULL;

void insert_spy(p_spy newspy)
{
	p_spy last = NULL;
	if (NULL == head)//说明此时头部没有指向,因为这个插入就是第一个
	{
		head = newspy;
		newspy->next = NULL;//尾巴为空
	}
	else
	{
		last = head;//头部不为空,说明不是第一个了
		while (last->next)//跳出说明遇到了最后一个
		{
			last = last->next;
		}

		last->next = newspy;//此时插在上面跳出的那个后面
		newspy->next = NULL;
	}
}


int main()
{
	//p_spy* head = NULL;//一开始写成这样了,肯定不对


	//while (1);//这个while循环代码不报错,但是运行报错
	//{
	//	head = &A;
	//	printf("%c\r\n",head->name);
	//}

	//A.next = &B;
	//B.next = &C;
	//C.next = &D;
	//D.next = NULL;

	insert_spy(&A);//用这四行代替上面四行结果一样
	insert_spy(&B);
	insert_spy(&C);
	insert_spy(&D);

	head = &A;

	//while (1);//这行代码也是不报错,运行报错
	//{
	//	printf("%c\r\n", head->name);
	//	head = head->next;
	//}

	while (head)//我靠,原来是while()后面加了个;号,所以才出错,淦
	{
		printf("%c\r\n", head->name);
		head = head->next;
	}



	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值