数据结构C语言版车牌号的查询与排序
</pre><pre name="code" class="cpp">
</pre><pre name="code" class="cpp">
</pre><pre name="code" class="cpp">#include<stdio.h> #define KEY_SIZE 8 #define LIST_SIZE 100 typedef struct { char key[KEY_SIZE]; char name[10]; char carname[20]; int next; }RecordType; typedef struct { RecordType r[LIST_SIZE]; int length; int keynum; }SLinkList; typedef int shuzi[10]; typedef int zimu[26]; void InitSLList(SLinkList *L) { L->length=0; L->keynum=7; } void Distribute_s(RecordType r[],int i,shuzi head,shuzi tail) { int j,p; for(j=0;j<=9;j++) { head[j]=0; tail[j]=0; } p=r[0].next; while(p!=0) { j=int(r[p].key[i]-'0'); if(head[j]==0) head[j]=p; else r[tail[j]].next=p; tail[j]=p; p=r[p].next; } } void Distribute_z(RecordType r[],int i,zimu head,zimu tail) { int p,j; for(j=0;j<=25;j++) { head[j]=0; tail[j]=0; } p=r[0].next; while(p!=0) { j=int(int(r[p].key[i])-'A'); if(head[j]==0)head[j]=p; else r[tail[j]].next=p; tail[j]=p; p=r[p].next; } } void Collectnumber(RecordType r[],shuzi head,shuzi tail) { int j=0,t; while(head[j]==0) ++j; r[0].next=head[j];t=tail[j]; while(j<9) { ++j; while((j<9)&&(head[j]==0)) ++j; if(head[j]!=0) { r[t].next=head[j]; t=tail[j]; } } r[t].next=0; } void Collectletter(RecordType r[],zimu head,zimu tail) { int j=0,t; while(head[j]==0) ++j; r[0].next=head[j];t=tail[j]; while(j<25) { ++j; while((j<25)&&(head[j]==0)) ++j; if(head[j]!=0) { r[t].next=head[j]; t=tail[j]; } } r[t].next=0; } void Sort(SLinkList *l) { int n=l->length; zimu head,tail; shuzi heads,tails; for(int i=0;i<=n-1;i++) l->r[i].next=i+1; l->r[n].next=0; for(i=6;i>2;i--) { Distribute_s(l->r,i,heads,tails); Collectnumber(l->r,heads,tails); } Distribute_z(l->r,2,head,tail); Collectletter(l->r,head,tail); for(i=1;i>=0;i--) { Distribute_s(l->r,i,heads,tails); Collectnumber(l->r,heads,tails); } } void Makeup(SLinkList *l) { int p,q; RecordType buf; p=l->r[0].next; for(int i=1;i<l->length;i++) { while(p<i) p=l->r[p].next; q=l->r[p].next; if(p!=i) { buf=l->r[p]; l->r[p]=l->r[i]; l->r[i]=buf; l->r[i].next=p; } p=q; } } void Iuputinfm(SLinkList *L) { int x; int j=1; printf("您将输入车牌信息:\n"); printf("例:01B1234 车牌格式为:01(两位数字)+B(大写字母)1234(四位数字)"); printf("\n"); printf("继续录入输入1完成录入输入0:"); scanf("%d",&x); printf("\n"); while(x) { x=0; printf("\t车牌号:"); scanf("%s",&(L->r[j].key)); printf("\t车主名:"); scanf("%s",&(L->r[j].name)); printf("\t车 名:"); scanf("%s",&(L->r[j].carname)); printf("输入1继续录入输入0完成录入:"); scanf("%d",&x); if(x) j++; } L->length=j; } void Outputinfm(SLinkList *L) { int i; printf("\t"); printf("车牌号 车主名 车名"); printf("\n"); for(i=1;i<=L->length;i++) { printf("\t%s",L->r[i].key); printf(" %s" , (L->r[i].name)); printf(" %s\n",L->r[i].carname); } } int Equal(char key1[],char key2[]) { for(int i=0;i<7;i++) { if(key1[i]!=key2[i]) return 0; } return 1; } int xiao(char key1[],char key2[]) { for(int i=0;i<7;i++) { if(key1[i]<key2[i]) return 1; else if(key1[i]>key2[i]) return 0; } return 0; } int Binsrch(SLinkList *L,char s[8]) { int mid,high,low; low=1; high=L->length; while(low<=high) { mid=(high+low)/2; if(Equal(s,L->r[mid].key)) return(mid); else if(xiao(s,L->r[mid].key)) high=mid-1; else low=mid+1; } return(0); } void noun() { printf("\t\t ========================\n"); printf("\t\t ----车牌信息管理系统---- \n"); printf("\t\t ========================\n"); printf("\t\t 选1-- 添加车辆信息 \n"); printf("\t\t-------------------\n"); printf("\t\t 选2-- 输出所有车辆信息 \n"); printf("\t\t-------------------\n"); printf("\t\t 选3-- 按车牌号码进行排序 \n"); printf("\t\t-------------------\n"); printf("\t\t 选4-- 按车牌号码查找车辆 \n"); printf("\t\t-------------------\n"); printf("\t\t 选0-- 退出程序 \n"); printf("\t\t++++++++++++++++++++++++++++++++++++\n"); } void main() { int i; SLinkList l; noun(); do { printf("输入你选择的数字(0~4):"); scanf("%d",&i); switch(i) { case 1: InitSLList(&l); Iuputinfm(&l); break; case 2: printf("------库中车牌信息:------\n"); Outputinfm(&l); break; case 3: Sort(&l); Makeup(&l); printf("------排序后的顺序为------\n"); Outputinfm(&l); break; case 4: int f; char s[7]; printf("----请输入要查找的车牌号码----\n"); scanf("%s",&s); f=Binsrch(&l,s); if(f) { printf("\t查找的车在表中的位置为:%d\n",Binsrch(&l,s)); printf("\t车牌号码:"); printf("%s",l.r[f].key); printf("\n"); printf("\t车主名:"); printf("%s\n",l.r[f].name); printf("\t车 名:"); printf("%s\n",l.r[f].carname); } else printf("此车牌号不在列表中"); printf("\n"); break; case 0: break; } }while(i!=0); }
不为失败找原因,要为成功找方法!