读书人

关于一个已排序成功的链表的内容处理有

发布时间: 2012-04-07 17:31:52 作者: rapoo

关于一个已排序成功的链表的内容处理问题

C/C++ code
//多项式指数排序void Rank (Count * pHead){    struct Count * p = pHead->pNext;    struct Count * temp;    while (p != NULL)    {        while (p->pNext != NULL)        {            if (pHead->pNext->Last > p->pNext->Last)            {                temp = pHead->pNext;                pHead->pNext = p->pNext;                p->pNext = pHead->pNext->pNext;                pHead->pNext->pNext = temp;            }            else                p = p->pNext;        }        pHead = pHead->pNext;        p = pHead->pNext;    }    return;}


以下函数目的为了将一个按多项式(已按指数大小排好序)合并同类项。last为指数,val为系数。
编译是通过了,运行到这函数是就出错了,求指导。谢谢!
C/C++ code
void Handle (Count * pHead){    struct Count * p_First = pHead->pNext;    struct Count * p_After = pHead->pNext->pNext;    while (p_First != NULL)    {        if (p_First->Last == p_After->Last)        {            p_First->Val += p_After->Val;            p_First->pNext = p_After->pNext;            p_After = p_After->pNext;        }        else        {        p_First = p_First->pNext;        p_After = p_After->pNext;        }    }    return;}


[解决办法]
while (p_First != NULL)这里应该判断的是p_After != NULL
[解决办法]
C/C++ code
while (p_First != NULL && p_After != NULL)//这里要一起判断,不然里边用到p_After会出错 

读书人网 >C语言

热点推荐