1、关于数据结构的一道题

2024-10-31 22:27:59
推荐回答(2个)
回答1:

算了,贴这儿吧。
#include
#include

typedef struct Lnode{
int data;
struct Lnode *next;
}Lnode,*link;
void main()
{
int m,n;
link p1,p2,list;
Lnode *initlnode(int n);
Lnode *mergelist(link p1,link p2);
void printf(link list);

scanf("%d %d",&m,&n);
p1=initlnode(m);
p2=initlnode(n);
list=mergelist(p1,p2);
printf(list);
}
link initlnode(int y)
{
link head=NULL;
while (y--)
{
link temp = (link)malloc(sizeof(Lnode));
scanf("%d",&temp->data);
if (head && head->datadata)
{
link before = head;
while (before->next!=head&&temp->data>before->data)
before = before->next;
temp->next = before->next;
before->next = temp;
}
else
{
temp->next = head?head:temp;
head = temp;
}
}
return head;
}
link mergelist(link p1,link p2)
{
link head = NULL;
link builder = head;
link p1Loader = p1, p2Loader = p2;
bool p1Round=false,p2Round=false;
while (1)
{
int v1,v2,v=-1;
if (p1Loader==p1 && p1Round)
break;
if (p2Loader==p2 && p2Round)
break;
v1 = p1Loader->data;
v2 = p2Loader->data;
if (v1 {
v = v1;
p1Loader = p1Loader->next;
p1Round = true;
}
else
{
v = v2;
p2Loader = p2Loader->next;
p2Round = true;
}

link temp = (link)malloc(sizeof(Lnode));
temp->data = v;
temp->next = NULL;

if (head)
{
builder->next = temp;
builder = temp;
}
else
{
builder = temp;
head = temp;
}
}
if (p1Round && p1Loader==p1)
{
int v;
while (1)
{
if (p2Loader==p2)
if (!p2Round)
p2Round = true;
else
break;
v = p2Loader->data;
p2Loader = p2Loader->next;
link temp = (link)malloc(sizeof(Lnode));
temp->data = v;
temp->next = NULL;

if (head)
{
builder->next = temp;
builder = temp;
}
else
{
builder = temp;
head = temp;
}
}
}
if (p2Round && p2Loader==p2)
{
int v;
while (1)
{
if (p1Loader==p1)
if (!p1Round)
p1Round = true;
else
break;
v = p1Loader->data;
p1Loader = p1Loader->next;

link temp = (link)malloc(sizeof(Lnode));
temp->data = v;
temp->next = NULL;

if (head)
{
builder->next = temp;
builder = temp;
}
else
{
builder = temp;
head = temp;
}
}
}
builder->next = head;
return head;
}
void printf(link list)
{
link q;
q=list;
while(q->next!=list)
{
printf("%d\t",q->data);
q=q->next;
}
printf("%d\t",q->data);

}

回答2:

你的代码,太啰嗦了,实在没有耐心看完!

typedef struct Lnode{
int data;
struct Lnode *next;
}Lnode,*link;
--》
typedef struct lnode{
int data;
struct lnode *next;
}Lnode,*link;

其他没仔细看!