ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

散列表-拉链法-

2022-01-01 10:04:27  阅读:148  来源: 互联网

标签:htb temp 拉链 HashNode 列表 printf position HashTableNode


#include<stdio.h>
#include<stdlib.h>
#define M 11
typedef struct node {
	int pos;
	struct node* link;
	struct hashnode* branch;
}HashTableNode;
typedef struct hashnode {
	int key;
	struct hashnode* next;
}HashNode;
HashTableNode*   NewHSTNode(int i) {
	HashTableNode* htb;
	htb = (HashTableNode*)malloc(sizeof(HashTableNode));
	htb->pos = i;
	htb->link = NULL;
	htb->branch = NULL;
	return htb;
}
HashNode* NewHashNode(int k) {
	HashNode* p;
	p= (HashNode*)malloc(sizeof(HashNode));
	p->key = k;
	p->next = NULL;
	return p;
}
HashTableNode* CreateHashTable() {
	int i;
	HashTableNode* p, * r = NULL, * first=NULL;
	for (i = 0; i < M;i++) {
		p = NewHSTNode(i);
		if (first!=NULL) {
			r->link = p;
		}
		else {
			first = p;
		}
		r = p;
	}
	return first;
}
void Search(HashTableNode* first, int k) {
	int position;
	HashNode * s;
	HashTableNode* temp;
	temp = (HashTableNode*)malloc(sizeof(HashTableNode));
	s = (HashNode*)malloc(sizeof(HashNode));
	temp = first;
	position = k % M;
	for (; temp; temp = temp->link) {
		if (temp->pos == position) {
			break;
		}
	}
	if (temp->branch == NULL) {
		printf("Find Nothing on %d\n",k);
	}
	else {
		s = temp->branch;
		while (s) {
			if (s->key==k) {
				printf("find %d, position is: %d\n",k, temp->pos);
				return;
			}
			else {
				s = s->next;
			}
		}
		printf("Find Nothing on %d\n", k);
	}
}
void Insert(HashTableNode* first, int k) {
	int position;
	HashNode* p, * s;
	HashTableNode* temp;
	s = (HashNode*)malloc(sizeof(HashNode));
	temp = (HashTableNode*)malloc(sizeof(HashTableNode));
	p = NewHashNode(k);
	position = k % M;
	temp = first;
	for (; temp; temp = temp->link) {
		if (temp->pos==position) {
			break;
		}
	}
	if(temp->branch ==NULL) {
		temp->branch = p;
	}
	else {
		s = temp->branch;
		while (s->next) {
			s = s->next;
		}
		s->next = p;
	}
}
void Delete(HashTableNode* first, int k) {
	int position;
	HashNode* s, *p;
	HashTableNode* temp;
	s = (HashNode*)malloc(sizeof(HashNode));
	p = (HashNode*)malloc(sizeof(HashNode));
	temp = (HashTableNode*)malloc(sizeof(HashTableNode));
	position = k % M;
	temp = first;
	for (; temp; temp = temp->link) {
		if (temp->pos == position) {
			break;
		}
	}
	if (temp->branch == NULL) {
		printf("Find Nothing on %d, cannot delete!\n", k);
	}
	else {
		s = temp->branch;
		while (s) {
			if (s->key == k) {
				if (s== temp->branch) {
					printf("find %d, position is: %d  ", k, temp->pos);
					temp->branch=s->next;
					printf("has been deleted!\n");
					free(s);
					return;
				}
				else {
					printf("find %d, position is: %d  ", k, temp->pos);
					printf("has been deleted!\n");
					p->next = s->next;
					free(s);
					return;
				}
				
			}
			else {
				p = s;
				s = s->next;
			}
		}
		printf("Find Nothing on %d, cannot delete!\n", k);
	}
}
void PrintHashTable(HashTableNode* first) {
	HashNode* s;
	HashTableNode* temp;
	s = (HashNode*)malloc(sizeof(HashNode));
	temp = (HashTableNode*)malloc(sizeof(HashTableNode));
	printf("HashTable Content here: \n");
	temp = first;
	for (; temp; temp = temp->link) {
		printf("Position[%d]:  ", temp->pos);
		if (temp->branch!=NULL) {
			s = temp->branch;
			while (s) {
				printf("%d  ",s->key );
				s = s->next;
			}
		}
		printf("\n");
	}
}
void main() {
	HashTableNode* htb;
	htb = CreateHashTable();
	Insert(htb, 11);
	Insert(htb, 33);
	Insert(htb, 55);
	Insert(htb, 66);
	Insert(htb, 36);
	Insert(htb, 69);
	Insert(htb, 16);
	Insert(htb, 49);
	Insert(htb, 82);
	Delete(htb, 49);
	Search(htb, 82);
	PrintHashTable(htb);
	system("pause");
}

Console out result:

find 49, position is: 5 has been deleted!
find 82, position is: 5
HashTable Content here:
Position[0]: 11 33 55 66
Position[1]:
Position[2]:
Position[3]: 36 69
Position[4]:
Position[5]: 16 82
Position[6]:
Position[7]:
Position[8]:
Position[9]:
Position[10]:
请按任意键继续. . .

C:\C程序&C语言数据结构\C语言数据结构_从入门到入坑\9\拉链法-散列表\Debug\拉链法-散列表.exe (进程 1800)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .

标签:htb,temp,拉链,HashNode,列表,printf,position,HashTableNode
来源: https://blog.csdn.net/m0_47472749/article/details/122265326

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有