20251101
This commit is contained in:
1 parent
e0fbc20716
commit
6151e4f8c9
29 files changed
+1010
-3
No files matched your search
+2
-2
@@ -96,8 +96,8 @@ int PrintList(LinkList L)
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
LinkList bss1, bss2, bss3;
|
LinkList bss1, bss2, bss3;
|
||||||
int A[] = { 958,599,9485,95626,989923 };
|
int A[] = { 253,342,465,586,678 };
|
||||||
int B[] = { 3155,78,9623,7265,98630,983266 };
|
int B[] = { 787,895,996,1023,2096,3323 };
|
||||||
CreatList(bss1, 5, A);
|
CreatList(bss1, 5, A);
|
||||||
CreatList(bss2, 6, B);
|
CreatList(bss2, 6, B);
|
||||||
printf("第一个链表: \n");
|
printf("第一个链表: \n");
|
||||||
|
|||||||
+112
@@ -0,0 +1,112 @@
|
|||||||
|
#include<stdio.h>
|
||||||
|
#include<stdlib.h>
|
||||||
|
|
||||||
|
#define ElemType int
|
||||||
|
#define OK 1
|
||||||
|
#define OVERFLOW 0
|
||||||
|
|
||||||
|
// 链表定义
|
||||||
|
typedef struct LNode{
|
||||||
|
ElemType data;
|
||||||
|
struct LNode* next;
|
||||||
|
} LNode, * LinkList;
|
||||||
|
|
||||||
|
// 初始化链表
|
||||||
|
int InitList(LinkList &L){
|
||||||
|
L = (LNode*)malloc(sizeof(LNode));
|
||||||
|
if (!L) return OVERFLOW;
|
||||||
|
L->next = NULL;
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成新节点
|
||||||
|
LNode* MakeNode(ElemType e) {
|
||||||
|
LNode* bss;
|
||||||
|
bss = (LNode*)malloc(sizeof(LNode));
|
||||||
|
if (bss != NULL) {
|
||||||
|
bss->data = e;
|
||||||
|
bss->next = NULL;
|
||||||
|
}
|
||||||
|
return bss;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 插入节点
|
||||||
|
int Insert(LinkList &L, LNode* s){
|
||||||
|
if (!L) return OVERFLOW;
|
||||||
|
s->next = L->next;
|
||||||
|
L->next = s;
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建链表
|
||||||
|
int CreatList(LinkList& L, int n, ElemType A[]){
|
||||||
|
LNode* p, * q;
|
||||||
|
int i;
|
||||||
|
if (!InitList(L)) return OVERFLOW;
|
||||||
|
p = L;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
q = MakeNode(A[i]);
|
||||||
|
Insert(p, q);
|
||||||
|
p = q;
|
||||||
|
}
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 合并链表
|
||||||
|
int MergeList(LinkList La, LinkList Lb, LinkList& Lc)
|
||||||
|
{
|
||||||
|
LNode* pa, * pb, * pc;
|
||||||
|
pa = La->next;
|
||||||
|
pb = Lb->next;
|
||||||
|
if (!InitList(Lc)) return OVERFLOW;
|
||||||
|
pc = Lc;
|
||||||
|
while (pa && pb)
|
||||||
|
{
|
||||||
|
if (pa->data <= pb->data)
|
||||||
|
{
|
||||||
|
pc->next = pa;
|
||||||
|
pc = pa;
|
||||||
|
pa = pa->next;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pc->next = pb;
|
||||||
|
pc = pb;
|
||||||
|
pb = pb->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pc->next = pa ? pa : pb;
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印链表
|
||||||
|
int PrintList(LinkList L)
|
||||||
|
{
|
||||||
|
LNode* p = L->next;
|
||||||
|
while (p)
|
||||||
|
{
|
||||||
|
printf("%d ", p->data);
|
||||||
|
p = p->next;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
LinkList bss1, bss2, bss3;
|
||||||
|
int A[] = { 253,342,465,586,678 };
|
||||||
|
int B[] = { 787,895,996,1023,2096,3323 };
|
||||||
|
CreatList(bss1, 5, A);
|
||||||
|
CreatList(bss2, 6, B);
|
||||||
|
printf("第一个链表: \n");
|
||||||
|
PrintList(bss1);
|
||||||
|
printf("第二个链表: \n");
|
||||||
|
PrintList(bss2);
|
||||||
|
MergeList(bss1, bss2, bss3);
|
||||||
|
printf("合并后的链表: \n");
|
||||||
|
PrintList(bss3);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define Type int
|
||||||
|
|
||||||
|
// 定义哈希表
|
||||||
|
typedef struct HashTable {
|
||||||
|
Type* table;
|
||||||
|
int Length;
|
||||||
|
} HashTable;
|
||||||
|
|
||||||
|
// 哈希函数
|
||||||
|
int HashFunction(HashTable &HashTable,int key) {
|
||||||
|
return key % HashTable.Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化哈希表
|
||||||
|
void initHashTable(HashTable &HashTable) {
|
||||||
|
HashTable.table = (Type*)malloc(HashTable.Length * sizeof(Type));
|
||||||
|
for (int i = 0; i < HashTable.Length; i++) {
|
||||||
|
HashTable.table[i] = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 线性探测再散列
|
||||||
|
void insert1(HashTable &HashTable, int key) {
|
||||||
|
int index = HashFunction(HashTable, key);
|
||||||
|
while (HashTable.table[index] != -1) {
|
||||||
|
index = (index + 1) % HashTable.Length;
|
||||||
|
}
|
||||||
|
HashTable.table[index] = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 二次探测再散列
|
||||||
|
void insert2(HashTable& HashTable, int key) {
|
||||||
|
int index = HashFunction(HashTable, key);
|
||||||
|
if (HashTable.table[index] == -1) {
|
||||||
|
HashTable.table[index] = key;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i < HashTable.Length; ++i) {
|
||||||
|
int sign = (i & 1) ? 1 : -1;
|
||||||
|
int sq = (i + 1) / 2;
|
||||||
|
int d = sign * sq * sq;
|
||||||
|
int position = (index + d) % HashTable.Length;
|
||||||
|
if (position < 0) position += HashTable.Length;
|
||||||
|
if (HashTable.table[position] == -1) {
|
||||||
|
HashTable.table[position] = key;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印哈希表
|
||||||
|
void printHashTable(HashTable &HashTable) {
|
||||||
|
for (int i = 0; i < HashTable.Length; i++) {
|
||||||
|
if (HashTable.table[i] == -1) {
|
||||||
|
printf("空 ");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printf("%d ", HashTable.table[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
HashTable HashTable1, HashTable2;
|
||||||
|
HashTable1.Length = 11;
|
||||||
|
HashTable2.Length = 11;
|
||||||
|
initHashTable(HashTable1);
|
||||||
|
initHashTable(HashTable2);
|
||||||
|
int keys[] = { 19, 1, 23, 14, 55, 68, 11, 82, 36 };
|
||||||
|
int n = sizeof(keys) / sizeof(keys[0]);
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
insert1(HashTable1, keys[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
insert2(HashTable2, keys[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印哈希表
|
||||||
|
printf("线性探测再散列结果:\n");
|
||||||
|
printHashTable(HashTable1);
|
||||||
|
printf("\n");
|
||||||
|
printf("二次探测再散列结果:\n");
|
||||||
|
printHashTable(HashTable2);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loaded 3 of 29 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user