数据结构实验报告-答案2021

上传人:氟**** 文档编号:196096744 上传时间:2021-09-16 格式:DOCX 页数:24 大小:170.04KB
返回 下载 相关 举报
数据结构实验报告-答案2021_第1页
第1页 / 共24页
数据结构实验报告-答案2021_第2页
第2页 / 共24页
数据结构实验报告-答案2021_第3页
第3页 / 共24页
数据结构实验报告-答案2021_第4页
第4页 / 共24页
数据结构实验报告-答案2021_第5页
第5页 / 共24页
点击查看更多>>
资源描述

《数据结构实验报告-答案2021》由会员分享,可在线阅读,更多相关《数据结构实验报告-答案2021(24页珍藏版)》请在金锄头文库上搜索。

1、数据结构(C 语言版 )实验报告专业班级学号姓名实验实验题目实验目得:单链表得插入与删除:了解与掌握线性表得逻辑结构与链式存储结构,掌握单链表得基本算法及相关得时间性能分析;实验要求:建立一个数据域定义为字符串得单链表,在链表中不允许有重复得字符串;根据输入得字符串 ,先找到相应得结点,后删除之;实验主要步骤:1.分析.理解给出得示例程序;2.调试程序 ,并设计输入数据(如:bat, at, eat,f t, at,jat, a,mat, ),测试程序得如下功能 :不允许重复字符串得插入;根据输入得字符串,找到相应得结点并删除;3.修改程序:( 1)增加插入结点得功能;( 2)将建立链表得方法

2、改为头插入法.程序代码:#i cl d td o.h” # cl e tring . #includest l b. #inclu ec e. ” p dstruct de /定义结点.chardat 1 ; /结点得数据域为字符串.s r tno*ne t;/结点得指针域 istNode ;typeef L N d * Link ist;/ 自定义 i k i单链表类型Li List Cr atLi tR1( );/ 函数,用尾插入法建立带头结点得单链表in Li C eat ist( v id) ;/函数,用头插入法建立带头结点得单链表L tNode *Lo a Node() ;/函数 ,

3、按值查找结点void Del te ist( ); oid r ntlis () ;/ 函数,删除指定值得结点/ 函数,打印链表中得所有值void DeleteA l() ;/函数,删除所有结点,释放内存ListNode *ddNo e();. /修改程序 :增加节点 .用头插法,返回头指针/= = = 主函数 = = void main ( ).char ch10,num5 ;LinkLi t ;h ad=Cr tL t(); /用头插入法建立单链表,返回头指针 intlist(head) ;/遍历链表输出其值pr n f( ” e ete e ( y/n): ” );/ 输入” y”或”

4、n”去选择就为否删除结点.s an ( s ,n );.if( tr mp(nu ,y ” )=0 |s mp( um, Y)=0) .pr n f( ” Please nput De et _data: )”;. an ( ” s” ,ch); / 输入要删除得字符串.DeleteL s(hea, h);.printli t( );. i tf( Ad node . (y/):” );/输入” y 或” 去选择就为否增加结点scanf( ” ,n m) ;if (strcmp ( nu ,y)=0strcmp(nu , Y ” )=0).h a = dNode( d);.printli (

5、ead);DeleteA l( he );/ 删除所有结点,释放内存/ = 用尾插入法建立带头结点得单链表= = LinkList CreatLi tR1(void )c arch10; inkLi t h d=( inkList ) ma loc ( s zeo( istNode) ) ; / 生成头结点Lis Node * , r, p;r=he d;r-next=NULL;p intf( Inputtoe d”) ;/ 输入”代表输入结束 p i tf( Pl se input Node_data: );scan (%s,ch) ;/ 输入各结点得字符串 hi e( s rcmp(ch,

6、 # ) .= ).pp=L ca N de( head,ch);/按值查找结点,返回结点指针.i( pp= ) / 没有重复得字符串,插入到链表中. =( ListN e *)mal oc(siz (L stN de));.st cp( s-data,ch) ;.r n xt=;. = ;. xt=NULL ;.printf ( ” In ut # t e) ;.p nt( Pleae inpuN da a: ”);. a ( ” %,s ch) ;return e d;/ 返回头指针/ = = 用头插入法建立带头结点得单链表= = = LinkL st eat ist( oi ).ch r

7、 ch100;. inkLi t head,p;. ad=(i st)maloc( izeo( Lis Node);.hea ext N L ;.w ile(1).printf(”puIt to end);.pri f( ”leaeinput N d a:) ;. can( s ”, ch) ;.f( t mp(ch ,” #)).i (Loc teNode( head,) = N L).s.trcpy( e ddata,ch);.p=( in List)m lloc( i eof( Lis N d );.p. x =hea ;.head=p;.else. reak; e rh d;/ = =

8、 = 按值查找结点,找到就返回该结点得位置,否就返回N L = = =istNode LocateNode(Lin Li t ead, char *key ) i tN *p=head- ne t; /从开始结点比较while ( p!=NU L &st cmp(p-d t ,key).=0 )/ 直到 p 为 LL 或 p-dta 为 e止.=p- n xt; /扫描下一个结点r urn;/若 pN LL 就查找失败,否就p 指向找到得值为ey 得结点/ = =修改程序:增加节点= = i N Ad Node (Li kLi t ea )char ch 1;Li tNode*s , pp;p

9、ri tf( Ple se i put a New Nod _data:);sc ( %s , ch);/输入各结点得字符串.p =L c te od( head,c );/ 按值查找结点,返回结点指针pri t(n” );.if(pp= ULL)/没有重复得字符串,插入到链表中.s( ist o e *) m oc(s o (L t ode); s cp( s a a, c) ;p intf(” ok3 ); next=h a et;.he d-next= ;. turn head;/ = = = 删除带头结点得单链表中得指定结点= = vo d De ete ist( nkL h ad,

10、har ey) stNode* , ,*q= ead;p=Lo ateNode(head, e) ; /按 key 值查找结点得if(p=NUL ) /若没有找到结点,退出.printf(”potion erro ”;).exit( );whi e(q next.=p)/p 为要删除得结点,为p 得前结点.q=q nex; =q- nex ; ext nex;fr( r) ;/释放结点/ = 打印链表 =void p n li t(LinkListead)ListNo e p=head t;/ 从开始结点打印w l (p)printf(”%,”, p ta); p=p-n x;p n ( n”

11、;)/ = = =删除所有结点,释放空间= = = v iDeleteAll(L nk ist head)ListN d *p= ead,*;whi e( ext) r=p ne ;.fre (p);p r;.free(p);实验结果 :Input #o nd Pleas nput edata:bat ut #oendl as inpu Node_d ta :cat n u # t e dPleaseinputode_data:eatIn t# tond Plea e inpu Nod _dt :fa npt #o endPe e putNd_data : at Inp t to e P ease npu Nde data:jatIn ut to en leas input o e_da: la Inp t #o nPl ase i pu Noe_data:mat In u to end Plese i put N d dta :mat,lt,jat,ht ,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 初中教育

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号