关于散列表插队买票问题
#include<stdio.h>#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define inf 1000005
#define NULL 0
int n,m,pt;
struct node{
char name[50];
int group;
}p[inf];
struct qu{
char na[50];
int group;
qu *next;
};
int find(char tmp[]){
int hi=pt-1,lo=0,mid;
while(lo<=hi){
mid=(hi+lo)>>1;
if(strcmp(tmp,p[mid].name)==0) return p[mid].group;
if(strcmp(tmp,p[mid].name) >0 ) lo=mid+1;
else hi=mid-1;
}return p[mid].group;
}
void insert(qu *head,int g,char tmp[]){
qu *add=new qu;
strcpy(add->na,tmp), add->group=g;
if(head->next==NULL){
head->next=add, add->next=NULL; return;
}
qu *t=head->next;
while(t->next!=NULL){
if(t->group == g && t->next->group!=g){
add->next=t->next;
t->next=add; return;
}else t=t->next;
}
if(t->next == NULL){
t->next=add, add->next=NULL; return;
}
}
void out(qu *head){
qu *t=head->next;
printf("%s\n",t->na);
head->next=t->next;
delete t;
}
void solve(){
char tmp[50];
int g;
qu *head=new qu;
head->next=NULL;
while(scanf("%s",tmp)){
if(strcmp(tmp,"ENQUEUE") == 0){
scanf("%s",tmp);
g=find(tmp);
insert(head,g,tmp);
qu *t=head->next;
}
else if(strcmp(tmp,"DEQUEUE") ==0) out(head);
else break;
}
}
bool cmp(node a,node b)
{return ( strcmp(a.name,b.name) < 0 );}
int main(){
int i,j,cas=1;
while(scanf("%d",&n) && n){
printf("Scenario #%d\n",cas++);
pt=0;
for(i=0;i<n;i++){
scanf("%d",&m);
for(j=0;j<m;j++){
scanf("%s",p[pt].name);
p[pt++].group=i;
}
}
sort(p,p+pt,cmp);
solve();
printf("\n");
}
return 0;
}
哪位好心人给点注释。实在看不懂