#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct listnode *position;
struct listnode
{
char a[10] ;
int group;
position next;
} ;
typedef position list;
struct hashtbl
{
int tablesize;
list *thelists;
};
typedef struct hashtbl *hashtable;
hashtable InitializeTable(int tablesize)
{
hashtable H;
int i;
H=malloc(sizeof(struct hashtbl));
if(H==NULL)
{
printf("out of space1!!") ;
}
H->tablesize=tablesize;
H->thelists=malloc(sizeof(list)*H->tablesize);
if(H->thelists==NULL)
{ printf("out of space2!!");
}
for(i=0;i<H->tablesize;i++)
{
H->thelists[i]=malloc(sizeof(struct listnode));
if(H->thelists[i]==NULL)
{
printf("out of space3!!");
}
else
H->thelists[i]->next=NULL;
}
return H;
}
void insert(char *s,hashtable H,int g)
{
position p, newcell;
list l;
??? newcell=malloc(sizeof(struct listnode));
if(newcell==NULL)
{ printf("out of space4!!");
}?????
else
{
l=H->thelists[s[0]-'A'];
p=l->next;
while(p!=NULL)
p=p->next;
p->next=newcell;
newcell->next=NULL;
strcpy(newcell->a,s);
newcell->group=g;
}
}
int find(char *s,hashtable H)
{
position p;
list l;
l=H->thelists[s[0]-'A'];
p=l->next;
while(p!=NULL&&strcmp(p->a,s)!=0)
p=p->next;
return p->group;
}
void ENQUEUE(char *s,int group,list l)
{ position p,temp,newcell;
p=l->next;
temp=l;
while(p!=NULL&&p->group>group)
{
temp=p;
p=p->next;
}
newcell=malloc(sizeof(struct listnode));
if(newcell==NULL)printf("out of space!!");
else
{
strcmp(newcell->a,s);
newcell->group=group;
temp->next=newcell;
newcell->next=p;
}
}
void DEQUEUE(list l)
{ position temp;
temp=l;
l=l->next;
free(temp);
}
main()
{
hashtable h;
list queue;
FILE *input,*output;
char s[10];
int group;
int k,a[1000];
int i,j;
if((input=fopen("input.txt","r"))==NULL)
{ printf("can't open the file1\n");
exit(0);
}
if((output=fopen("output.txt","w"))==NULL)
{ printf("can't open the file2\n");
exit(0);
}
while(1)
{ queue=malloc(sizeof(struct listnode));
queue->next=NULL;
fscanf(input,"%d",&k);
if(k==0)break;
h=InitializeTable(26);
for(i=0;i<k;i++)
{
fscanf(input,"%d",&a[i]);
for(j=0;j<a[i];j++)
{
fscanf(input,"%s",s);
/*insert(s,h,i); */
fprintf(output,"%s\n",s);
}
fprintf(output,"\n");
}
while(fscanf(input,"%s",s))
{
if(strcmp("stop",s)==0)break;
else if(strcmp("ENQUEUE",s)==0)
{
fscanf(input,"%s",s);
group=find(s,h);
ENQUEUE(s,group,queue);
}
else if(strcmp("DEQUEUE",s)==0)
{ fprintf(output,"%s\n",s);
DEQUEUE(queue);
}
}
}
fclose(input);
fclose(output);
}
打问号的地方,不能申请内存,部知道什么原因,请个位帮帮忙。
我用的是c-free编译的