线性表的实现代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

/*´´½¨Èçϲ˵¥£¬²¢ÊµÏÖËùÁеŦÄÜ¡£ÏßÐÔ±íÓ¦ÓÃϵͳ*********************************1---------³õʼ»¯**2---------ÅжÏÊÇ·ñΪ¿Õ±í**3---------Çó±í³¤**4---------Êä³ö±í**5---------²åÈëÔªËØ**6---------ɾ³ýÔªËØ**7---------²éÕÒÔªËØ**8---------ÐÞ¸ÄÔªËØ**9---------Çå¿Õ±í**10--------Êͷűí¿Õ¼ä**11--------Í˳ö*********************************ÇëÑ¡Ôñ²Ëµ¥ºÅ£¨1-11£©£º*/#includestdio.h#includestdlib.h#defineMAXSIZE100typedefintDataType;#defineOK1#defineERROR-1#defineFALSE0#defineTrue2typedefstruct{DataTypedata[MAXSIZE];intlength;}SqList;intInitList(SqList*L)//³õʼ»¯{L-length=0;//¿Õ±í£¬³¤¶ÈΪ0returnOK;}intListEmpty(SqList*L)//ÅжÏÊÇ·ñΪ¿Õ±í{if(L-length==0)returnTrue;if(L-length!=0)returnFALSE;}intListLength(SqListL)//Çó±í³¤{return(L.length);}voidListTraverse(SqListL)//Êä³ö±í{inti;for(i=0;iL.length;i++)printf(%d,L.data[i]);printf(\n);}intListInsert(SqList*L,inti,DataTypee)//²åÈëÔªËØ{intk;if(L-length==MAXSIZE)returnERROR;if(i1||iL-length+1)returnERROR;if(i=L-length){for(k=L-length-1;k=i-1;k--)L-data[k+1]=L-data[k];}L-data[i-1]=e;L-length++;returnOK;}intListDelete(SqList*L,inti,DataType*e)//ɾ³ýÔªËØ{intk;if(L-length==0)returnERROR;if(i1||iL-length)returnERROR;*e=L-data[i-1];if(iL-length){for(k=i;kL-length;k++)L-data[k-1]=L-data[k];}L-length--;returnOK;}intListFindEle(SqList*L,DataTypee)//²éÕÒÔªËØ{inti;for(i=1;i=L-length;i++){if(e==L-data[i-1])returni;}returnFALSE;}intListAlter(SqList*L,inti,DataType*e)//ÐÞ¸ÄÔªËØ{if(i1||iL-length)returnERROR;L-data[i-1]=*e;returnOK;}voidClearList(SqList*L)//Çå¿Õ±í{L-length=0;//½«ÏßÐÔ±íµÄ³¤¶ÈÖÃΪ0}//Êͷűí¿Õ¼ävoidDestroyList(SqList*L)//ÊÍ·ÅÏßÐÔ±íÕ¼¾ÝµÄËùÓд洢¿Õ¼ä{if(L-data)free(L-data);}intmain(){inttmp=0,locat=0,res=0,n,i;SqList*list;list=(SqList*)malloc(sizeof(*list));printf(***********************************\n);printf(*1---------³õʼ»¯*\n);printf(*2---------ÅжÏÊÇ·ñΪ¿Õ±í*\n);printf(*3---------Çó±í³¤*\n);printf(*4---------Êä³ö±í*\n);printf(*5---------²åÈëÔªËØ*\n);printf(*6---------ɾ³ýÔªËØ*\n);printf(*7---------²éÕÒÔªËØ*\n);printf(*8---------ÐÞ¸ÄÔªËØ*\n);printf(*9---------Çå¿Õ±í*\n);printf(*10--------Êͷűí¿Õ¼ä*\n);printf(*11--------ÊäÈëÏßÐÔ±í³õʼֵ*\n);printf(*12--------Í˳ö*\n);printf(************************************\n);while(1){printf(ÇëÑ¡Ôñ²Ëµ¥ºÅ£¨1-12£©:);scanf(%d,&n);if(n==0||n11)break;switch(n){case1:InitList(list);printf(ÒÑÍê³É³õʼ»¯!\n);break;case2:if(ListEmpty(list)==True)printf(ÊÇ¿Õ±í!\n);elseprintf(²»ÊÇ¿Õ±í!\n);break;case3:printf(ÏßÐÔ±í³¤¶ÈΪ:%d\n,ListLength(*list));break;case4:printf(ÏßÐÔ±íÔªËصÄֵΪ:);ListTraverse(*list);break;case5:printf(Òª²åÈëµÄλÖúÍÊýֵΪ:);scanf(%d%d,&locat,&res);ListInsert(list,locat,res);printf(²åÈëÍê³É!\n);break;case6:printf(Ҫɾ³ýµÄλÖÃΪ:);scanf(%d,&locat);ListDelete(list,locat,&res);printf(ɾ³ýÍê³É!\n);break;case7:printf(Òª²éÕÒµÄλÖÃΪ:);scanf(%d,&locat);printf(¸ÃλÖõÄֵΪ:%d\n,ListFindEle(list,locat));break;case8:printf(ÒªÐ޸ĵÄλÖúÍÊýֵΪ:);scanf(%d%d,&locat,&res);ListAlter(list,locat,&res);printf(ÐÞ¸ÄÍê³É!\n);break;case9:ClearList(list);printf(±íÖÐÄÚÈÝÒÑÇå¿Õ!\n);break;case10:DestroyList(list);printf(Êͷųɹ¦!\n);break;case11:InitList(list);printf(ÇëÊäÈë˳Ðò±í³¤¶È:);scanf(%d,&list-length);printf(ÇëÊäÈë˳Ðò±íµÄÖµ:);for(i=0;ilist-length;i++)scanf(%d,&list-data[i]);printf(ÏßÐÔÔªËصÄֵΪ:\n);ListTraverse(*list);break;case12:exit(1);default:break;}}return0;}

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功