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_arrMAXSIZE;

}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_arri.data;//数据域不使用 不用赋值

pla->LA_arri.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_arrindex.next;

pla->LA_arrindex.next = pnewnode;

return true;

}

//3.查找

LA_Node* Search_LA(LinkAddress* pla, int key)

{

int index = Get_HashAddress(key);

for (LA_Node* p = pla->LA_arrindex.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_arri.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_arrindex;

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;

}

相关推荐
Lost of 程序猿3 小时前
.NET 线程安全集合与并发数据结构深度实战:从 lock 到无锁
数据结构·安全·.net
机器学习之心4 小时前
基于BiGRU-Attention的轴承剩余寿命预测(MATLAB实现):从振动信号到RUL曲线的完整闭环
数据结构·算法·matlab·轴承剩余寿命预测·振动信号·bigru-attention
青梅橘子皮4 小时前
优选算法---专题2(滑动窗口)
数据结构·算法
程序猫.4 小时前
双指针问题
java·数据结构·算法
50万马克的面包4 小时前
数据结构:线性表 —— 顺序表与链表完整总结
数据结构·链表
白狐_7985 小时前
408 数据结构|散列表构造题:开放定址法做题套路
数据结构·散列表
runningshark5 小时前
CH-6图论基础-有向树
数据结构
旖旎夜光5 小时前
LeetCode 525:连续数组(前缀和) —— 题解
数据结构·c++·算法·leetcode·前缀和
白狐_7985 小时前
408 数据结构|双散列与探测覆盖率:互质判断法
数据结构
shehuiyuelaiyuehao15 小时前
算法31,前缀和,可被k整除的子数组
数据结构·python·算法