主题:关于两双链表的连接
这个本来是很简单的 但我现在的问题是使用函数后 不是完全正确 没首尾连上 用的是TC2.0 下面是程序 谢谢提个醒 就应该是合并部分的问题
#include "stdio.h"
typedef int ElemType;
typedef struct dnode
{ ElemType data;
struct dnode *prior;
struct dnode *next;
}dlink;
dlink *credlink(void)
{ dlink *p,*q,*head;ElemType x;
p=head=(dlink *)malloc(sizeof(dlink));
scanf("%d",&x);
while(x!=-1)
{ q=(dlink *)malloc(sizeof(dlink));
q->data=x;
p->next=q;
q->prior=p;
p=q;
scanf("%d",&x);
}
p->next=head;
head->prior=p;
return head;
}
void combine(dlink *head,dlink *head1) /*合并函数*/
{ dlink *p,*q;
p=head->next;q=head1->next;
while(p->next!=head)
{ p=p->next;}
while(q->next!=head1)
{ q=q->next;}
head1->next->prior=p; /*就是这地方 我觉得有错 大家看看吧*/
head->next->prior=q;
p->next=head1;
q->next=head;
}
void output(dlink *head)
{ dlink *p;
p=head->next;
while(p!=head)
{ printf("%-4d",p->data);
p=p->next;
}
printf("\n");
}
main()
{ dlink *H,*K;
H=credlink();
K=credlink();
output(H);
output(K);
combine(H,K);
output(H);
}
#include "stdio.h"
typedef int ElemType;
typedef struct dnode
{ ElemType data;
struct dnode *prior;
struct dnode *next;
}dlink;
dlink *credlink(void)
{ dlink *p,*q,*head;ElemType x;
p=head=(dlink *)malloc(sizeof(dlink));
scanf("%d",&x);
while(x!=-1)
{ q=(dlink *)malloc(sizeof(dlink));
q->data=x;
p->next=q;
q->prior=p;
p=q;
scanf("%d",&x);
}
p->next=head;
head->prior=p;
return head;
}
void combine(dlink *head,dlink *head1) /*合并函数*/
{ dlink *p,*q;
p=head->next;q=head1->next;
while(p->next!=head)
{ p=p->next;}
while(q->next!=head1)
{ q=q->next;}
head1->next->prior=p; /*就是这地方 我觉得有错 大家看看吧*/
head->next->prior=q;
p->next=head1;
q->next=head;
}
void output(dlink *head)
{ dlink *p;
p=head->next;
while(p!=head)
{ printf("%-4d",p->data);
p=p->next;
}
printf("\n");
}
main()
{ dlink *H,*K;
H=credlink();
K=credlink();
output(H);
output(K);
combine(H,K);
output(H);
}