数据结构 | 顺序表实现

————————————————————————————————————————————

顺序表

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

概念图

模块设计:伪代码写的非常差劲- -见谅

//初始化顺序表(InitList

使用malloc函数申请一个指定大小100*4字节的地址并将首地址赋值给L的结构体成员elem

if 指针为NULL

函数溢出,退出执行

为结构体成员listsize赋值列表大小100

为结构体成员length赋值元素长度0

返回值(1)

//建立表(Build

定义整型 i 循环使用

定义整型 n 存储元素个数

if 元素个数超过了列表大小

使用realloc函数对L的结构体成员elem指向的地址空间大小调整为 n+10

if 指针为NULL

函数溢出,退出执行

为结构体成员listsize赋值列表大小 n+10

for i<-0 to n

将输入的数据元素存入第 i

为结构体成员length赋值元素长度 n

return OK

//打印表的元素(Print)

定义 int i作为辅助指针时使用

for i++ 0~length

打印顺序表中的元素

打印顺序表中元素的长度

//删除值为X的元素(ListDelete1)

传入顺序表L与需要删除的值X

定义辅助 i

for 遍历顺序表中的元素

if 指针的值等于X

跳出循环

if i等于length(for循环后未找到X)

return ERROR;

i++(执行这一步之前 i 指向X的位置)

for 遍历顺序表

i 位置的值覆盖 i-1 位置的值

长度减一

返回OK

//删除第X位的元素(ListDelete2

传入顺序表与位置X-1

定义辅助 i

if 位置在0~length-1之外

return ERROR

i = x+1(执行这一步之前,x指向需要删除的位置)

for 遍历顺序表

i 的位置覆盖掉 i-1的位置

长度减一

return OK

//逆置函数(Inverse

定义辅助 i temp

for i 遍历函数的 1/2

首尾交换

//冒泡排序(Sort

定义辅助 ijtemp

for i 遍历0~length

for j 遍历 0~length - i

if j个大于第j+1

两数值交换

//插入x,使依然有序(ListInsert)

定义辅助 i,k

if 现在length>=listsize

L->elem重新修改顺序表的长度为原listsize+10

if L->elem等于NULL

exit OVERFLOW

listsize表大小在原来基础上+10

for i 遍历顺序表

if L->elem+i的值>x

跳出循环

k 暂存 i 的值

for ilength向前遍历,直到>k

数据元素向后挪一位

k位的值赋x

长度加一

return OK

//合并两个线性表(Merger)

main函数中调用InitListBuild新建第二张线性表Lb

将两张线性表传入Merger方法中

定义int i,j,k辅助

定义新顺序表Lc

调用InitList方法初始化Lc

if Lc的顺序表大小<A的长度+B的长度

修改Lc的长度为 A.length+B.length+10

if 指针为NULL

exit溢出

列表大小置为A.length+B.length+10

i,j,k置为0

while i小于A.lengthj小于B.length

if 指向Li的值小于指向Lbj的值

指向Lck的值 = 指向Li的值

k指向下一个

i指向下一个

else

指向Lck的值 = 指向Lbj的值

k指向下一个

j指向下一个

while i<length //此时默认j超过了length范围

指向Lck的值 = 指向Li的值

k指向下一个

i指向下一个

while j<length

指向Lck的值 = 指向Lbj的值

k指向下一个

j指向下一个

Lc的长度 = L的长度 + Lb的长度

Lc的值赋给L

return OK

