电子科大软基第二次上机实验报告_第1页
电子科大软基第二次上机实验报告_第2页
电子科大软基第二次上机实验报告_第3页
电子科大软基第二次上机实验报告_第4页
电子科大软基第二次上机实验报告_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

12/12软基第二次上机实验报告EX2.1程序流程说明1)首先创建一个单链表:从键盘读入五个整数,按输入顺序形成单链表。将创建好的链表元素依次输出到屏幕上。2)在已创建好的链表中插入一个元素:从键盘读入元素值和插入位置,调用插入函数完成插入操作。然后将链表元素依次输出到屏幕上。3)在已创建好的链表中删除一个元素:从键盘读入欲删除的元素位置(序号),调用删除函数完成删除操作。然后将链表元素依次输出到屏幕上。二、程序代码#include<stdio.h>#include<malloc.h>/*定义链点*/typedefstructnode_type{intdata;structnode_type*next;}node_type;/*定义链表*/typedefstructlist_type{node_type*head;node_type*tail;intlength;}list_type;intread(){intx;scanf("%d",&x);returnx;}voiderror(intx){switch(x){case1:printf("\ntheplaceofthedataiswrong,pleaseinputtheplaceagain\n");break;}}/*创建链表*/voidcreatelist(list_type*lb) {node_type*p,*s; intx;lb->head=(node_type*)malloc(sizeof(node_type));lb->length=0;p=lb->head;while(lb->length<5) //输入五个数{scanf("%d",&x);s=(node_type*)malloc(sizeof(node_type));s->data=x; p->next=s;p=s;lb->length++;}p->next=NULL;lb->tail=p;}/*显示元素*/voidshowlist(list_type*lb) {node_type*p;p=lb->head->next;printf("\nThelinkedlistis\n\n");while(p!=NULL){printf("%d",p->data);p=p->next; } printf("\nThelengthofthislinkedlistis%d\n",lb->length);}/*插入*/voidinsertlist(list_type*lb,intnew_data,intplace){node_type*new_node,*p;inti=0;new_node=(node_type*)malloc(sizeof(node_type));new_node->data=new_data;p=lb->head;while(lb->length+1<place||place<1){error(1);place=read(); }while(i<place-1) {p=p->next;i++;}new_node->next=p->next; //插入链点p->next=new_node;if(new_node->next==NULL){lb->tail=new_node; }lb->length++;}/*删除*/voiddeletelist(list_type*lb,intplace){inti=0;node_type*p,*g;p=lb->head;while(place>lb->length||place<1){error(1);place=read();}while(i<place-1) {p=p->next;i++;}g=p->next;p->next=p->next->next; //删除链点free(g);if(p->next==NULL) {lb->tail=p;}lb->length--; }/*主函数*/voidmain(){list_typelb;intnew_data,place;intdel_place;printf("pleaseinputthethelist\n");createlist(&lb); showlist(&lb); printf("\npleaseinputthenewdata\n");new_data=read();printf("pleaseinputtheplaceofthenewdatawhereitshouldbe\n");place=read();insertlist(&lb,new_data,place);showlist(&lb);printf("\npleaseinputtheplaceofthenumberwhichwillbedeleted\n");del_place=read();deletelist(&lb,del_place);showlist(&lb);}测试数据输入:12345输出:12345插入位置与元素:2,5输出:152345删除位置:3输出:15345上机时遇到的问题输入的头结点之类的不对,但后来及时改正实际运行结果小结体会EX2.2程序流程说明1)创建一个单链表,其数据元素为整数,从键盘输入,输入0结束(注意0不放到链表内);2)从键盘任意输入一个整数,在单链表中查询该数,如果单链表中已经存在这个数,就调用删除函数,删除该元素所在结点,并将单链表在删除前后的数据元素依次输出到屏幕上;如果单链表中不存在这个数,就调用插入函数,将这个数插入到单链表尾,并将单链表在插入前后的数据元素依次输出到屏幕上。二、程序代码#include<stdio.h>#include<malloc.h>typedefstructnode_type{intdata;structnode_type*next;}node_type;/*定义链表*/typedefstructlist_type{node_type*head;node_type*tail;intlength;}list_type;intread(){intx;scanf("%d",&x);returnx;}voiderror(intx){switch(x){case1:printf("\ntheplaceofthedataiswrong,pleaseinputtheplaceagain\n");break;}}voidcreatelist(list_type*lb){node_type*p,*s; intx;lb->head=(node_type*)malloc(sizeof(node_type));lb->length=0;p=lb->head;while(1){scanf("%d",&x);s=(node_type*)malloc(sizeof(node_type));if(x==0)break; s->data=x; p->next=s;p=s;lb->length++;}p->next=NULL;lb->tail=p;}/*显示元素*/voidshowlist(list_type*lb) {node_type*p;p=lb->head->next;printf("\nThelinkedlistis\n\n");while(p!=NULL){printf("%d",p->data);p=p->next; } printf("\nThelengthofthislinkedlistis%d\n",lb->length);}/*插入链点*/voidinsertlist(list_type*lb,intnew_data,intplace){node_type*new_node,*p;inti=0;new_node=(node_type*)malloc(sizeof(node_type));new_node->data=new_data;p=lb->head;while(lb->length+1<place||place<1){error(1);place=read(); }while(i<place-1) {p=p->next;i++;}new_node->next=p->next; p->next=new_node;if(new_node->next==NULL){lb->tail=new_node; }lb->length++;}/*删除链点*/voiddeletelist(list_type*lb,intplace){inti=0;node_type*p,*g;p=lb->head;while(place>lb->length||place<1){error(1);place=read();}while(i<place-1) {p=p->next;i++;}g=p->next;p->next=p->next->next; free(g);if(p->next==NULL) {lb->tail=p;}lb->length--; }/*寻找那个整数*/voidsearchlist(list_type*lb,intnumber){node_type*p;inti=0;intm;m=lb->length; p=lb->head;while(p->next!=NULL){while(p->next->data==number){deletelist(lb,i+1);if(p->next==NULL){break;}}if(p->next!=NULL){p=p->next;i++;}}if(i==m){insertlist(lb,number,m+1);}}/*主函数*/voidmain(){list_typelb;intnumber;printf("pleaseinputnumber:\n");createlist(&lb); showlist(&lb); printf("pleaseinputnumberwhichyouneed:\n");number=read();searchlist(&lb,number);showlist(&lb);}三、测试数据输入:123450输出:12345输入:3输出:1245输入:6输出:123456四、上机遇到的问题在WORD排版时丢失了一些数据,导致错误。五、实际运行结果小结体会失之毫厘,差之千里EX2.3程序流程说明链表存放在向量前elenum个分量重,且递增有序,现要将x插入链表的适当位置,以保持链表的有序性。二、程序代码include<stdio.h>#include<malloc.h>#defineMAXNUM20/*定义链点*/typedefstructnode_type{intdata;structnode_type*next;}node_type;/*定义链表*/typedefstructlist_type{node_type*head;node_type*tail;intlength;}list_type;intread(){intx;scanf("%d",&x);returnx;}voiderror(intx){switch(x){case1:printf("\ntheplaceofthedataiswrong,pleaseinputtheplaceagain\n");break;}}/*创建链表*/voidcreatelist(list_type*lb){node_type*p,*s; intx;lb->head=(node_type*)malloc(sizeof(node_type));lb->length=0;p=lb->head;while(1){scanf("%d",&x);s=(node_type*)malloc(sizeof(node_type));if(x==-1)break; s->data=x; p->next=s;p=s;lb->length++;}p->next=NULL;lb->tail=p;}/*显示元素*/voidshowlist(list_type*lb) {node_type*p;p=lb->head->next;printf("\nThelinkedlistis\n\n");while(p!=NULL){printf("%d",p->data);p=p->next; } printf("\nThelengthofthislinkedlistis%d\n",lb->length);}/*插入*/voidinse

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论