欢迎来到天天文库
浏览记录
ID:52141381
大小:72.50 KB
页数:9页
时间:2020-03-23
《软件技术基础上机实验报告(链表).doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、ex2:链表的插入与删除1)首先创建一个单链表:从键盘读入五个整数,按输入顺序形成单链表。将创建好的链表元素依次输出到屏幕上。2)在已创建好的链表中插入一个元素:从键盘读入元素值和插入位置,调用插入函数完成插入操作。然后将链表元素依次输出到屏幕上。3)在已创建好的链表中删除一个元素:从键盘读入欲删除的元素位置(序号),调用删除函数完成删除操作。然后将链表元素依次输出到屏幕上。4)从键盘任意输入一个整数,在单链表中查询该数,如果单链表中已经存在这个数,就调用删除函数,删除该元素所在结点,并将单链表在删除前后的数据元素依
2、次输出到屏幕上;如果单链表中不存在这个数,就调用插入函数,将这个数插入到单链表尾,并将单链表在插入前后的数据元素依次输出到屏幕上软件技术基础上机实验报告姓名:肖燕平学号:2011019090028上机实验二Ex2_l(链表的创建和插入删除)#include#ineludetypedefstructnode_type〃定义链点{intdata;structnode_type*next;}node_type;typedefstructlist_type〃定义链表{node_type*
3、head;node_type*tail;intlength;}list_type;intread(){intx;scanf(”%d",&x);returnx;}voiderror(intx){switch(x){case1:printf("theplaceofthedataiswrong,pleaseinputtheplaceagainrT);break;}}voidcreat_list(list_type*lianbiao)〃创建链表{nodejype*p,*s;〃注意此处的指针要为链点结构体类型intx;l
4、ianbiao->head=(node_type*)malloc(sizeof(node_type));lianbiao->length=O;p=lianbiao->head;〃输入五个数while(lianbiao・>length<5)scanf(”%cT,&x);s=(node_type*)malloc(sizeof(node_type));s->data=x;p->next=s;P=S;〃在链点连接上出现了问题导致后面显示链表时也出问题lianbiao->length++;}p->next=NULL;lianb
5、iao->tail=p;}voidshow_list(list_type*lianbiao)〃把链表元素打印出来{node_type*p;p=lianbiao->head・>next;printf("Thelinkedlistis");while(p!=NULL){printf("%d",p->data);p=p->next;//p往下走一步}printf("Thelengthofthislinkedlistis%d",lianbiao->length);}voidinsert_list(list
6、_type*lianbiao,intnewdatajntplace){node_type*newnode,*p;inti=0;newnode=(node_type*)malloc(sizeof(node_type));newnode->data=newdata;p=lianbiao・>head;while(lianbiao->length+l7、位置的前一个p=p->next;i++;}newnode-〉next=p->next;p・>next二newnode;讦(newnode・>next==NULL){lianbiao・>tail=newnode;}lianbiao->length++;〃插入链点〃若插入的位置为表尾,则改变尾指针voiddeletejist(list_type*lianbiao」ntplace){inti=0;node_type*p,*g;p=lianbiaoohead;while(place>lianbiao->length11pla8、cenext;i++;}g=p->next;p・>next=p->next->next;free(g);if(p->next==NULL){lianbiao->tail=p;}lianbi
7、位置的前一个p=p->next;i++;}newnode-〉next=p->next;p・>next二newnode;讦(newnode・>next==NULL){lianbiao・>tail=newnode;}lianbiao->length++;〃插入链点〃若插入的位置为表尾,则改变尾指针voiddeletejist(list_type*lianbiao」ntplace){inti=0;node_type*p,*g;p=lianbiaoohead;while(place>lianbiao->length11pla
8、cenext;i++;}g=p->next;p・>next=p->next->next;free(g);if(p->next==NULL){lianbiao->tail=p;}lianbi
此文档下载收益归作者所有