顺序表的子函数,查找元素
时间: 2023-05-20 12:02:57 浏览: 64
顺序表的子函数,查找元素可以通过遍历整个顺序表来实现。具体实现方式可以使用循环语句,逐个比较每个元素,直到找到目标元素或者遍历完整个顺序表。如果找到目标元素,可以返回该元素在顺序表中的位置,如果遍历完整个顺序表都没有找到目标元素,则返回-1表示未找到。以下是一个示例代码:
int search(int *arr, int n, int target) {
for (int i = 0; i < n; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
相关问题
(1) 编写程序建立一个顺序表,并逐个输出顺序表中所有数据元素的值。编写主函数测试结果。 (2) 编写顺序表定位操作子函数,在顺序表中查找是否存在数据元素x。如果存在,返回顺序表中和x值相等的第1个数据元素的序号(序号从0开始编号);如果不存在,返回-1。编写主函数测试结果。
对于问题(1)和问题(2),我可以回答。以下是我的回答:
(1)建立顺序表程序的代码:
```
#include <stdio.h>
#define MAXSIZE 100 // 最大容量
typedef struct {
int data[MAXSIZE]; // 存放元素的数组
int length; // 当前表长
} SeqList;
void InitList(SeqList *L) {
L->length = 0;
}
void Insert(SeqList *L, int x) {
if (L->length == MAXSIZE) {
printf("Error: SeqList is full\n");
return;
}
L->data[L->length++] = x;
}
void PrintList(SeqList *L) {
for (int i = 0; i < L->length; i++) {
printf("%d ", L->data[i]);
}
printf("\n");
}
int main() {
SeqList L;
InitList(&L);
Insert(&L, 1);
Insert(&L, 2);
Insert(&L, 3);
PrintList(&L);
return 0;
}
```
以上是建立顺序表程序的代码,使用 `SeqList` 结构体存放元素的数组,使用 `InitList` 初始化操作,使用 `Insert` 插入操作,使用 `PrintList` 输出操作。
主函数测试结果:
```
1 2 3
```
(2)顺序表定位操作子函数的代码:
```
#define NotFound -1 // 元素不存在返回值
int Locate(SeqList *L, int x) {
for (int i = 0; i < L->length; i++) {
if (L->data[i] == x) {
return i; // 找到了,返回下标
}
}
return NotFound; // 没找到,返回-1
}
```
以上是顺序表定位操作子函数的代码,使用 `Locate` 函数查找顺序表中是否存在指定元素 `x`,如果找到了,返回下标,如果没有找到,返回 `-1`。
主函数测试结果:
```
int main() {
SeqList L;
InitList(&L);
Insert(&L, 1);
Insert(&L, 2);
Insert(&L, 3);
int pos = Locate(&L, 2);
if (pos != NotFound) {
printf("Position of 2: %d\n", pos);
} else {
printf("2 not found.\n");
}
pos = Locate(&L, 4);
if (pos != NotFound) {
printf("Position of 4: %d\n", pos);
} else {
printf("4 not found.\n");
}
return 0;
}
```
输出结果为:
```
Position of 2: 1
4 not found.
```
顺序表的简单插入排序和二分法查找c语言
顺序表的简单插入排序代码如下:
```c
#include <stdio.h>
#define MAXSIZE 10
typedef struct {
int r[MAXSIZE+1]; // 存储排序关键字,r[0]不用
int length; // 顺序表长度
} SqList;
void InsertSort(SqList *L) {
int i, j;
for (i = 2; i <= L->length; ++i) {
if (L->r[i] < L->r[i-1]) { // 需将L->r[i]插入有序子表
L->r[0] = L->r[i]; // 设置哨兵
for (j = i-1; L->r[j] > L->r[0]; --j) {
L->r[j+1] = L->r[j]; // 记录后移
}
L->r[j+1] = L->r[0]; // 插入到正确位置
}
}
}
int BinarySearch(SqList L, int key) {
int low = 1, high = L.length, mid;
while (low <= high) {
mid = (low + high) / 2;
if (L.r[mid] == key) {
return mid;
} else if (L.r[mid] > key) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return 0;
}
int main() {
SqList L = {{0, 49, 38, 65, 97, 76, 13, 27, 49}, 8};
InsertSort(&L);
int key = 49;
int pos = BinarySearch(L, key);
if (pos) {
printf("%d is found at position %d\n", key, pos);
} else {
printf("%d is not found\n", key);
}
return 0;
}
```
这里定义了一个 `SqList` 结构体,包含一个数组 `r` 和一个整型变量 `length`,数组 `r` 存储了排序关键字。`InsertSort` 函数实现了简单插入排序算法,`BinarySearch` 函数实现了二分查找算法,其中 `low` 和 `high` 分别表示查找范围的起始位置和结束位置,`mid` 表示中间位置,如果找到了关键字 `key`,则返回其位置,否则返回 0。
本例中,先将一个顺序表排序,然后查找关键字为 49 的元素。程序输出:
```
49 is found at position 2
```
以上代码仅供参考,实际使用中需要根据具体情况进行适当修改。