代码实现:

 

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #define TRUE 1
  4 #define FALSE 0
  5 #define OK 1
  6 #define ERROR 0
  7 #define OVERFLOW -2
  8 #define LIST_INIT_SIZE 100
  9 #define LISTINCREMENT 10
 10 typedef int status ;
 11 typedef int ElemType ;
 12 typedef struct//结构体定义SqList:其中含有int *elem指针,int length长度,int listsize列表大小
 13 {
 14     ElemType *elem;
 15     int length, listsize;
 16 } SqList;
 17 status InitList(SqList *L)//初始化L
 18 {
 19     L->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));//L的指针申请长度为100个4字节int空间
 20     if(!L->elem) exit(OVERFLOW);//如果分配不成功则返回溢出
 21     L->listsize = LIST_INIT_SIZE;//定义L->listsize==100
 22     L->length = 0;//L的初始长度为0
 23     return OK;
 24 }
 25 status Build(SqList *L)//建立表,传入的是L的地址
 26 {
 27     int i, n;
 28     printf("Please enter the number of elements n:\n");
 29     scanf("%d", &n); //输入元素个数n
 30     if(n > LIST_INIT_SIZE)
 31     {
 32         //如果元素个数大于n,则使用realloc对malloc申请的内存进行大小的调整,调整为n+10
 33         L->elem = (ElemType *)realloc(L->elem, (n + LISTINCREMENT) * sizeof(ElemType));
 34         if(!L->elem) exit(OVERFLOW);
 35         L->listsize = n + LISTINCREMENT; //列表的大小置为n+10
 36     }
 37     for(i = 0; i < n; i++)
 38         scanf("%d", L->elem + i); //循环输入n个元素
 39     L->length = n; //长度置为n
 40     return OK;
 41 }
 42 void Print(SqList *L) //输出表中元素和长度
 43 {
 44     int i;
 45     for(i = 0; i < L->length; i++)
 46         printf("%d ", *(L->elem + i));
 47     printf("\nlength is:%d\n\n", L->length);
 48 }
 49 void Tips()//提示函数
 50 {
 51     printf("<1> Output sequence table\n");//输出顺序表及顺序表的长度
 52     printf("<2> Delete a node with a value of X \n");//删除值为x的结点
 53     printf("<3> Delete node to locate I \n");//删除给定位置i的结点
 54     printf("<4> Reverse the order table \n");//将顺序表逆置
 55     printf("<5> Sort sequential tables in ascending order \n");//将顺序表按升序排序
 56     printf("<6> Insert x into the appropriate position of the sequence table \n");//将x插入到顺序表的适当位置上
 57     printf("<7> Merge two ordered tables \n");//将两个有序表合并
 58     printf("<0> exit\n\n");//退出
 59 }
 60 status ListDelete1(SqList *L, int x) //删除值为X的元素
 61 {
 62     int i;
 63     for(i = 0; i < L->length; i++)
 64         if(*(L->elem + i) == x)
 65             break;
 66     if(i == L->length)
 67         return ERROR;
 68     for(i++; i < L->length; i++)
 69         *(L->elem + i - 1) = *(L->elem + i);
 70     L->length--;
 71     return OK;
 72 }
 73 status ListDelete2(SqList *L, int x) //删除第X个元素
 74 {
 75     int i;
 76     if(x < 0 || x >= L->length)
 77         return ERROR;
 78     for(i = x + 1; i < L->length; i++)
 79         *(L->elem + i - 1) = *(L->elem + i);
 80     L->length--;
 81     return OK;
 82 }
 83 void Inverse(SqList *L)//逆置函数
 84 {
 85     int i, t;
 86     for(i = 0; i < L->length / 2; i++)
 87     {
 88         t = *(L->elem + i);
 89         *(L->elem + i) = *(L->elem + L->length - i - 1);
 90         *(L->elem + L->length - i - 1) = t;
 91     }
 92 }
 93 void Sort(SqList *L)//冒泡排序(升序)
 94 {
 95     int i, j, t;
 96     for(i = 1; i < L->length; i++)
 97         for(j = 0; j < L->length - i; j++)
 98         {
 99             if(*(L->elem + j) > *(L->elem + j + 1))
100             {
101                 t = *(L->elem + j);
102                 *(L->elem + j) = *(L->elem + j + 1);
103                 *(L->elem + j + 1) = t;
104             }
105         }
106     printf("Arranged in ascending order \n\n");//已按升序排列
107 }
108 status ListInsert(SqList *L, int x) //将X插入,使仍然有序
109 {
110     int i, k;
111     if(L->length >= L->listsize)
112     {
113         L->elem = (ElemType *)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(ElemType));
114         if(!L->elem) exit(OVERFLOW);
115         L->listsize += LISTINCREMENT;
116     }
117     for(i = 0; i < L->length; i++)
118         if(x < * (L->elem + i))
119             break;
120     k = i;
121     for(i = L->length; i > k; i--)
122         *(L->elem + i) = *(L->elem + i - 1);
123     *(L->elem + k) = x;
124     L->length++;
125     return OK;
126 }
127 status Merger(SqList *L, SqList *Lb) //合并两个线性表
128 {
129     int i, j, k;
130     SqList Lc;
131     InitList(&Lc);
132     if(Lc.listsize < L->length + Lb->length)
133     {
134         Lc.elem = (ElemType *)realloc(Lc.elem, (L->length + Lb->length + LISTINCREMENT) * sizeof(ElemType));
135         if(!L->elem) exit(OVERFLOW);
136         Lc.listsize = L->length + Lb->length + LISTINCREMENT;
137     }
138     i = j = k = 0;
139     while(i < L->length && j < Lb->length)
140     {
141         if(*(L->elem + i) < * (Lb->elem + j))
142         {
143             *(Lc.elem + k) = *(L->elem + i);
144             k++;
145             i++;
146         }
147         else
148         {
149             *(Lc.elem + k) = *(Lb->elem + j);
150             k++;
151             j++;
152         }
153     }
154     while(i < L->length)
155     {
156         *(Lc.elem + k) = *(L->elem + i);
157         k++;
158         i++;
159     }
160     while(j < Lb->length)
161     {
162         *(Lc.elem + k) = *(Lb->elem + j);
163         k++;
164         j++;
165     }
166     Lc.length = L->length + Lb->length;
167     *L = Lc;
168     return OK;
169 }
170 int main()
171 {
172     int op, x, flag;
173     SqList L, Lb; //定义SqList类型的两个结构L和Lb
174     InitList(&L);
175     Build(&L);
176     Tips();
177     scanf("%d", &op);
178     while(op)
179     {
180         switch(op)
181         {
182         case 1:
183             Print(&L);
184             break;
185         case 2:
186             printf("Please enter the data you want to delete X :\n");
187             scanf("%d", &x);
188             flag = ListDelete1(&L, x);
189             if(flag)
190                 printf("success!!\n\n");
191             else
192                 printf("Element does not exist, delete failed !!\n\n");
193             break;
194         case 3:
195             printf("Please enter the location you want to delete I :\n");//请输入要删除的位置i
196             scanf("%d", &x);
197             flag = ListDelete2(&L, x - 1); //第i个元素对应的下标为i-1
198             if(flag)
199                 printf("success!!\n\n");
200             else
201                 printf("Element does not exist, delete failed !!\n\n");//元素不存在,删除失败
202             break;
203         case 4:
204             Inverse(&L);
205             break;
206         case 5:
207             Sort(&L);
208             break;
209         case 6:
210             printf("Please enter the data you want to insert X :\n");//请输入要插入的数据X
211             scanf("%d", &x);
212             flag = ListInsert(&L, x);
213             if(flag)
214                 printf("success!!\n\n");//插入成功
215             else
216                 printf("fail!!\n\n");//插入失败
217             break;
218         case 7:
219             printf("Please enter the contents of Lb :\n");//请输入Lb内容
220             InitList(&Lb);
221             Build(&Lb);
222             flag = Merger(&L, &Lb);
223             if(flag)
224                 printf("success!!\n\n");//合并成功
225             break;
226         }
227         Tips();
228         scanf("%d", &op);
229     }
230     return 0;
231 }

 

 

 

posted @ 2017-04-25 15:08  hugh.dong  阅读(768)  评论(0编辑  收藏  举报