首页 > 代码库 > 线性表实践-选票算法
线性表实践-选票算法
/* 选票系统,输入每个候选人的得票结果(采用单链表存放选票,候选人编号依次123...N,且每张选票只选一人)。 */ /* 单链表存放选票,每个节点的data域存放该选票所选的候选人,用一个数组a统计得票结果。 */ typedef int Elemtype; typedef struct linknode { Elemtype data; struct linknode *next; }Lnode; Lnode *create() { Elemtype d; Lnode h=NULL,*s,*t; int i=1; printf("创建一个单链表\n"); while(1) { printf("输入第%d节点data域值:",i); scanf("%d",&d); if(d==0) break; if(i==1) { h=(Lnode*)malloc(sizeof(Lnode)); h->data=http://www.mamicode.com/d; h->next=NULL; t=h; } else { s=(Lnode*)malloc(sizeof(Lnode)); s->data=http://www.mamicode.com/d; s->next=NULL; t->next=s; t=s; } i++; } return h; } void sat(Lnode* h,int a[]) { Lnode*p=h; while(p!=NULL) { a[p->data]++; p=p->next; } } void main() { int a[N+1]; for(i=0;i<N;i++) a[i]=0; Lnode*head; head=create(); sat(head,a); printf("候选人:"); for(i=1;i<N;i++) printf("%3d",i); printf("得票数:"); for(i=1;i<=N;i++) printf("%3d",a[i]); printf("\n"); }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。