习题二和上机答案(4)

2019-04-17 15:13

}

return head; }

link merge(link a , link b) {

link p , q , s , c;

c= malloc(sizeof(listnode)); c->next =NULL; p=a; q=b;

while(p->next&&q->next) {

if (p->next->datanext->data) { s = p->next;p->next=s->next;} else

{ s = q->next;q->next = s->next;} s->next = c->next; c->next = s; }

while (p->next) {

s = p->next; p->next = s->next; s->next = c->next; c->next = s; }

while(q->next) {

s = q->next;

q->next = s->next; s->next = c->next; c->next = s; }

free(p);free(q); return c; } main() {

link a , b , c; a = creat(); b = creat(); print(a); print(b); c = merge ( a , b); print(c); printf(“\\n”); }

输入:ysplhd zyxrmhb 输出:dhlpsy

bhmrxyz zyyxsrpmlhhdb

2. 生成两个多项式PA和PB,求PA和PB之和,输出“和多项式”。 解:typedef struct node {int exp; float coef;

struct node *next; }polynode; polynode *p,*q;

polynode *polyadd(pa,pb) polynode *pa,*pb; {polynode *p,*q,*pre,*r; float x; p=pa->next; q=pb->next; pre=pa;

while ((p!=NULL)&&(q!=NULL)) if (p->exp>q->exp) {r=q->next; q->next=p; pre->next=q; pre=q; q=r; } else

if(p->exp==q->exp) {x=p->coef+q->coef; if (x!=0) {p->coef=x; s=p; } else

{pre->next=p->next; free(p); }

p=pre->next; r=p; q=q->next; free(r); } else

if (p->expexp) {pre=p; p=p->next; } } if (q!=NULL) pre->next=q; free(pb); }

3.设计一个统计选票的算法,输出每个候选的得票结果(假设采用单链表存放选票,候选人编号依次为1,2,3,??,N,且每张选票选且只选一人)

提示:以单链表存放选票,每个结点的data域存放该选票所选的候选人。用一个数组a统计得票结果。

typedef int elemtype; typedef struct linknode {

elemtype data; struct linknode *next; } nodetype; nodetype *create() {

elemtype d;

nodetype 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=(nodetype *)malloc(sizeof(nodetype)); h->data=d;h->next=NULL;t=h; } else

{

s=(nodetype *)malloc(sizeof(nodetype)); s->data=d;s->next=NULL;t->next=s; t=s; } I++; } return h; }

void sat (nodetype *h, int a[]) {

nodetype *p=h; while (p!=NULL)

{

a[p->data]++; p=p->next;


习题二和上机答案(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:麻醉主治医师考试基础知识模拟试题(二)_ss

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: