注册 登录
编程论坛 数据结构与算法

两单循环链表合并代码跑不了 求助

飓风Cc 发布于 2017-03-28 13:21, 1876 次点击
#include <stdio.h>
#include <stdlib.h>


typedef int ElemType;


typedef struct LNode{
    ElemType data;
    struct LNode   *next;
    }LNode,*LinkList;//定义结点


void CreateList(LinkList L,int n)
{
    L=(LinkList)malloc(sizeof(LNode));
    L->next=L;//空的单循环链表
    int i;
    for(i=1;i<=n;i++){
       LNode *p=(LinkList)malloc(sizeof(LNode));
        scanf("%d",&p->data);
        p->next=L->next;
        L->next=p;
    }//赋值
}//倒着输入


int main()
{
    int m,n;
    m=5;
    n=4;
    LNode *p,*q;
    LinkList L1,L2,L;
    CreateList( L1, m);
    CreateList(L2, n);
    if(m<=n){
        p=L2->next;
        q=L1->next;
        L2->next=q;
        while(q->next!=L1){
            q=q->next;
        }
        q->next=p;
        free(L1);
        L=L2;
    }
    else {
        p=L1->next;
        q=L2->next;
        L1->next=q;
        while(q->next!=L2){
            q=q->next;
        }
        q->next=p;
        free(L2);
        L=L1;

    }
    p=L->next;
    while(p->next!=L){
        printf("%d \n",p->data);
    }
    return 1;


}//将小的单循环链表拆开合并到大的链表中去
在codeblocks 上没发现问题 但是运行就出问题了,求指导该,谢谢
1 回复
#2
xzlxzlxzl2017-03-28 15:02
如下代码可行:

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


typedef int ElemType;


typedef struct LNode{
    ElemType data;
    struct LNode   *next;
    }LNode,*LinkList;//定义结点


void CreateList(LinkList &L,int n)
{
    L=(LinkList)malloc(sizeof(LNode));
    L->next=L;//空的单循环链表
    int i;
    for(i=1;i<=n;i++){
       LNode *p=(LinkList)malloc(sizeof(LNode));
        scanf("%d",&p->data);
        p->next=L->next;
        L->next=p;
    }//赋值
}//倒着输入


int main()
{
    int m,n;
    m=5;
    n=4;
    LNode *p,*q;
    LinkList L1,L2,L;
    CreateList( L1, m);
    CreateList(L2, n);
    if(m<=n){
        p=L2->next;
        q=L1->next;
        L2->next=q;
        while(q->next!=L1){
            q=q->next;
        }
        q->next=p;
        free(L1);
        L=L2;
    }
    else {
        p=L1->next;
        q=L2->next;
        L1->next=q;
        while(q->next!=L2){
            q=q->next;
        }
        q->next=p;
        free(L2);
        L=L1;

    }
    p=L;
    do{
        p=p->next;
        printf("%d \n",p->data);
    }while(p->next!=L);
    return 1;


}
1