文档详情

数据结构代码文档..doc

发布:2017-01-28约1.51万字共20页下载文档
文本预览下载声明
2.1顺序表: #includestdio.h #define MAXSIZE 100 typedef int Elemtype; typedef struct { Elemtype elem[MAXSIZE]; int last; }Seqlist; ///初始化顺序表 void Init_1(Seqlist* L) { int i; for(i = 0;i 10;i ++) L-elem[i] = i + 1; L-last = i; } void Init_2(Seqlist* L) { int i; for(i = 0;i 10;i ++) L-elem[i] = i + 11; L-last = i; } ///位置查找 int locate(Seqlist L,Elemtype e) { int i = 0; while(i = L.last L.elem[i] != e) i ++; if(i = L.last) return i + 1; else return i; } ///插入操作 int Inslist(Seqlist* L,int i,Elemtype e) { int k; if(i 1 || (i L-last+2)) { printf(输入位置不合法\n); return 0; } if(L-last = MAXSIZE-1) { printf(表已满,不能插入\n); return 0; } for(k = L-last;k = i-1;k --) L-elem[k+1] = L-elem[k]; L-elem[i-1] = e; L-last ++; return 1; } ///删除元素 int Dellist(Seqlist* L,int i,Elemtype* e) { int k; if(i 1 || (i L-last+1)) { printf(删除位置不合法\n); return 0; } *e = L-elem[i-1]; for(k = i;k = L-last;k ++) L-elem[k-1] = L-elem[k]; L-last --; return 1; } ///线性表的合并 void mergeList(Seqlist* LA,Seqlist* LB,Seqlist* LC) { int i,j,k,l; i = j = k = 0; while(i = LA-last j = LB-last) { if(LA-elem[i] = LB-elem[j]) LC-elem[k ++] = LA-elem[i ++]; else LC-elem[k ++] = LB-elem[j ++]; } while(i = LA-last) LC-elem[k ++] = LA-elem[i ++]; while(j = LB-last) LC-elem[k ++] = LB-elem[j ++]; LC-last = LA-last + LB-last; } ///打印输出 void print(Seqlist *L) { for(int i = 0;i L-last;i ++) printf(%d ,L-elem[i]); printf(\n\n\n); } int main() { Seqlist L; Init_1(L); // for(int i = 0;i 10;i ++) // printf(%d ,L.elem[i]); Elemtype e; int x; scanf(%d,e); printf(该元素所在的位置 %d\n,locate(L,e)); scanf(%d%d,x,e); Inslist(L,x,e); printf(在%d位置插入元素%d之后的序列:\n,x,e); p
显示全部
相似文档