主题:大家帮我看看这个散列表实现符号表的问题!!!谢谢
不知道为什么没有结果输出??
下面是代码:
#include <stdlib.h>
#include <stdio.h>
typedef int setitem;
typedef struct node *Node;
typedef struct node {
setitem x;
Node next;
}Nnode;
Node Newnode(){
Node n=malloc(sizeof(Nnode));
n->next=0;
return n;
}
typedef struct list *List;
typedef struct list{
Node first;
}Llist;
List NewList(){
List L=malloc(sizeof(*L));
L->first=0;
return L;
}
int hashes(int x){
return x/2;
}
typedef struct open *openhash;
typedef struct open{
int size;
int (*hf)(setitem x);
List *ht;
}Open;
void listinsert(List L,setitem x){
Node n=Newnode();
n->x=x;
n->next=L->first;
}
openhash Innitopen(int sizeop,int(*open)(setitem x)){
int i;
openhash oh=malloc(sizeof(*oh));
oh->hf=open;
oh->size=sizeop;
oh->ht=malloc(sizeop*sizeof(List));
for(i=0;i<sizeop;i++)
oh->ht[i]=NewList();
return oh;
}
void insertopen(setitem x,openhash op){
int i;
i=(*(op->hf))(x)%op->size;
listinsert(op->ht[i],x);
}
void printx(openhash op){
int i;
Node p;
for(i=0;i<op->size;i++){
List L=op->ht[i];
for(p=L->first;p;p=p->next)
printf("%d ",p->x);
}
}
void main(){
openhash op=Innitopen(5,hashes);
insertopen(100,op);
insertopen(90,op);
insertopen(500,op);
insertopen(200,op);
printx(op);
}
[color=FF0000][/color]
下面是代码:
#include <stdlib.h>
#include <stdio.h>
typedef int setitem;
typedef struct node *Node;
typedef struct node {
setitem x;
Node next;
}Nnode;
Node Newnode(){
Node n=malloc(sizeof(Nnode));
n->next=0;
return n;
}
typedef struct list *List;
typedef struct list{
Node first;
}Llist;
List NewList(){
List L=malloc(sizeof(*L));
L->first=0;
return L;
}
int hashes(int x){
return x/2;
}
typedef struct open *openhash;
typedef struct open{
int size;
int (*hf)(setitem x);
List *ht;
}Open;
void listinsert(List L,setitem x){
Node n=Newnode();
n->x=x;
n->next=L->first;
}
openhash Innitopen(int sizeop,int(*open)(setitem x)){
int i;
openhash oh=malloc(sizeof(*oh));
oh->hf=open;
oh->size=sizeop;
oh->ht=malloc(sizeop*sizeof(List));
for(i=0;i<sizeop;i++)
oh->ht[i]=NewList();
return oh;
}
void insertopen(setitem x,openhash op){
int i;
i=(*(op->hf))(x)%op->size;
listinsert(op->ht[i],x);
}
void printx(openhash op){
int i;
Node p;
for(i=0;i<op->size;i++){
List L=op->ht[i];
for(p=L->first;p;p=p->next)
printf("%d ",p->x);
}
}
void main(){
openhash op=Innitopen(5,hashes);
insertopen(100,op);
insertopen(90,op);
insertopen(500,op);
insertopen(200,op);
printx(op);
}
[color=FF0000][/color]