顺序表和数组的操作特别相近,我在学习数据结构的时候采用的是郝斌老师推荐的书籍《数据结构算法实现与分析》,这本书是由西安交大高义凡教授
编写的,是一本特别好的教材,该教材实现了严蔚敏版的《数据结构》中的所有伪代码。我在随笔中用的大部分是高义凡老师的代码,因为我经常忘记一些
知识点,所以想把这些优秀的代码,添加进随笔里面,方便我日后进行学习。现在国家在版权方面控制的特别严,希望高老师看见后,理解一下,我也会注
名这些程序的引用。
顺序表头文件:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #pragma once 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<iostream> 5 #include<string> 6 #include<malloc.h> 7 #include<io.h> 8 #include<math.h> 9 10 #define TRUE 1 11 #define FALSE 0 12 #define OK 1 13 #define ERROR 0 14 15 typedef int ElemType; 16 typedef int Boolean; 17 typedef int Status; 18 19 using namespace std; 20 21 #define LIST_INIT_SIZE 10 // 线性表存储空间的初始分配量 22 #define LIST_INCREMENT 2 // 线性表存储空间的分配增量 23 24 struct SqList 25 { 26 ElemType *elem; //存储空间 27 int length; //元素个数 28 int listsize; //当前分配的容量 29 }; 30 31 32 void InitSeqList(SqList &L); //初始化 33 int ListLength(SqList &L); //线性表长度 34 Status GetElem(SqList &L, int pos, int &elem); //获取指定位置的元素 35 int LocateElem(SqList &L, ElemType e); //找到指定元素的位置 36 Status ListInsert(SqList &L, int pos, ElemType elem); //插入元素 37 void Union(SqList &La, SqList &Lb); //合并 38 void ListTraverse(SqList &L);
顺序表函数模块实现(实现顺序表的增、删、改、查):
但是在该模块中,实现两个顺序表的合并。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include"SeqList.h" 2 3 4 void InitSeqList(SqList &L) { 5 L.elem = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType)); 6 if (L.elem == NULL) { 7 exit(0); 8 } 9 L.length = 0; 10 L.listsize = LIST_INIT_SIZE; 11 } 12 13 int ListLength(SqList &L) { 14 return L.length; 15 } 16 17 Status GetElem(SqList &L, int pos, int &elem) { 18 if (pos<1 || pos>L.length) { 19 return FALSE; 20 } 21 elem = *(L.elem + pos - 1); 22 return TRUE; 23 24 } 25 26 int LocateElem(SqList &L, ElemType e) { 27 int pos = 1; 28 while (pos<=L.length && e!=*(L.elem+pos-1)) 29 { 30 ++pos; 31 } 32 if (pos <= L.length) 33 return pos; 34 else 35 return 0; 36 } 37 38 Status ListInsert(SqList &L, int pos, ElemType elem) { 39 ElemType *newbase=NULL; 40 ElemType *p, *q; 41 if (pos<1 || pos>L.length + 1) 42 return FALSE; 43 if (L.length >= L.listsize) { 44 newbase = (ElemType *)realloc(L.elem, (L.listsize + LIST_INCREMENT) * sizeof(ElemType)); 45 if (newbase) { 46 L.elem = newbase; 47 L.listsize += LIST_INCREMENT; 48 } 49 else 50 { 51 exit(0); 52 } 53 } 54 q = L.elem + pos - 1; //q插入的位置 55 for (p = L.elem + L.length - 1; p >= q; --p) { 56 *(p + 1) = *p; 57 } 58 *q = elem; 59 ++L.length; 60 return OK; 61 62 } 63 64 void Union(SqList &La, SqList &Lb) { 65 ElemType e; 66 int La_len, Lb_len; 67 La_len = ListLength(La); // 求线性表的长度 68 Lb_len = ListLength(Lb); 69 for (int i = 1; i <= Lb_len; i++) { 70 GetElem(Lb, i, e); // 取Lb中第i个数据元素赋给e 71 if (!LocateElem(La, e)) // La中不存在和e相同的元素,则插入之 72 ListInsert(La, ++La_len, e); 73 } 74 } 75 76 void ListTraverse(SqList &L) { 77 ElemType *p = NULL; 78 p = L.elem; 79 for (int i = 1; i <= L.length; i++) { 80 cout << *(p + i - 1) << " "; 81 } 82 cout << endl; 83 }
接下来是主函数的实现:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include"SeqList.h" 2 3 int main(int argc, char *argv[]) { 4 SqList La, Lb; 5 InitSeqList(La); 6 for (int j = 1; j <= 5; j++) { 7 ListInsert(La, j, j); 8 } 9 cout << "LA="; 10 ListTraverse(La); 11 InitSeqList(Lb); 12 for (int i = 0; i <= 5; i++) { 13 ListInsert(Lb, i, 2 * i); 14 } 15 cout << "LB="; 16 ListTraverse(Lb); 17 Union(La, Lb); 18 cout << "LC="; 19 ListTraverse(La); 20 return 0; 21 }
在vs2015上运行如图所示: