自我介绍
这是我开始第一次写这样的微博,瞬间感觉自己很有优越感了,其实还是一条咸鱼的样子。我是林中旬,一个还没有出道的码农。正在学习C语言。在学习之前对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,*new;
char str[10];
char flag;
printf("please inpurt a name\n");
scanf("%s",str);
getchar();
head=(struct student *)malloc(sizeof(struct student *));
current = head;
printf("please input 'n' to stop\n""or 'y' to continue");
scanf ("%c",&flag);
while (flag!='n')
{
printf("plaese input name :\n");
scanf("%s",str);
getchar();
new=(struct student*)malloc (sizeof(struct student));
strcpy(new->name,str);
current->next=new;
current=new;
printf(" do you want continue?");
scanf("%c",&flag);
current->next=NULL;
}
return head;
}
int main()
{
struct student *p;
p=create();
while(1)
{
printf("%s\n",p->name);
if (p->next!=NULL)
{
p=p->next;
}
else
{
break;
}
}
return 0;
}