From 0f2bfea4c6cda6e057bf16ac8fee5e3f0834ce40 Mon Sep 17 00:00:00 2001 From: LSM <1185424014@qq.com> Date: Fri, 8 Jan 2021 23:42:49 +0800 Subject: [PATCH] dasd --- "\346\225\243\345\210\227\350\241\250.txt" | 114 +++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 "\346\225\243\345\210\227\350\241\250.txt" diff --git "a/\346\225\243\345\210\227\350\241\250.txt" "b/\346\225\243\345\210\227\350\241\250.txt" new file mode 100644 index 0000000..bca549c --- /dev/null +++ "b/\346\225\243\345\210\227\350\241\250.txt" @@ -0,0 +1,114 @@ + +typedef struct node{ + int key; + int num; + struct node * next; + struct node * pre; +}Node, *Pnode; + +typedef struct Hash{ + Pnode link[N]; + int num; + int len; +}hash,*Phash; + +Pnode IniPnode(int key){ + Pnode p=(Pnode)malloc(sizeof(Node)); + p->key=key; + p->num=1; + p->next=NULL; + p->pre=NULL; +} + +int HashPos(int key){ + return key % Nhash; +} + +Pnode FindNodePos(Phash h, int key){ + int pos=HashPos(key); + Pnode link = h->link[pos]; + while(link->next != NULL && link->key != key){ + link=link->next; + } + return link; +} + +void IniHash(Phash *h, int len){ + int i; + *h=(Phash)malloc(sizeof(hash)); + for(i=0;ilink[i] = IniPnode(-1); + } + (*h)->num =0; + (*h)->len=len; +} + +void Insert(Phash h, int key){ + Pnode p=FindNodePos(h,key); + if(p->next != NULL) p->num ++; + else{ + Pnode q =IniPnode(key); + p->next = q; + q->pre=p; + } + ++ h->num ; +} + +Pnode Search(Phash h, int key){ + Pnode p=FindNodePos(h,key); + if(p->next = NULL) return NULL; + else return p; +} + +int Delete(Phash h, int key){ + Pnode p=FindNodePos(h,key); + p->pre->next=p->next; + free(p); +} + +void PrintLink(Pnode p){ + while(p->next!=NULL){ + printf("[key=%d|num=%d] -> ",p->next->key,p->next->num); + p=p->next; + } + //printf("[key=%d|num=%d]",p->key,p->num); + printf("\n"); +} + +void PrintHash(Phash h){ + int i; + printf("哈希表中公有结点%d个\n",h->num); + for(i=0;ilen;i++){ + printf("%d\t",i); + PrintLink(h->link[i]); + } +} + +void DeleteHash(Phash h){ + int i; + for(i=0;inum;i++){ + free(h->link[i]); + } + free(h); +} + +int main(){ + int i, a[N]; + Phash h=NULL; + IniHash(&h,Nhash); + + srand((unsigned)time(NULL)); + for(i=0;i