目录
编译环境
编译环境: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;
}