标题:求两个链表的交集
只看楼主
ldsh304
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:7
帖 子:242
专家分:755
注 册:2016-1-18
结帖率:100%
已结贴  问题点数:9 回复次数:2 
求两个链表的交集
/* 2.12 假设有两个按元素值递增有序排列的有序表A和B,均以单链表作存储结构。
 *请编写算法利用A表和B表中原有的结点将A表和B表归并成一个按元素值非递增有序排列的有序表C。
 *( 例如: A=(1,2.3,4.6),B=(2,3,5,7,9),则 C=(9,7,6,5.4,3,3,2,2,1) )
 */
 

# include <stdio.h>
# include <malloc.h>

typedef struct node
{
    int data;
    node *pNext;
}NODE, *PNODE;

PNODE create(int array[])//创建链表
{
    PNODE pHead, pTail, pNew;
   
    pHead = (PNODE)malloc(sizeof(NODE));
    pTail = pHead;
    pTail->pNext = NULL;
    for (int i=0; i<5; i++)
    {
        pNew = (PNODE)malloc(sizeof(NODE));
        pNew->data = array[i];
        pTail->pNext = pNew;
        pNew->pNext = NULL;
        pTail = pNew;
    }
    return pHead;
}

void traverse(PNODE pHead)//遍历链表
{
    PNODE pTemp = pHead->pNext;
   
    while (NULL != pTemp)
    {
        printf("%d ", pTemp->data);
        pTemp = pTemp->pNext;
    }
    printf("\n");
   
}

PNODE coalition(PNODE A, PNODE B)// A 和 B 的交集
{
    PNODE pHead, pTail, pNew;
    PNODE pTemp1 = A->pNext;
    PNODE pTemp2 = B->pNext;
    PNODE p = NULL;
    PNODE q = pTemp2;
   
    pHead = (PNODE)malloc(sizeof(NODE));
    pTail = pHead;
    pTail->pNext = NULL;
   
    while (NULL != pTemp1)
    {
        printf("%d-----1--------------\n", pTemp1->data);
        p = pTemp1->pNext;
        //保存数据
        pNew = (PNODE)malloc(sizeof(NODE));
        pNew->data = pTemp1->data;
        pTail->pNext = pNew;
        pNew->pNext = NULL;
        pTail = pNew;
        
   
        while (NULL != pTemp2)
        {
            printf("%d-------2--------------\n", pTemp2->data);
            if (pTemp2->data >= pTemp1->data && pTemp2->data <= p->data)
            {
                //保存到c中的
                pNew = (PNODE)malloc(sizeof(NODE));
                pNew->data = pTemp2->data;
                pTail->pNext = pNew;
                pNew->pNext = NULL;
                pTail = pNew;
            }
            pTemp2 = pTemp2->pNext;
        }
        pTemp2 = q = q->pNext;
        pTemp1 = pTemp1->pNext;
    }
   
    return pHead;
}

PNODE reverse_copy_list(PNODE &pHead)    //逆置链表
{
    PNODE p, q;
   
    p = pHead;
    pHead = NULL;
    while (NULL != p)
    {
        q = p;
        p = p->pNext;
        q->pNext = pHead;
        pHead = q;
    }
   
    return pHead;
}

int main()
{
    int A[5] = {1, 2, 3, 5, 6}, B[5] = {2, 5, 7, 8, 9};
    PNODE pHead = NULL, pHeadA = NULL, pHeadB = NULL;

    pHeadA = create(A);
    pHeadB = create(B);
   
    pHead = coalition(pHeadA, pHeadB);
    pHead = reverse_copy_list(pHead);//逆序
    printf("改变后的链表 : \n");
    traverse(pHead);
        return 0;
}

每次运行到coalition();就会终止, 是为什么,怎样修改呀?
搜索更多相关主题的帖子: include create 元素 
2016-03-21 22:39
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
得分:9 
算法有问题
程序代码:
PNODE coalition(PNODE A, PNODE B) // A 和 B 的交集
{
    PNODE pA, pB, pHead, pTmp, pNew;

    pHead = (PNODE) malloc(sizeof(NODE));
    pHead->pNext = NULL;
    pTmp = pHead;

    pA = A->pNext;
    pB = B->pNext;

    while (pA && pB)
    {
        // 申请空间存放数据
        pNew = (PNODE) malloc(sizeof(NODE));
        if (pA->data < pB->data)
        {
            pNew->data = pA->data;
            pA = pA->pNext;
        }
        else if (pA->data > pB->data)
        {
            pNew->data = pB->data;
            pB = pB->pNext;
        }
        else
        {
            pNew->data = pA->data;
            pA = pA->pNext;
            pB = pB->pNext;
        }
        pNew->pNext = NULL;

        // 添加到链表
        pTmp->pNext = pNew;
        pTmp = pNew;
    }
    while (pA)
    {
        pNew = (PNODE) malloc(sizeof(NODE));
        pNew->data = pA->data;
        pNew->pNext = NULL;

        pTmp->pNext = pNew;
        pTmp = pNew;

        pA = pA->pNext;
    }
    while (pB)
    {
        pNew = (PNODE) malloc(sizeof(NODE));
        pNew->data = pB->data;
        pNew->pNext = NULL;

        pTmp->pNext = pNew;
        pTmp = pNew;

        pB = pB->pNext;
    }

    return pHead;
}



[fly]存在即是合理[/fly]
2016-03-22 09:39
ldsh304
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:7
帖 子:242
专家分:755
注 册:2016-1-18
得分:0 
谢谢了, 请问下我那个算法错在哪?
2016-03-22 18:06



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-462756-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.261778 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved