有关文件链表的c语言的题目

来源:百度知道 编辑:UC知道 时间:2024/05/16 08:39:58
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *next;
};
struct node *insert1( struct node *head, int num )
{
struct node *p;
p = (struct node *)malloc(sizeof(struct node));
p->num = num;
p->next = head;
return p;
}
void print( struct node *head )
{
struct node *p;
for ( p=head; p!=NULL; p=p->next )
printf( "%d\t", p->num );
printf( "\n" );
}
main()
{
struct node *head1=NULL, *head2=NULL, *head=NULL;
struct node *p, *q;
head1=insert1( head1, 600 );
head1=insert1( head1, 100 );
head1=insert1( head1, 400 );
head2=insert1( head2, 700 );
head2=insert1( head2, 300 );
head2=insert1( head2, 500 );
head2=insert1( head2, 200 );
for ( p=head1,q=head2; p!=NULL && q!=NULL;
p=p->next,q=q->next )
if ( p->num < q->

这是你写的程序吗?
你的程序的意思是两个链表按对应的顺序比较大小,较小者插入第三个链表中。不过你想达到的效果是什么呢?如果仅仅是两个链表的合并,for循环至少该这样写:
for ( p=head1,q=head2; p!=NULL && q!=NULL; )
if ( p->num < q->num ) {
head = insert1( head, p->num ); p=p->next;
}
else {
head = insert1( head, q->num ); q=q->next;
}