C语言数据结构基本操作

上传人:hs****ma 文档编号:486394868 上传时间:2023-11-02 格式:DOC 页数:29 大小:87.51KB
返回 下载 相关 举报
C语言数据结构基本操作_第1页
第1页 / 共29页
C语言数据结构基本操作_第2页
第2页 / 共29页
C语言数据结构基本操作_第3页
第3页 / 共29页
C语言数据结构基本操作_第4页
第4页 / 共29页
C语言数据结构基本操作_第5页
第5页 / 共29页
点击查看更多>>
资源描述

《C语言数据结构基本操作》由会员分享,可在线阅读,更多相关《C语言数据结构基本操作(29页珍藏版)》请在金锄头文库上搜索。

1、/*顺序表的基本操作*/#include #include #include #include #define Length 10#define LISTINCREMENT 3#define TRUE 1#define FALSE 0#define OK 1#define ERROR -1#define OVERFLOW -2typedef int Status;typedef int ElemType;typedef struct lineorder int *elem; int length; int listsize; SqList;Status initlist_Sq(SqList &

2、L)/*初始化顺序表*/L.elem=(ElemType*)malloc(sizeof(ElemType)*Length); if(!L.elem) exit(OVERFLOW); L.listsize=Length; L.length=0; return OK;void destroylist(SqList &c) /*销毁顺序表*/ free(c.elem); c.elem=NULL; c.length=0;void clearlist_Sq(SqList &c) /*清空顺序表*/ c.length=0; Status listempty_Sq(SqList c)/*测试顺序表是否为空*

3、/ if(c.length!=0) return(FALSE); return(TRUE);Status ListInsert_Sq(SqList &L, int i,ElemType e) /*在第i个位置上插入一个元素*/ int j,*newbase; if (iL.length+1) return ERROR; if (L.length = L.listsize) newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType); if (!newbase) exit(OVERFLOW); L.

4、elem=newbase; L.listsize+=LISTINCREMENT; for (j=L.length ; j=i ; -j) L.elemj=L.elemj-1; L.elemj=e ; +L.length ; return OK;int LocateElem_Sq(SqList L,ElemType e) /*返回元素e在顺序表中的位置*/ int i=1; ElemType *p=L.elem; while(i=L.length &!(*p=e) i+; p+; if(i=L.length) return i; return FALSE;Status ListDelete_Sq

5、(SqList &L , int i , int &e) /*删除第i个位置上的元素*/ int j; if(iL.length) return ERROR; e=L.elemi-1; for( j=i; jL.length;j+) L.elemj-1=L.elemj; L.length-; return OK;void Print_Sq(SqList L) /*输出顺序表*/ int i; if (listempty_Sq(L) printf(nSequential Lists length is %d. Its empty!,L.length); else printf(nSequenti

6、al Lists length is %d. These element are : ,L.length); for(i=0;iL.length;i+) printf( %d,L.elemi);void menu() printf(n); printf(n* * * * * * * * * * * * * * * * * * * * * * * * * *n); printf( 1 - Print the Sequential List.n); printf( 2 - Insert a data in the Sequential List.n); printf( 3 - Delete a d

7、ata in the Sequential List.n); printf( 4 - Get a elements locationt in the SqList.n); printf( 5 - Clear the Sequential List.n); printf( 6 - Exit.n); printf(* * * * * * * * * * * * * * * * * * * * * * * * * *n); printf(n);void menuselect(SqList L) int k,i,done=1; ElemType e; char ch10; while (done) m

8、enu(); printf(Please choose: ); scanf(%d,&k); getchar(); switch(k) case 1: Print_Sq(L); break;case 2: printf(nInput the location and data you want to Insert: ); scanf(%d,%d,&i,&e); if (ListInsert_Sq(L,i,e)=ERROR) printf(Insert location is not correct!n); break; case 3: printf(nInput the location you

9、 want to delete data: ); scanf(%d,&i);if(ListDelete_Sq(L,i,e)=ERROR) printf(Delete location is not exit!n); else printf(nDelete data is %d.,e); break; case 4: printf(nInput the data you want to find: ); scanf(%d,&e);if(LocateElem_Sq(L,e)!=FALSE) printf(nData %d location in the Sequential List is %d,

10、e,LocateElem_Sq(L,e); else printf(n%d is not in the Sequential List.n, e); break; case 5: clearlist_Sq(L); break;case 6: done=0; void main() ElemType e; SqList La; int n,i; clrscr(); initlist_Sq(La); printf(Input a number of the element in the Sequential List (n=%d):,Length); scanf(%d,&n); printf(En

11、ter these elements:); for(i=1;ilength,Lb_len=Lb.length,j; ElemType e; for(i=1;i=Lb_len;i+) e=Lb.elemi-1; if(!LocateElem_Sq(La,e) ListInsert_Sq(La,+La_len,e); return OK;MergeList(SqList La,SqList Lb,SqList *Lc) int i=1,j=1,k=0,La_len=La.length,Lb_len=Lb.length; ElemType ai,bj; while(i=La_len)&(j=Lb_l

12、en) ai=La.elemi-1; bj=Lb.elemj-1; if(ai=bj) ListInsert_Sq(Lc,+k,ai);i+; else ListInsert_Sq(Lc,+k,bj);j+; while(i=La_len) ai=La.elem-1+i+;ListInsert_Sq(Lc,+k,ai);while(j=Lb_len) bj=Lb.elem-1+j+;ListInsert_Sq(Lc,+k,bj); return OK;void main() ElemType e; SqList La,Lb,Lc; int n,i; clrscr(); initlist_Sq(La); initlist_Sq(Lb); printf(nInput a number of the element in the Sequential List La (n=%d):,Length); scanf(%d,&n); printf(nEnter these elements:); for(i=1;i=n;i+) scanf(%d,&e); ListInsert_Sq(&La,i,e);

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 文学/艺术/历史 > 人文/社科

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