2026.4.26数据结构 链地址法

Link_Address.h

#pragma once

#define MAXSIZE 12

//链地址法对应的单链表的有效节点结构体设计

typedef struct LA_Node

{

int data;

struct LA_Node* next;

}LA_Node;

//链地址法的表头

typedef struct LinkAddress

{

LA_Node LA_arr[MAXSIZE];

}LinkAddress;

//0.计算哈希地址函数

int Get_HashAddress(int key);

//1.初始化

void Init_LA(LinkAddress* pla);

//2.插入

bool Insert_LA(LinkAddress* pla, int key);

//3.查找

LA_Node* Search_LA(LinkAddress* pla, int key);

//4.打印

void Show(LinkAddress* pla);

//5.删除

bool Delete_LA(LinkAddress* pla, int key);

Link_Address.cpp

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <assert.h>

#include <memory.h>

#include "Link_Address.h"

//0.计算哈希地址函数

int Get_HashAddress(int key)

{

return key % MAXSIZE;

}

//1.初始化

void Init_LA(LinkAddress* pla)

{

for (int i = 0; i < MAXSIZE; i++)

{

//pla->LA_arr[i].data;//数据域不使用 不用赋值

pla->LA_arr[i].next = NULL;

}

}

//2.插入

bool Insert_LA(LinkAddress* pla, int key)

{

LA_Node* p = Search_LA(pla, key);

if (p != NULL)

{

return true;

}

int index = Get_HashAddress(key);

LA_Node*pnewnode = (LA_Node*)malloc(sizeof(LA_Node));

if (pnewnode == NULL)

exit(EXIT_FAILURE);

pnewnode->data = key;

pnewnode->next = pla->LA_arr[index].next;

pla->LA_arr[index].next = pnewnode;

return true;

}

//3.查找

LA_Node* Search_LA(LinkAddress* pla, int key)

{

int index = Get_HashAddress(key);

for (LA_Node* p = pla->LA_arr[index].next; p != NULL; p=p->next)

{

if (p->data == key)

{

return p;

}

}

return NULL;

}

//4.打印

void Show(LinkAddress* pla)

{

for (int i = 0; i < MAXSIZE; i++)

{

printf("第%d行:", i);

for (LA_Node* p = pla->LA_arr[i].next; p != NULL; p=p->next)

{

printf("%d->", p->data);

}

printf("\n");

}

}

//5.删除

bool Delete_LA(LinkAddress* pla, int key)

{

int index = Get_HashAddress(key);

LA_Node* q = Search_LA(pla, key);

if (NULL == q)

return false;

LA_Node* p = &pla->LA_arr[index];

for (; p->next != q; p = p->next);

p->next = q->next;

free(q);

q = NULL;

return true;

}

int main()

{

LinkAddress head;

Init_LA(&head);

Insert_LA(&head, 12);

Insert_LA(&head, 67);

Insert_LA(&head, 56);

Insert_LA(&head, 16);

Insert_LA(&head, 25);

Insert_LA(&head, 37);

Insert_LA(&head, 22);

Insert_LA(&head, 29);

Insert_LA(&head, 15);

Insert_LA(&head, 47);

Insert_LA(&head, 48);

Insert_LA(&head, 34);

Show(&head);

return 0;

}

相关推荐
凯瑟琳.奥古斯特2 小时前
常见排序算法性能对比
数据结构·算法·排序算法
gumichef15 小时前
算法的时间复杂度和空间复杂度
数据结构
cpp_250116 小时前
P1832 A+B Problem(再升级)
数据结构·c++·算法·动态规划·题解·洛谷·背包dp
꧁细听勿语情꧂16 小时前
合并两个有序表、判断链表的回文结构、相交链表、环的链表一和二
c语言·开发语言·数据结构·算法
大肥羊学校懒羊羊16 小时前
完数与盈数的计算题解
数据结构·c++·算法
无限进步_19 小时前
C++ 继承机制完全解析:从基础原理到菱形继承问题
java·开发语言·数据结构·c++·vscode·后端·算法
不知名的忻19 小时前
并查集(QuickUnion)
java·数据结构·算法·并查集
数智化精益手记局19 小时前
仓库安灯管理系统的异常响应机制:破解仓库安灯管理系统的跨部门协同难题
大数据·数据结构·人工智能·制造·精益工程
小张成长计划..20 小时前
【C++】25:哈希表的实现
数据结构·哈希算法·散列表