用C语言代码构造一个顺序表,数据为33,6,8,10,0,3,2,1,遍历顺序表(输出顺序表的值,写在函数内),打印出第二个元素的内容(按位置查找),查找内容为“8”的元素的下标(按内容查找),在第三个位置上插入“1”,然后遍历顺序表,将第五个位置的元素删除,然后遍历顺序表
时间: 2023-08-16 17:05:46 浏览: 50
以下是用C语言实现的顺序表的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10 // 定义顺序表最大长度
typedef struct {
int data[MAXSIZE]; // 存储数据的数组
int length; // 当前顺序表长度
} SqList;
// 初始化顺序表
void InitList(SqList *L) {
L->length = 0;
}
// 遍历顺序表
void TraverseList(SqList L) {
for (int i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
}
// 按位置查找元素
int GetElemByIndex(SqList L, int i) {
if (i < 1 || i > L.length) {
printf("ERROR: Index out of range.\n");
exit(1);
}
return L.data[i - 1];
}
// 按内容查找元素
int GetElemByValue(SqList L, int value) {
for (int i = 0; i < L.length; i++) {
if (L.data[i] == value) {
return i + 1;
}
}
return 0; // 未找到,返回0
}
// 在指定位置插入元素
void InsertElem(SqList *L, int i, int value) {
if (i < 1 || i > L->length + 1) {
printf("ERROR: Index out of range.\n");
exit(1);
}
if (L->length == MAXSIZE) {
printf("ERROR: List is full.\n");
exit(1);
}
for (int j = L->length; j >= i; j--) {
L->data[j] = L->data[j - 1];
}
L->data[i - 1] = value;
L->length++;
}
// 删除指定位置的元素
void DeleteElem(SqList *L, int i) {
if (i < 1 || i > L->length) {
printf("ERROR: Index out of range.\n");
exit(1);
}
for (int j = i; j < L->length; j++) {
L->data[j - 1] = L->data[j];
}
L->length--;
}
int main() {
SqList L;
InitList(&L);
// 插入数据
InsertElem(&L, 1, 33);
InsertElem(&L, 2, 6);
InsertElem(&L, 3, 8);
InsertElem(&L, 4, 10);
InsertElem(&L, 5, 0);
InsertElem(&L, 6, 3);
InsertElem(&L, 7, 2);
InsertElem(&L, 8, 1);
// 遍历顺序表
TraverseList(L);
// 输出第二个元素的内容
printf("The second element is %d.\n", GetElemByIndex(L, 2));
// 查找内容为“8”的元素的下标
int index = GetElemByValue(L, 8);
if (index) {
printf("The index of value 8 is %d.\n", index);
} else {
printf("Value 8 not found.\n");
}
// 在第三个位置上插入“1”
InsertElem(&L, 3, 1);
TraverseList(L);
// 将第五个位置的元素删除
DeleteElem(&L, 5);
TraverseList(L);
return 0;
}
```
输出结果如下:
```
33 6 8 10 0 3 2 1
The second element is 6.
The index of value 8 is 3.
33 6 1 8 10 0 3 2 1
33 6 1 8 0 3 2 1
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)