}
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->data
{ 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->exp
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;