C语言链表学习

学校学习的C语言并没有涉及链表的深入学习,今天又重新学习链表。
首先链表类似于一节一节的火车厢,你可以把每个车厢分成两部分,一部分用来存储本车厢数据,另外一部分存放下一节车厢的地址。
你只要知道第一个车厢的地址,就可以在第一节车厢找到第二节车厢,以此类推。
链表的地址不需要连续,当你需要一个新车厢时可以进行动态内存申请。
并附上程序:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct Student //定义节点
{
char name[10];
struct Student *link;
};

struct Student *creat()
{
char str[10],flag;
struct Student *head,*next,*current;
printf(“please input name:\n”);
scanf("%s",str);
getchar();
head = (struct Student *)malloc(sizeof(struct Student));//申请动态空间
strcpy(head->name,str);
printf(“是否继续输入:Y/N \n”);
scanf("%c",&flag);
getchar();
current = head;
while(flag==‘Y’)
{
printf(“please input name:\n”);
scanf("%s",str);
getchar();
next = (struct Student *)malloc(sizeof(struct Student));//申请动态空间
strcpy(next->name,str);
current->link = next; //当前节点的指针区存储下一个节点地址
current = next;
printf(“是否继续输入:Y/N \n”);
scanf("%c",&flag);
getchar();
}
current->link = NULL;
return head;
}

int main()
{
struct Student *p;
p = creat();
while(1)
{
if(p->link != NULL)
{
printf("%s\n",p->name);
p=p->link;
}
else
break;
}
printf("%s\n",p->name);
system(“pause”);
return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值