数据结构第七章-顺序查找(顺序存储结构+链式存储结构)

1.顺序存储结构

cs 复制代码
#include <stdio.h>  
#include <stdlib.h>  
  
#define MAXSIZE 100  
#define OK 1  
  
typedef struct {  
    int key; // 关键字域  
} ElemType;  
  
typedef struct {  
    ElemType *R;  
    int length;  
} SSTable;  
  
int InitList_SSTable(SSTable *L) {  
    L->R = (ElemType *)malloc(MAXSIZE * sizeof(ElemType));  
    if (!L->R) {  
        printf("初始化错误\n");  
        return 0;  
    }  
    L->length = 0;  
    return OK;  
}  
  
int Insert_SSTable(SSTable *L) {  
    int j = 0;  
    for (int i = 0; i < MAXSIZE; i++) {  
        L->R[i].key = j;  
        L->length++;  
        j++;  
    }  
    return 1;  
}  
  
int Search_Seq(SSTable ST, int key) {  
    // 在顺序表ST中顺序查找其关键字等于key的数据元素。若找到,则函数值为  
    // 该元素在表中的位置,否则为0  
    for (int i = ST.length - 1; i >= 0; --i) {  
        if (ST.R[i].key == key) return i + 1; // C语言中数组从0开始,但通常位置从1开始计数,这里返回i+1  
    }  
    return 0;  
}  
  
void Show_End(int result, int testkey) {  
    if (result == 0)  
        printf("未找到%d\n", testkey);  
    else  
        printf("找到%d位置为%d\n", testkey, result);  
}  
  
int main() {  
    SSTable ST;  
    InitList_SSTable(&ST);  
    Insert_SSTable(&ST);  
    int testkey1 = 7, testkey2 = 200;  
    int result;  
    result = Search_Seq(ST, testkey1);  
    Show_End(result, testkey1);  
    result = Search_Seq(ST, testkey2);  
    Show_End(result, testkey2);  
      
    // 释放动态分配的内存  
    free(ST.R);  
      
    return 0;  
}

2.链式存储结构

cs 复制代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Node
{
	int data;
	struct Node* next;
}Node;
Node* Create(int data)
{
	Node* newnode = (Node*)malloc(sizeof(Node));
	if (newnode == NULL)
	{
		return;
	}
	newnode->data = data;
	newnode->next = NULL;
	return newnode;
}
//尾插
void appendNode(Node** headRef, int data)
{
	Node* newnode = Create(data);
	if (*headRef == NULL)
	{
		*headRef = newnode;
		return;
	}
	Node* temp = *headRef;
	while (temp->next != NULL)
	{
		temp = temp->next;
	}
	temp->next = newnode;
}
//顺序查找
Node* Find_Node(Node* head, int key)
{
	Node* current = head;
	while (current)
	{
		if (current->data == key)
		{
			return current;
		}
		current = current->next;
	}
	return NULL;
}
void printNode(Node* head)
{
	Node* temp = head;
	while (temp)
	{
		printf("%d ", temp->data);
		temp = temp->next;
	}
}
void Free_Node(Node* head)
{
	Node* temp = head;
	while (temp)
	{
		Node* pcur = temp->next;
		free(temp);
		temp = pcur;
	}
}
int main()
{
	Node* temp = NULL;
	appendNode(&temp, 10);
	appendNode(&temp, 20);
	appendNode(&temp, 30);
	appendNode(&temp, 40);

	printf("链表内容如下:\n");
	printNode(temp);
	printf("\n");
	int key = 30;
	Node* result = Find_Node(temp, key);
	if (result != NULL)
	{
		printf("链表中存在key的值为%d", key);
	}
	else {
		printf("链表中不存在key的值为%d", key);
	}
	Free_Node(temp);
	return 0;
}
相关推荐
ai.Neo17 分钟前
牛客网NC22015:最大值和最小值
数据结构·c++·算法
Swift社区1 小时前
LeetCode 高频题实战:如何优雅地序列化和反序列化字符串数组?
算法·leetcode·职场和发展
徐子童2 小时前
《从零开始入门递归算法:搜索与回溯的核心思想 + 剑指Offer+leetcode高频面试题实战(含可视化图解)》
算法
天宫风子2 小时前
抽象代数小述(二之前)
经验分享·笔记·算法·生活·抽象代数
向上的车轮2 小时前
“傅里叶变换算法”来检测纸箱变形的简单示例
算法
范纹杉想快点毕业2 小时前
以项目的方式学QT开发(一)——超详细讲解(120000多字详细讲解,涵盖qt大量知识)逐步更新!
c语言·数据结构·c++·git·qt·链表·github
九亿AI算法优化工作室&2 小时前
乡村地区无人机医药配送路径规划与优化仿真
人工智能·算法·matlab·回归
米粉03053 小时前
算法图表总结:查找、排序与递归(含 Mermaid 图示)
数据结构·算法·排序算法
人类发明了工具3 小时前
【优化算法】协方差矩阵自适应进化策略(Covariance Matrix Adaptation Evolution Strategy,CMA-ES)
线性代数·算法·矩阵·cma-es
黑色的山岗在沉睡3 小时前
LeetCode100.4 移动零
数据结构·算法·leetcode