LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 477|回复: 1

双链表的问题!!请教高手

[复制链接]
发表于 2004-12-8 22:04:51 | 显示全部楼层 |阅读模式
我的代码实现的功能是归并两个双向链表,代码如下:
#include <stdio.h>
#include <malloc.h>
struct Dnode{
        int data;
        struct Dnode *next;
        struct Dnode *prior;
};
void createlist(struct Dnode *head)
{
        int e;
        struct Dnode *p,*q;
        q=head;
        printf("输入非零数据:\n");
        scanf("%d",&e);
        while(e){
                p=(struct Dnode *)malloc(sizeof(struct Dnode));
                p->data=e;
                q->next=p; p->prior=q;
                q=p;
                scanf("%d",&e);
        }
        p->next=head;
        head->prior=p;
}
void display(struct Dnode *head)
{
        struct Dnode *p;
        p=head->next;
        while(p!=head){
                printf("%d\t",p->data);
                p=p->next;
        }
        printf("\n");
}
void freelist(struct Dnode *head )
{
        struct Dnode *p,*temp;
        p=head->next;
        while( p!=head ) {
                temp=p;
                p=p->next;
                free(temp);
        }
        free(head);
}
void merge(struct Dnode *head1,struct Dnode *head2,struct Dnode *head3)
{
        struct Dnode *p1,*p2,*p3,*p;
        p1=head1->next;
        p2=head2->next;
        p3=head3;
        while(p1!=NULL&&p2!=NULL){
                if((p1->data)<=(p2->data)){
                        p=(struct Dnode *)malloc(sizeof(struct Dnode));
                        p->data=p1->data;
                        p1=p1->next;
                        p3->next=p;
                        p->prior=p3;
                        p3=p3->next;
                }
                else{
                        p=(struct Dnode *)malloc(sizeof(struct Dnode));
                        p->data=p2->data;
                        p2=p2->next;
                        p3->next=p;
                        p->prior=p3;
                        p3=p3->next;
                }

        }
        while(p1!=NULL){
                p=(struct Dnode *)malloc(sizeof(struct Dnode));
                p->data=p1->data;
                p3->next=p;
                p->prior=p3;
                p1=p1->next;
                p3=p3->next;
        }
        while(p2!=NULL){
                p=(struct Dnode *)malloc(sizeof(struct Dnode));
                p->data=p2->data;
                p3->next=p;
                p->prior=p3;
                p2=p2->next;
                p3=p3->next;
        }
    p3->next=head3;
        head3->prior=p3;
}

void main()
{
        struct Dnode *head1,*head2,*head3;
        head1=(struct Dnode *)malloc(sizeof(struct Dnode));
        head1->next=head1;
        head1->prior=head1;
        printf("输入数据<递归>\n");
        createlist(head1);
        printf("正向生成的链表1为:\n");
        display(head1);
        head2=(struct Dnode *)malloc(sizeof(struct Dnode));
        head2->next=head2;
        head2->prior=head2;
        createlist(head2);
        printf("输入数据<递归>\n");
        printf("正向生成的链表2为:\n");
        display(head2);
        head3=(struct Dnode *)malloc(sizeof(struct Dnode));
        head3->next=head3;
        head3->prior=head3;

        merge(head1,head2,head3);
        printf("归并后的链表为:\n");
        display(head3);

        freelist(head1);
        freelist(head2);
        freelist(head3);
}
编译没有问题,运行时出错,不知为何。困惑中!~~~~~
另外,我的方法有点笨,哪位有更高级的方法啊?可否贴出来啊??^_^
发表于 2004-12-9 10:04:44 | 显示全部楼层
代码缩进呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表