SCAU 8610 顺序查找

本文详细介绍了如何使用顺序查找算法在一个无序表中搜索特定元素。通过具体实例,展示了算法的实现过程,包括创建静态查找表、动态内存分配、输入数据、查找元素并输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

8610 顺序查找

时间限制:1000MS 代码长度限制:10KB
提交次数:2303 通过次数:1423

题型: 编程题 语言: G++;GCC
Description 编写Search_Seq函数,实现在一个无序表ST中采用顺序查找算法查找值为key的元素的算法.
#include"malloc.h" /* malloc()等 */
#include"stdio.h"
#include"stdlib.h"

typedef int ElemType;
typedef struct /*静态查找表的顺序存储结构 */
{
ElemType elem; / 数据元素存储空间基址,建表时按实际长度分配,0号单元留空 /
int length; /
表长度 */
}SSTable;

void Creat_Seq(SSTable &ST,int n)
{ /* 操作结果: 构造一个含n个数据元素的静态顺序查找表ST(数据来自数组r) */
int i,temp;
ST.elem=(ElemType )malloc((n+1) * sizeof(ElemType)); / 动态生成n个数据元素空间(0号单元不用) */
if(!(ST).elem)
{
printf(“ERROR\n”);
exit(0);
} /内存分配失败结束程序/
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
(ST.elem+i)=temp; / 依次赋值给ST */
}
ST.length=n;
}

int Search_Seq(SSTable &ST,ElemType key)
{ /* 在顺序表ST中顺序查找其关键字等于key的数据元素。若找到,则函数值为 /
/
该元素在表中的位置,否则为0。算法9.1 */

}

main()
{
SSTable ST;
int loc,key;
int n;
scanf("%d",&n);
Creat_Seq(ST,n);
//printf(“Please input the key value:”);
scanf("%d",&key);
loc = Search_Seq(ST,key);
if(loc!=0)
printf(“The element position is %d.\n”,loc);
else
printf(“The element is not exist.\n”);
}

输入格式
第一行:元素个数n
第二行:依次输入n个元素的值
第三行:输入要查找的关键字key的值

输出格式
输出分两种情形:
1.如果key值存在,则输出其在表中的位置x(表位置从1开始),格式为The element position is x.
2.如果key值不存在输出:“The element is not exist.”

输入样例
6
1 3 5 7 9 10
5

输出样例
The element position is 3.

#include"cstdlib" /* malloc()等 */
#include"cstdio"

typedef int ElemType;
typedef struct /*静态查找表的顺序存储结构 */
{
    ElemType *elem; /* 数据元素存储空间基址,建表时按实际长度分配,0号单元留空 */
    int length; /* 表长度 */
}SSTable;

void Creat_Seq(SSTable &ST,int n)
{ /* 操作结果: 构造一个含n个数据元素的静态顺序查找表ST(数据来自数组r) */
    int i,temp;
    ST.elem=(ElemType *)malloc((n+1) * sizeof(ElemType)); /* 动态生成n个数据元素空间(0号单元不用) */
    if(!(ST).elem)
    {
        printf("ERROR\n");
        exit(0);
    } /*内存分配失败结束程序*/
    for(i=1;i<=n;i++)
    {
        scanf("%d",&temp);
        *(ST.elem+i)=temp; /* 依次赋值给ST */
    }
    ST.length=n;
}

int Search_Seq(SSTable &ST,ElemType key)
{ /* 在顺序表ST中顺序查找其关键字等于key的数据元素。若找到,则函数值为 */
/* 该元素在表中的位置,否则为0。算法9.1 */
int n;
n=ST.length;
for(int i=1;i<=n;i++)
{
    if(ST.elem[i]==key)
        return i;
}
return 0;
}

int main()
{
    SSTable ST;
    int loc,key;
    int n;
    scanf("%d",&n);
    Creat_Seq(ST,n);
    //printf("Please input the key value:");
    scanf("%d",&key);
    loc = Search_Seq(ST,key);
    if(loc!=0)
        printf("The element position is %d.\n",loc);
    else
        printf("The element is not exist.\n");
}
### 链的基本概念 链是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链中的指针链接次序实现的[^4]。每个节点由两部分组成:一个是存储数据元素的数据域,另一个是存储下一个节点地址的指针域[^4]。链的特点在于其动态生成性,能够在运行时根据需要创建节点。 ### 链的实现方法 #### 定义链节点 定义链节点时,通常使用结构体来示。例如,在C语言中可以这样定义单链节点: ```c typedef struct node { int data; // 数据域 struct node* next; // 指针域,指向下一个节点 } ListNode; ``` 这里定义了一个`ListNode`结构体,包含一个整型数据域`data`和一个指向下一节点的指针`next`[^3]。 #### 创建链 创建链可以通过头插法或尾插法实现。以下是一个简单的尾插法实现示例: ```c ListNode* createList(int arr[], int n) { if (n == 0) return NULL; ListNode* head = (ListNode*)malloc(sizeof(ListNode)); head->data = arr[0]; head->next = NULL; ListNode* tail = head; for (int i = 1; i < n; i++) { ListNode* newNode = (ListNode*)malloc(sizeof(ListNode)); newNode->data = arr[i]; newNode->next = NULL; tail->next = newNode; tail = newNode; } return head; } ``` 此代码通过循环逐步将数组中的元素插入到链中,并始终保持`tail`指向当前链的最后一个节点[^4]。 #### 基本操作 链的基本操作包括插入、删除、查找等。以下是几个常见的操作函数: 1. **插入节点** 插入节点可以分为头插法、尾插法以及在指定位置插入。 ```c void insertAtEnd(ListNode** head, int value) { ListNode* newNode = (ListNode*)malloc(sizeof(ListNode)); newNode->data = value; newNode->next = NULL; if (*head == NULL) { *head = newNode; return; } ListNode* temp = *head; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; } ``` 2. **删除节点** 删除链中第一次出现的指定的节点。 ```c void deleteNode(ListNode** head, int value) { if (*head == NULL) return; ListNode* temp = *head; if (temp->data == value) { *head = temp->next; free(temp); return; } ListNode* prev = NULL; while (temp != NULL && temp->data != value) { prev = temp; temp = temp->next; } if (temp == NULL) return; prev->next = temp->next; free(temp); } ``` 3. **查找节点** 查找中是否存在某个。 ```c int searchNode(ListNode* head, int value) { ListNode* temp = head; while (temp != NULL) { if (temp->data == value) return 1; temp = temp->next; } return 0; } ``` 以上代码展示了链的一些基本操作,具体实现可根据需求调整[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值