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;

}

相关推荐
逐光沧海1 天前
数据结构-链表习题(C++)
数据结构·c++·链表
老师我太想进步了20262 天前
【数据结构-12】树与二叉树的结构、概念
数据结构
2601_967980112 天前
Python中heapq模块的各种用法实例
数据结构·python·算法·应用实例·heapq模块
zaizai10072 天前
算法(左程云)2
数据结构
漂流瓶jz2 天前
UVA-1615 高速公路 题解答案代码 算法竞赛入门经典第二版
数据结构·算法·贪心算法·贪心·aoapc·算法竞赛入门经典·uva
夊2 天前
数据结构第四课:栈与队列
数据结构
凌冰_2 天前
数据结构 - 时间空间复杂度(一)
数据结构·算法·软件工程
郝学胜-神的一滴3 天前
Effective Python 条款36:优雅拼装迭代器与生成器
开发语言·数据结构·人工智能·python·numpy·scikit-learn
console.log('npc')3 天前
Python 学习教程 · 第4课:数据结构
数据结构·python·学习
郝学胜-神的一滴4 天前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl