线性表的顺序结构

数据结构是算法的基础操作

线性表的顺序结构操作有着快熟查找的优势

代码示例:

  1 #ifndef SQLIST_H_INCLUDED
  2 #define SQLIST_H_INCLUDED
  3 
  4 #include<iostream>
  5 #include<cstring>
  6 #include<string.h>
  7 #include<malloc.h>
  8 #include<stdlib.h>
  9 #include<stdio.h>
 10 #define LIST_INIT_SIZE 50
 11 #define LISTINCREMENT 10
 12 using namespace std;
 13 
 14 typedef int SeqType; //存储单元类型
 15 
 16 typedef struct{
 17     SeqType *elem; //存储空间基地址
 18     int length; //当前长度
 19     int listsize; //当前分配的存储容量(以sizeof(ElemType)为单位)
 20 } SqList;
 21 
 22 SqList createList_sq() {
 23     //SqList list;
 24     //return list;
 25 
 26     SqList* list = (SqList*)malloc(sizeof(SqList));
 27     return *list;
 28 }
 29 
 30 int initList_sq(SqList &L) {
 31     L.elem = (SeqType *) malloc(sizeof(SeqType) * LIST_INIT_SIZE);
 32     if (!L.elem)
 33         return 0; //内存分配失败,存储空间不够
 34     L.length = 0; //表示顺序表为空
 35     L.listsize = LIST_INIT_SIZE; //表示顺序表里,最大存储单元个数
 36     return 1;
 37 }
 38 
 39 int insertList_sq(SqList &L, int index, SeqType val) {
 40     if (index > L.length) { //存储的下表超出顺序表实际的长度
 41         printf("插入的下标超出顺序表的实际长度");
 42         return 0;
 43     }
 44     if (index < 0) //下标是负数,插入到结尾
 45         index = L.length;
 46     if (L.length == L.listsize) { //顺序表的存储单元已经存满
 47         printf("顺序表的存储单元已满,继续分配新的存储单元。");
 48         SeqType* newBase = (SeqType*) realloc(L.elem,
 49                 (L.listsize + LISTINCREMENT) * sizeof(SeqType)); //继续分配存储单元
 50         if (!newBase) {
 51             printf("分配内存单元失败");
 52             return 0;
 53         }
 54         L.elem = newBase;
 55         L.listsize += LISTINCREMENT;
 56     }
 57     //寻找合适的插入位置,index后面的元素向后移动
 58     for (int i = L.length; i > index; i--) {
 59         L.elem[i] = L.elem[i - 1]; //向后移动
 60     }
 61     L.elem[index] = val; //插入元素
 62     L.length++;
 63     return 1;
 64 }
 65 
 66 /**
 67  * 插入顺序表(结尾的位置)
 68  * 与上面的函数是重名函数,这叫函数重载,在C++里面支持
 69  */
 70 int insertList_sq(SqList &L, SeqType val) {
 71     return insertList_sq(L, L.length, val);
 72 }
 73 
 74 /**
 75  * 删除指定的元素
 76  * 返回0 找不到指定的元素,删除失败。
 77  * 返回1 找到待删除的元素,删除成功。
 78  */
 79 int removeList_sq(SqList &L, SeqType val) {
 80     int index = -1; //记录匹配到的下标
 81     for (int i = 0; i < L.length; i++) {
 82         if (L.elem[i] == val) {
 83             //找到匹配的val,结束循环
 84             index = i;
 85             break;
 86         }
 87     }
 88     if (index < 0)
 89         return 0;
 90     for (; index < L.length - 1; index++) {
 91         L.elem[index] = L.elem[index + 1];
 92     }
 93     L.length--;
 94     return 1;
 95 }
 96 /**
 97  * 根据下标删除是指定的结点,并返回元素的值
 98  * 返回0 下标超出顺序表长度,删除失败。
 99  * 返回1 下标正确,删除元素,并且将已删除元素值转给elem
100  */
101 int removeList_sq(SqList &L, int index, SeqType &elem) {
102     if (index >= L.length) //下标超出顺序表的长度
103         return 0;
104     index = index < 0 ? L.length : index; //下标负数表示删除最后一个节点
105     elem = L.elem[index];
106     for (int i = index; i < L.length - 1; i++) {
107         L.elem[i] = L.elem[i + 1];
108     }
109     L.length--;
110     return 1;
111 }
112 /**
113  * 销毁顺序表
114  */
115 void destoryList_sq(SqList &L) {
116     free(L.elem); //释放存储空间
117     L.length = 0;
118     L.listsize = 0;
119 //  free(&L);
120 }
121 void DisplayList(SqList &L)
122 {
123     for(int i=0;i<L.length;i++)
124         cout<<L.elem[i]<<"  ";
125     cout<<endl;
126 }
127 int DelElemList(SqList &L,SeqType e) {//删除指定的值:
128 
129     for(int i=0;i<L.length;i++)
130     {
131         if(L.elem[i]==e)
132         {
133             for(int j=i;j<L.length-1;j++)
134             {
135                 L.elem[j]=L.elem[j+1];
136             }
137             L.length--;
138         }
139      }
140     return 1;
141 }
142 int combine(SqList &La,SqList &Lb,SqList &Lc)
143 {
144         int i=0;
145         int j=0;
146         while(i<La.length&&j<Lb.length)
147         {
148             if(La.elem[i]<Lb.elem[j])
149             {
150                 insertList_sq(Lc,La.elem[i]);
151                 i++;
152             }
153             else
154             {
155                 insertList_sq(Lc,Lb.elem[j]);
156                 j++;
157             }
158 
159         }
160         while(i<La.length){
161             insertList_sq(Lc,La.elem[i]);
162             i++;
163         }
164         while(j<Lb.length){
165             insertList_sq(Lc,Lb.elem[j]);
166             j++;
167         }
168 
169     cout<<"合并后Lc元素"<<endl;
170     DisplayList(Lc);
171     return 0;
172 }
173 int combine2(SqList &La,SqList &Lb,SqList &Ld)
174 {
175         int i=0;
176         int j=0;
177         int k=0;
178 //        Ld.elem[0]=0;
179         while(i<La.length&&j<Lb.length)
180         {
181             if(La.elem[i]<=Lb.elem[j])
182             {
183                 if(La.elem[i]>Ld.elem[k-1]||Ld.length==0)
184                 {
185                     insertList_sq(Ld,La.elem[i]);
186                     k++;
187                 }
188             i++;
189             }
190             else
191             {
192                 if(Lb.elem[j]>Ld.elem[k-1]||Ld.length==0)
193                 {
194                     insertList_sq(Ld,Lb.elem[j]);
195                     k++;
196                 }
197                 j++;
198             }
199         }
200         while(i<La.length){
201             if(La.elem[i]>Ld.elem[k-1]||Ld.length==0)
202             {
203                 insertList_sq(Ld,La.elem[i]);
204                 k++;
205             }
206             i++;
207         }
208         while(j<Lb.length){
209             if(Lb.elem[j]>Ld.elem[k-1]||Ld.length==0)
210             {
211                 insertList_sq(Ld,Lb.elem[j]);
212                 k++;
213             }
214             j++;
215         }
216         cout<<"合并(没有重复的)后Ld元素"<<endl;
217         DisplayList(Ld);
218 }
219 
220 #endif // SQLIST_H_INCLUDED

猜你喜欢

转载自www.cnblogs.com/huiyuanai/p/9827230.html
今日推荐