用c语言写一个线性顺序表,要有建立,输入,取值,查找,插入,删除, 输出, 删除自第i个元素开始的k个元素,就地逆置. 退出
时间: 2023-06-17 15:09:01 浏览: 59
好的,下面是一个简单的线性顺序表的 C 语言实现,包含你所需要的各种操作:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int length;
} SqList;
// 初始化线性表
void InitList(SqList *L) {
L->length = 0;
}
// 判断线性表是否为空
int ListEmpty(SqList L) {
return L.length == 0;
}
// 获取线性表长度
int ListLength(SqList L) {
return L.length;
}
// 获取线性表中第 i 个元素的值
int GetElem(SqList L, int i) {
if (i < 1 || i > L.length) {
printf("Error: GetElem index out of range\n");
exit(1);
}
return L.data[i - 1];
}
// 查找线性表中第一个值为 e 的元素的位置
int LocateElem(SqList L, int e) {
for (int i = 0; i < L.length; i++) {
if (L.data[i] == e) {
return i + 1;
}
}
return 0;
}
// 在线性表中第 i 个位置插入元素 e
void ListInsert(SqList *L, int i, int e) {
if (L->length == MAXSIZE) {
printf("Error: ListInsert failed, the list is full\n");
exit(1);
}
if (i < 1 || i > L->length + 1) {
printf("Error: ListInsert index out of range\n");
exit(1);
}
for (int j = L->length; j >= i; j--) {
L->data[j] = L->data[j - 1];
}
L->data[i - 1] = e;
L->length++;
}
// 删除线性表中第 i 个位置的元素
void ListDelete(SqList *L, int i) {
if (i < 1 || i > L->length) {
printf("Error: ListDelete index out of range\n");
exit(1);
}
for (int j = i; j < L->length; j++) {
L->data[j - 1] = L->data[j];
}
L->length--;
}
// 输出线性表中的所有元素
void ListTraverse(SqList L) {
for (int i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
}
// 删除线性表中自第 i 个元素开始的 k 个元素
void ListDeleteRange(SqList *L, int i, int k) {
if (i < 1 || i > L->length || i + k - 1 > L->length) {
printf("Error: ListDeleteRange index out of range\n");
exit(1);
}
for (int j = i + k - 1; j < L->length; j++) {
L->data[j - k] = L->data[j];
}
L->length -= k;
}
// 就地逆置线性表
void ListReverse(SqList *L) {
for (int i = 0; i < L->length / 2; i++) {
int temp = L->data[i];
L->data[i] = L->data[L->length - i - 1];
L->data[L->length - i - 1] = temp;
}
}
int main() {
SqList L;
int choice, i, e, k;
InitList(&L);
while (1) {
printf("1. Insert element\n");
printf("2. Delete element\n");
printf("3. Get element\n");
printf("4. Locate element\n");
printf("5. Get list length\n");
printf("6. Check if list is empty\n");
printf("7. Traverse list\n");
printf("8. Delete range\n");
printf("9. Reverse list\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the position and element to be inserted: ");
scanf("%d%d", &i, &e);
ListInsert(&L, i, e);
printf("Element has been inserted.\n");
break;
case 2:
printf("Enter the position of the element to be deleted: ");
scanf("%d", &i);
ListDelete(&L, i);
printf("Element has been deleted.\n");
break;
case 3:
printf("Enter the position of the element to be obtained: ");
scanf("%d", &i);
printf("The element is: %d\n", GetElem(L, i));
break;
case 4:
printf("Enter the element to be located: ");
scanf("%d", &e);
i = LocateElem(L, e);
if (i) {
printf("The element is located at position %d\n", i);
} else {
printf("The element is not in the list\n");
}
break;
case 5:
printf("The length of the list is: %d\n", ListLength(L));
break;
case 6:
if (ListEmpty(L)) {
printf("The list is empty.\n");
} else {
printf("The list is not empty.\n");
}
break;
case 7:
ListTraverse(L);
break;
case 8:
printf("Enter the starting position and the number of elements to be deleted: ");
scanf("%d%d", &i, &k);
ListDeleteRange(&L, i, k);
printf("Elements have been deleted.\n");
break;
case 9:
ListReverse(&L);
printf("List has been reversed.\n");
break;
case 0:
exit(0);
default:
printf("Invalid choice.\n");
}
printf("\n");
}
return 0;
}
```
注意事项:
- 在 `ListInsert` 和 `ListDelete` 操作中,我们需要判断插入或删除的位置是否合法,以及线性表是否已满(插入操作)或为空(删除操作)。
- 在 `GetElem` 操作中,我们需要判断获取的位置是否合法。
- 在 `LocateElem` 操作中,如果未找到要查找的元素,返回值为 0。
- 在 `ListDeleteRange` 操作中,我们需要判断删除的范围是否合法。
阅读全文