C语言--新建链表(个人程序记录)

来源:互联网 发布:吉利汽车硕士待遇 知乎 编辑:程序博客网 时间:2024/06/03 09:17
#include <stdio.h>#include <malloc.h>struct  dataclass{int a;struct dataclass *next;//指针指向下一个结构地址 };int main(){/*p2存放上次的地址因为拓展新的以后要将旧的next指向新的地址。p1是用来存放新拓展的结构的。head是存放第一个的地址。 */ struct dataclass *p2,*p1,*head;//拓展大小的固定写法 (struct 结构名 *)malloc(sizeof(struct 结构名));p2=p1=(struct dataclass *)malloc(sizeof(struct dataclass));//第一次的地址存放到head中 head=p2;//输入第一个值; scanf("%d",&p1->a);//开始循环输入值,直到我输入的值为负数时停止while( (p1->a)>=0){//拓展一个地址 p1=(struct dataclass *)malloc(sizeof(struct dataclass));//上次结构中的next指向新的地址 p2->next=p1;//重新给p2储存的地址做改变 p2=p1;//输入值存到刚拓展的 scanf("%d",&p1->a);} //将p2指空 p2->next=NULL;//遍历输出do{printf("%d\n",head->a);head=head->next;} while (head!=NULL);} 

1 0
原创粉丝点击