线性表顺序存储的各项操作的实现
2015-07-30 10:12
429 查看
该文中主要采用顺序存储来完成对线性表的各项操作,通过动态分配的一维数组来实现,主要包括初始化、插入、删除、取第i个数据元素、定位、销毁、合并两个有序表以及在有序表中添加元素依旧保持其有序。
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #define OK 1 #define ERROR 0 #define OVERFLOW -1 #define LIST_INIT_SIZE 100 #define lISTINCREMENT 10
typedef int ElemType; typedef int Status; typedef struct SqList { ElemType *elem;//存储空间基址 int length;//当前长度 int listsize;//当前的存储容量 }SqList; Status InitList(SqList *L)//初始化 { L->elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if(!L->elem) exit(OVERFLOW);//存储分配失败 L->length = 0; L->listsize = LIST_INIT_SIZE; return OK; } Status DestroyList(SqList *L)//销毁表 { if(L->elem!=NULL) { free(L->elem); L->elem=NULL; L->length=0; L->listsize=0; } printf("Destroy List\n"); return OK; } Status ListInsert(SqList *L,int i, ElemType e)//插入 { if(i<1||i>L->length+1) return ERROR; if(L->length >= L->listsize) { ElemType *newbase = (ElemType *)realloc(L->elem,(L->listsize+lISTINCREMENT)*sizeof(ElemType)); if(!newbase) exit(OVERFLOW); L->elem=newbase; L->listsize+=lISTINCREMENT; } ElemType *q =&(L->elem[i-1]); ElemType *p; for (p=&(L->elem[L->length-1]);p>=q;--p) { *(p+1)=*p; } *q=e; ++L->length; return OK; } void IcreaseInsert(SqList *L, ElemType e)//L有序递增,将e插入依旧保持递增 { int i; for(i=0;i<=L->length;i++) { if(e<=L->elem[0]) { ListInsert(L,1,e); break; } else if(e>=L->elem[i]&&e<=L->elem[i+1]) { ListInsert(L,i+2,e); break; } } } Status ListDelete(SqList *L,int i, ElemType e)//删除 { if(i<1||i>L->length+1) return ERROR; ElemType *p = &(L->elem[i-1]); e=*p; ElemType *q =L->elem+L->length-1; for(++p;p<=q;++p) *(p-1)=*p; --L->length; return OK; } int GetElement(SqList *L,int i)//获取第i个位置上的元素 { if(i<1||i>L->length+1) return ERROR; return L->elem[i-1]; } int LocateElem(SqList *L, ElemType e)//定位元素e { int i=0; while(i<=L->length&&L->elem[i]!=e) ++i; if (i<=L->length) return i+1; else return -1; } void UnionList(SqList *La,SqList *Lb, SqList *L)//合并两个有序表 { int i=0,j=0; int k; for(k=0;k<(La->length+Lb->length);k++) { if(La->elem[i]<=Lb->elem[j]) { L->elem[k]=La->elem[i]; i++; } else if(La->elem[i]>Lb->elem[j]) { L->elem[k]=Lb->elem[j]; j++; } } L->length=k; } void PrintList(SqList *L)//打印 { int i; for(i = 0; i<L->length;i++) { printf("%4d",L->elem[i]); } printf("\n"); } int main() { SqList *L; SqList *P,*Q; ElemType e; int k,i; int quit=1; L=(SqList *)malloc(sizeof(SqList)); InitList(L); P=(SqList *)malloc(sizeof(SqList)); InitList(P); Q=(SqList *)malloc(sizeof(SqList)); InitList(Q); for(i=1;i<=5;i++) ListInsert(L,i,i*2); for(i=1;i<=5;i++) ListInsert(P,i,i*3); PrintList(L); while(quit==1) { printf("1.Insert 2.Delete 3.getElement 4.Locate 5.IncreaseInsert 6.Union 7.Destroy\n"); scanf("%d",&k); getchar(); switch(k) { case 1: PrintList(L); printf("insert location and value\n"); scanf("%d %d",&i,&e); ListInsert(L,i,e); PrintList(L); break; case 2: PrintList(L); printf("insert location you want delete\n"); scanf("%d",&i); ListDelete(L,i,e); PrintList(L); break; case 3: PrintList(L); printf("insert location you want get\n"); scanf("%d",&i); printf("%d\n",GetElement(L,i));InitList(P); break; case 4: PrintList(L); printf("insert value you want locate\n"); scanf("%d",&e); printf("%d\n",LocateElem(L,e)); break; case 5: PrintList(L); printf("insert value you want increase insert\n"); scanf("%d",&e); IcreaseInsert(L,e); PrintList(L); break; case 6: PrintList(L); PrintList(P); UnionList(L,P,Q); PrintList(Q); break; case 7: DestroyList(L); break; } } return 0; }
相关文章推荐
- 用VBScript写合并文本文件的脚本
- oracle列合并的实现方法
- 使用BAT一句话命令实现快速合并JS、CSS
- SQL 合并多行记录的方法总汇
- C#自适应合并文件的方法
- GridView单元格合并
- 如何合并多个 .NET 程序集
- 使用UglifyJS合并/压缩JavaScript的方法
- 高性能WEB开发 JS、CSS的合并、压缩、缓存管理
- 多个js与css文件的合并方法详细说明
- 使用不同的方法结合/合并两个JS数组
- java线性表排序示例分享
- 合并Excel工作薄中成绩表的VBA代码,非常适合教育一线的朋友
- 用js实现table单元格高宽调整,兼容合并单元格(兼容IE6、7、8、FF)实例
- oracle实现多行合并的方法
- php分割合并两个字符串的函数实例
- PHP合并静态文件详解
- php线性表的入栈与出栈实例分析
- Windows下使用apache模块实现合并多个js、css提高网页加载速度
- C++语言实现线性表之数组实例