自己学习C语言关于链表的基础理解

链表给我的感觉就挺麻烦的,至于说是很好修改我表示太菜了还没接触到

废话不多说,先贴一段代码

#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct student
{
 char name[10];
 struct student *next;
};
struct student *create()
{
 struct student *head,*current,*next2;
 char str[10];
 char flag;
 printf("please input your name:\n");
 scanf("%s",&str);
 getchar();
 head=(struct student*)malloc(sizeof(struct student));
 strcpy(head->name,str);
 current=head;
 printf("是否继续输入:");
 scanf("%c",&flag);
 while(flag!='n')
 {
  printf("pleas input your name:\n");
  scanf("%s",&str);
  getchar();
  next2=(struct student*)malloc(sizeof(struct student));
  strcpy(next2->name,str);
  current->next=next2;
  current=next2;
  printf("是否继续输入:");
  scanf("%c",&flag) ;
 }
 current->next=NULL;
 return head;
 
}
int main()
{
 struct student *p;
 p=create();
 while(1)
 {
  if(p->next!=NULL)
  {
   printf("%s\n",p->name);
   p=p->next;
  }else
  {
   printf("%s\n",p->name);
   break;
  }
 }
}

这代码是我学习视频之后跟着边打边修改出来的基本和视频差不多 视频代码https://www.bilibili.com/video/av418203

不看还不知道这东西居然是12年的....(感谢作者)


视频中说得很详细(感谢视频作者说的这么详细,我这个渣渣都能听懂)

我最大的感受可能就是这玩意的结构给我感觉是这样的

head current

current

current

....

current

一点就是这货不用区分current就能输出,这一点可能是我没弄明白(因为以数组的形式来输出的话是肯定要区分的比如stu[])

几乎所有的数据在current上,head主要就是给个返回值,给个头,让你好从上往下撸一遍其中还有学到了getchar()清除缓存的功能,怪不得我以前做的简单循环计算器有蜜汁错误,每次回车都给scanf了 


如上有什么好的理解希望能交流,有错误希望指出。


猜你喜欢

转载自blog.csdn.net/asukari/article/details/80519671