哈希表.txt
来自「数据结构学习用到的一些程序!!里面有二叉树相关的几个」· 文本 代码 · 共 48 行
TXT
48 行
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int number;
struct node *next;
}Node;
int main(void){
int i,first=1;
Node *hash[1000]={NULL};
for(i=0;i<1000;i++){
int inp,fx;
scanf("%d",&inp);
fx=inp/10;
if(hash[fx]==NULL){
hash[fx]=(Node *)malloc(sizeof(Node));
hash[fx]->number=inp;
hash[fx]->next=NULL;
}else if(hash[fx]->number>inp){
Node *temp;
temp=(Node *)malloc(sizeof(Node));
temp->number=inp;
temp->next=hash[fx];
hash[fx]=temp;
}else{
Node *temp,*insert;
for(temp=hash[fx]; temp->next!=NULL;temp=temp->next)
if(temp->next->number>inp)
break;
insert=(Node *)malloc(sizeof(Node));
insert->number=inp;
insert->next=temp->next;
temp->next=insert;
}
}
for(i=1;i<1000;i++){
Node *temp;
for(temp=hash[i];temp!=NULL;temp=temp->next)
if(first){
printf("%d",temp->number);
first=0;
}else
printf(" %d",temp->number);
}
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?