
数据结构顺序表的合并c语言无法输出合并后的表 20
2个回答
展开全部
我自己写过一段一样的代码,用的是链表,你要是有兴趣可以对比一下。
#include <stdio.h>
#include <malloc.h>
/*链表节点定义*/
typedef struct node
{
int data;
struct node *next;
}node;
typedef struct list
{
node *h;
int length;
}list;
list l;
node *head1,*head2,*Head;
/*链表节点创建*/
void initiatesl(node **h)
{
*h = (node*)malloc(sizeof(node));
(*h)->next = NULL;
}
/*根据给定表长生成基础表1、2*/
void create_list(node *h,int i)
{
int n = 0;
node *p,*q = h;
for(;n < i;n++)
{
initiatesl(&p);
q->next = p;
scanf("%d",&q->next->data);
q = q->next;
}
}
/*链表合成*/
int merge(node *H,node *h1,node *h2)
{
int n = 0;
node *p = h1->next,*q = h2->next,*r = H,*s = H;
for(;p != NULL && q != NULL;r = r->next,n++)
{
initiatesl(&r->next);
if(p->data < q->data)
{
r->next->data = p->data;
p = p->next;
}
else if(p->data > q->data)
{
r->next->data = q->data;
q = q->next;
}
else
{
r->next->data = p->data;
p = p->next;
q = q->next;
}
}
if(p == NULL)
for(;q != NULL;q = q->next,r = r->next,n++)
{
initiatesl(&r->next);
r->next->data = q->data;
}
else
for(;p != NULL;p = p->next,r = r->next,n++)
{
initiatesl(&r->next);
r->data = p->data;
}
return n;
}
/*主函数及交互界面*/
int main(void)
{
int a,b;
node *s,*S = head1;
initiatesl(&head1);/*创建三个头结点*/
initiatesl(&head2);
initiatesl(&Head);
printf("Please input the length of the two list.(a,b)\n");/*给定长度输入样式*/
scanf("%d,%d",&a,&b);
printf("Input list one:");/*创建链表1、2*/
create_list(head1,a);
printf("Input list two:");
create_list(head2,b);
l.length = merge(Head,head1,head2);
l.h = Head;
s = Head->next;
printf("L :");
for(;s != NULL;s = s->next)
printf("%d ",s->data);
printf("\n");
return 0;
}
#include <stdio.h>
#include <malloc.h>
/*链表节点定义*/
typedef struct node
{
int data;
struct node *next;
}node;
typedef struct list
{
node *h;
int length;
}list;
list l;
node *head1,*head2,*Head;
/*链表节点创建*/
void initiatesl(node **h)
{
*h = (node*)malloc(sizeof(node));
(*h)->next = NULL;
}
/*根据给定表长生成基础表1、2*/
void create_list(node *h,int i)
{
int n = 0;
node *p,*q = h;
for(;n < i;n++)
{
initiatesl(&p);
q->next = p;
scanf("%d",&q->next->data);
q = q->next;
}
}
/*链表合成*/
int merge(node *H,node *h1,node *h2)
{
int n = 0;
node *p = h1->next,*q = h2->next,*r = H,*s = H;
for(;p != NULL && q != NULL;r = r->next,n++)
{
initiatesl(&r->next);
if(p->data < q->data)
{
r->next->data = p->data;
p = p->next;
}
else if(p->data > q->data)
{
r->next->data = q->data;
q = q->next;
}
else
{
r->next->data = p->data;
p = p->next;
q = q->next;
}
}
if(p == NULL)
for(;q != NULL;q = q->next,r = r->next,n++)
{
initiatesl(&r->next);
r->next->data = q->data;
}
else
for(;p != NULL;p = p->next,r = r->next,n++)
{
initiatesl(&r->next);
r->data = p->data;
}
return n;
}
/*主函数及交互界面*/
int main(void)
{
int a,b;
node *s,*S = head1;
initiatesl(&head1);/*创建三个头结点*/
initiatesl(&head2);
initiatesl(&Head);
printf("Please input the length of the two list.(a,b)\n");/*给定长度输入样式*/
scanf("%d,%d",&a,&b);
printf("Input list one:");/*创建链表1、2*/
create_list(head1,a);
printf("Input list two:");
create_list(head2,b);
l.length = merge(Head,head1,head2);
l.h = Head;
s = Head->next;
printf("L :");
for(;s != NULL;s = s->next)
printf("%d ",s->data);
printf("\n");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询