这个是顺序表的实验代码。
#include <stdio.h>
#define ElementType int
#define MaxSize 100
typedef struct {
ElementType data[MaxSize];
int length;
} list;
list createList(int n) {
list L;
if(n > MaxSize) {
printf("输入的值过多");
return L;
}
L.length = 0;
for(int i = 0; i < n; i++) {
scanf("%d", &L.data[i]);
L.length++;
}
return L;
}
void readList(list L) {
for(int i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
}
int fineNode(list L, int num) {
for(int i = 0; i < L.length; i++) {
if(L.data[i] == num) return i+1;
}
return -1;
}
int insertNode(list *L, ElementType num, int id) {
if(id > L->length + 1) {
printf("超出可用范围");
return 1;
}
for(int i = L->length; i >= 0; i--) {
L->data[i+1] = L->data[i];
if(i == id - 1) {
L->data[i] = num;
L->length++;
break;
}
}
return 0;
}
int removeNode(list *L, int id) {
if(id > L->length) {
printf("超出可用范围!");
return 1;
}
for(int i = id-1; i < L->length; i++) {
L->data[i] = L->data[i+1];
}
L->length--;
return 0;
}
int fromStartRemovekNodes(list *L, int start, int k) {
if(start > L->length || (k-1)+start > L->length) {
printf("超标!\n");
return 1;
}
for(int i = start-1; i < L->length; i++) {
L->data[i] = L->data[i+k];
}
L->length -= k;
return 0;
}
int fromHeadRemovekNodes(list *L, int k) {
if(k > L->length) {
printf("超标!\n");
return 1;
}
for(int i = 0; i < L->length; i++) {
L->data[i] = L->data[i+k];
}
L->length -= k;
return 0;
}
int insertANodeInOrder(list *L, ElementType num) {
int i = L->length-1;
for(; i >= 0; i--) {
if(L->data[i] <= num) break;
L->data[i+1] = L->data[i];
}
L->data[i+1] = num;
L->length++;
return 0;
}
int reverseNodes(list *L) {
ElementType temp;
for(ElementType *p = L->data, *q = L->data+(L->length-1); p < q; p++, q--) {
temp = *p;
*p = *q;
*q = temp;
}
return 0;
}
int merge_List_A_In_Order_And_List_B_In_Order_To_List_C(list a, list b, list *c) {
ElementType *p = a.data, *q = b.data, *k = c->data;
while(p < a.data + a.length && q < b.data + b.length) {
if(*p < *q) {
*(k++) = *p;
p++;
} else {
*(k++) = *q;
q++;
}
}
while(p < a.data+a.length) {
*(k++) = *p;
p++;
}
while(q < b.data+b.length) {
*(k++) = *q;
q++;
}
c->length = a.length+b.length;
return 0;
}
int main() {
int choose;
list L, L2, L3;
int n, num, numid, id, k, start;
while(1) {
printf("----------menu----------\n");
printf(" [1] 创建顺序表\n");
printf(" [2] 读取顺序表\n");
printf(" [3] 查找顺序表\n");
printf(" [4] 插入元素\n");
printf(" [5] 移除元素\n");
printf(" [6] 从i开始删除k个元素\n");
printf(" [7] 从头开始删除k个元素\n");
printf(" [8] 有序表插入\n");
printf(" [9] 元素逆置\n");
printf(" [10] 2个有序表合并\n");
printf("------------------------\n");
scanf("%d", &choose);
switch (choose) {
case 1:
printf("下面是:从键盘输入n个整数,产生顺序表,并输出结点值。\n");
scanf("%d", &n);
L = createList(n);
readList(L);
break;
case 2:
readList(L);
break;
case 3:
printf("下面是:从键盘输入1个整数,在顺序表中查找该结点。若找到,输出结点的位置;若找不到,则显示“找不到”。\n");
scanf("%d", &num);
numid = fineNode(L, num);
if(numid == -1) printf("找不到\n");
else printf("I find %d at %d\n", num, numid);
break;
case 4:
printf("下面是:从键盘输入2个整数,一个表示欲插入的位置i,另一个表示欲插入的数值x,将x插入在对应位置上,输出顺序表所有结点值,观察输出结果。\n");
scanf("%d %d", &id, &num);
insertNode(&L, num, id);
readList(L);
break;
case 5:
printf("下面是:从键盘输入1个整数,表示欲删除结点的位置,输出顺序表所有结点值,观察输出结果。\n");
scanf("%d", &id);
removeNode(&L, id);
readList(L);
break;
case 6:
printf("下面是:从键盘输入2个整数start和k,表示欲删除结点的开始位置和删除元素的个数,并输出结果。\n");
scanf("%d %d", &start, &k);
fromStartRemovekNodes(&L, start, k);
readList(L);
break;
case 7:
printf("下面是:从键盘输入1个整数k,表示从0号元素开始删除元素的个数,并输出结果。\n");
scanf("%d", &k);
fromHeadRemovekNodes(&L, k);
readList(L);
break;
case 8:
printf("下面是:有序表插入,并输出结果。\n");
scanf("%d", &num);
insertANodeInOrder(&L, num);
readList(L);
break;
case 9:
printf("下面是:元素逆置,并输出结果。\n");
reverseNodes(&L);
readList(L);
break;
case 10:
printf("下面是:2个有序表合并,并输出结果。\n");
printf("下面创建一个顺序表L1。\n");
scanf("%d", &n);
L = createList(n);
printf("L1:");
readList(L);
printf("下面是:从键盘输入n个整数,产生顺序表L2,并输出结点值。\n");
scanf("%d", &n);
L2 = createList(n);
printf("L2:");
readList(L2);
merge_List_A_In_Order_And_List_B_In_Order_To_List_C(L, L2, &L3);
printf("L3:");
readList(L3);
break;
default:
goto out;
}
}
out:;
}