# 数据结构-顺序表的基本操作

• 创建和输出顺序表
• 顺序表插入
• 顺序表查询
• 顺序表删除
• 顺序表排序
• 顺序表有序插入
• 删除X到Y之间的元素

``````#include "stdio.h"

#define listSize 20 //默认表容量20
typedef int List;

typedef struct {
List data[listSize];    //数组data用于存放表节点
int length;    //表节点的长度（节点数）
} SeqList;

//输出顺序表 OK
void printList(SeqList *list, int n) {
int i;
printf("The list is: ");
for (i = 0; i < n; i++) {
printf("%4d", list->data[i]);
}
}

//新建顺序表和显示 OK
void createAndShowList(SeqList *list, int n) {
int i;
list->length = n;
for (i = 0; i < n; i++) {
scanf("%d", &list->data[i]);
}
printList(list, n);
}

//查找元素 OK
int selectElem(SeqList *list, List selectNum) {
int i;
for (i = 0; i < (*list).length - 1; i++) {
if ((*list).data[i] == selectNum) {
return i + 1;
}
}
return -1;
}

//插入元素 OK
void insertElem(SeqList *list, List num, int position) {
int flag,i;
if ((*list).length > listSize) {
printf("溢出\n");
} else if ((position < 1) || (position > (*list).length + 1)) {
printf("位置有误\n");
} else {
flag = (*list).data[position - 1];
for (i = (*list).length - 1; i >= position - 1; i--) {
(*list).data[i + 1] = (*list).data[i];
}
list->data[position] = flag;
list->data[position - 1] = num;
(*list).length += 1;

printList(list, list->length);
}
}

//删除元素 OK
void deleteElem(SeqList *list, int position) {
int i;
if (position > listSize) {
printf("Out of Bounds");
} else if (position < 1 || position > (*list).length) {
printf("The position is null");
} else {
for (i = position - 1; i < (*list).length; i++) {
(*list).data[i] = (*list).data[i + 1];
}
(*list).length--;
printList(list, list->length);
}
}

//简单排序 OK
void sortList(SeqList *list) {
int temp;
int compare = 0, move = 0;
int i,j;
for (i = 0; i < list->length - 1; i++) {
for (j = i + 1; j < list->length; j++) {
compare++;
if (list->data[i] > list->data[j]) {
temp = list->data[i];
list->data[i] = list->data[j];
list->data[j] = temp;
move++;
}
}
}
printf("Compared Count:%d,Move Count:%d.\n", compare, move);
printList(list, list->length);
}

//快速排序 OK
void quickSort(SeqList *list, int l, int r) {
if (l < r) {
int i = l, j = r, x = list->data[l];
while (i < j) {
while (i < j && list->data[j] >= x) // 从右向左找第一个小于x的数
j--;
if (i < j)
list->data[i++] = list->data[j];
while (i < j && list->data[i] < x) // 从左向右找第一个大于等于x的数
i++;
if (i < j)
list->data[j--] = list->data[i];
}
list->data[i] = x;
quickSort(list, l, i - 1); // 递归调用
quickSort(list, i + 1, r);
}
}

//按顺序插入元素 OK
void insertElementAsSort(SeqList *list, List num) {
int maxIndex = (*list).length - 1;
int max = (*list).data[maxIndex];
int min = (*list).data[0];
int i,j,flag;
if (num < min) {
for (i = (*list).length - 1; i >= 0; i--) {
(*list).data[i + 1] = (*list).data[i];
}
(*list).data[0] = num;

} else if (num > max) {
(*list).data[maxIndex + 1] = num;
} else {
for (i = 1; i < (*list).length - 2; i++) {
if (num < (*list).data[i]) {
flag = (*list).data[i];
for (j = (*list).length - 1; j > i; j--) {
(*list).data[j + 1] = (*list).data[j];
}
(*list).data[i] = num;
(*list).data[i + 1] = flag;
break;
}
}
}
(*list).length += 1;
printList(list, list->length);
}

//删除X-Y之间的元素 OK
void deleteXtoY(SeqList *list, List start, List end) {
int maxIndex = (*list).length - 1;
int max = (*list).data[maxIndex];
int min = (*list).data[0];
int i,j = 0;
if ((start < min) && (end > max)) {
list->length = 0;
} else {
for (i = 0; i < list->length; i++) {
if (!(list->data[i] > start && list->data[i] < end))
list->data[j++] = list->data[i];
}
list->length = j;
}
printList(list, list->length);
}

void main() {

/*创建初始化顺序表，并打印输出*/
SeqList list;   //顺序表
int n;  //顺序表长度
List insertNum;
int position;
List selectNum;
int index;
int positionDel;
int s;
int insertElement;
int i;
List startDel, endDel;

printf("Please input the length of list: ");
scanf("%d", &n);
createAndShowList(&list, n);

/*在顺序表指定位置插入元素*/

printf("\nPlease input the position and element that you want to insert: ");
scanf("%d %d", &position, &insertNum);
insertElem(&list, insertNum, position);

/*在顺序表中查找给定值的位置，并输出比较次数*/

printf("\nPlease input the element that you want to select: ");
scanf("%d", &selectNum);
index = selectElem(&list, selectNum);
printf("The position is %d, The count is %d.", index, index);

/*删除顺序表中指定位置的元素*/

printf("\nPlease input the position which you want to delete: ");
scanf("%d", &positionDel);
deleteElem(&list, positionDel);

/*顺序表排序*/

printf("\nPlease choice 1.(SimpleSort) or 2.(QuickSort) ?: ");
scanf("%d", &s);
switch (s) {
case 1:
printf("\nSimpleSort list: ");
sortList(&list);
break;
case 2:
quickSort(&list, 0, list.length - 1);
printf("\nQuickSort list: ");
for (i = 0; i < n; i++) {
printf("%4d", list.data[i]);
}
break;
default:
printf("Input ERROR");
break;
}

/*顺序表按顺序插入元素*/

printf("\nPlease input the element what you want to insert: ");
scanf("%d", &insertElement);
insertElementAsSort(&list, insertElement);

/*快速删除x-y之间的元素*/

printf("\nPlease input the delete position of start and end: ");
scanf("%d %d", &startDel, &endDel);
deleteXtoY(&list, startDel, endDel);
printf("\n");
}``````