用visual C++编程

来源:百度知道 编辑:UC知道 时间:2024/05/29 05:27:27
设A和B分别为两个带头结点的有序循环链表(数据域值按从小到大排列)list1和list2分别指向两个指向链表的指针。写出将这两个链表结合并为一个带头结点的有序循环链表的程序

#include <stdio.h>
#include <stdlib.h>

typedef struct List{
int num ;
struct List * next ;
} L ;

void buildLink(struct List * head)
{
L *p, *q ;
q = head;
int i;
for ( i = 1; i <= 5 ; i++)
{
p=(L *)malloc(sizeof(L));
p->num = i;
q->next=p;
q = p;
}
p->next = NULL;
}

void main()
{
L *p , *q , *r, *head1, *head2 ;
q=head1=(L *)malloc(sizeof(L));

int i;
for (i=1 ; i<=4 ; i++)
{
p = (L *)malloc(sizeof(L));
printf("Input number %d please:",i);
scanf("%d",&p->num);
q ->next = p ;
q = p ;
}
p->next = NULL ;

head2 = (L *)malloc(sizeof(L));
buildLink(head2);

p = head2->next ;
while(p!=NULL)
{
printf("%d\n",p->num);
p = p->next ;