数据结构单链表(填空题)
#include“stdio.h”#include“alloc.h”structnode{chardata;structnode*next;}listnode;typed...
#include “stdio.h”
#include “alloc.h”
struct node
{
char data;
struct node *next;
} listnode;
typedef struct node *link;
void print(link head)
{
struct node *p;
printf(“\n”);
printf(“\n”);
p= head->next;
while(p)
{printf(“%c”, p->data);____(1)____ ;}
}
link creat() /*头插法建立单链表*/
{
link head ,s;
char ch;
head = malloc(sizeof(listnode));
head->next =NULL;
while(( ch= getchar())!=’\n’)
{
s= malloc(sizeof(listnode));
s->data= ch;
s->next =_____(2)_____ ;
_______(3)_______ = s;
}
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<q->next->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 ( _______(4)_____ );
print(c);
printf(“\n”);
}
输入:ysplhd
zyxrmhb
输出 ______(5)_______
__________(6)______
__________(7)_____ 展开
#include “alloc.h”
struct node
{
char data;
struct node *next;
} listnode;
typedef struct node *link;
void print(link head)
{
struct node *p;
printf(“\n”);
printf(“\n”);
p= head->next;
while(p)
{printf(“%c”, p->data);____(1)____ ;}
}
link creat() /*头插法建立单链表*/
{
link head ,s;
char ch;
head = malloc(sizeof(listnode));
head->next =NULL;
while(( ch= getchar())!=’\n’)
{
s= malloc(sizeof(listnode));
s->data= ch;
s->next =_____(2)_____ ;
_______(3)_______ = s;
}
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<q->next->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 ( _______(4)_____ );
print(c);
printf(“\n”);
}
输入:ysplhd
zyxrmhb
输出 ______(5)_______
__________(6)______
__________(7)_____ 展开
展开全部
#include "stdio.h"
#include "malloc.h"
struct node
{
char data;
struct node *next;
}listnode;
typedef struct node *link;
void print(link head)
{
struct node *p;
printf("\n");
p= head->next;
while(p)
{printf("%c",p->data);p=p->next;}
}
link creat() /*头插法建立单链表*/
{
link head ,s;
char ch;
head =(link)malloc(sizeof(listnode));
head->next =NULL;
while(( ch= getchar())!='\n')
{
s= (link)malloc(sizeof(listnode));
s->data= ch;
s->next =head->next;
head->next = s;
}
return head;
}
link merge(link a , link b)
{
link p, q, s, c;
c=(link) malloc(sizeof(listnode));
c->next =NULL;
p=a;
q=b;
while(p->next&&q->next)
{
if (p->next->data<q->next->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;
}
void 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
*/
#include "malloc.h"
struct node
{
char data;
struct node *next;
}listnode;
typedef struct node *link;
void print(link head)
{
struct node *p;
printf("\n");
p= head->next;
while(p)
{printf("%c",p->data);p=p->next;}
}
link creat() /*头插法建立单链表*/
{
link head ,s;
char ch;
head =(link)malloc(sizeof(listnode));
head->next =NULL;
while(( ch= getchar())!='\n')
{
s= (link)malloc(sizeof(listnode));
s->data= ch;
s->next =head->next;
head->next = s;
}
return head;
}
link merge(link a , link b)
{
link p, q, s, c;
c=(link) malloc(sizeof(listnode));
c->next =NULL;
p=a;
q=b;
while(p->next&&q->next)
{
if (p->next->data<q->next->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;
}
void 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
*/
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询