关于链表添加值的问题?

以下是建立的链表,在没有进行添加值操作之前是可以正常打印链表值的,但是在进行了添加
操作后,链表就无法正常打印了。
代码如下:

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

typedef struct MyNode{
    int id;
    struct MyNode* pFront; 
    struct MyNode* pNext; 
}LISTNODE;
int main(){
    LISTNODE* pHead = NULL;
    LISTNODE* pTemp = NULL;

    for (int i = 0; i < 10; i++){
        pTemp = (LISTNODE*)malloc(sizeof(LISTNODE));
        pTemp->id = (i + 1) * 10;
        pTemp->pNext = pHead;
        if (pHead)
            pHead->pFront = pTemp;
        pTemp->pFront = NULL;
        pHead = pTemp;
    }

    int newVal = 112;
    LISTNODE* pNew = NULL;
    pNew->id = newVal;
    pNew->pNext = NULL;
    pNew->pFront = NULL;

    if (pHead){
        if (pNew->id > pHead->id){
            pNew->pNext = pHead;
            pHead->pFront = pNew;
            pHead = pNew;
        }
        else{
            LISTNODE* P = pHead;
            while (P->pNext){
                if (pNew->id > P->pNext->id){
                    pNew->pNext = P->pNext;
                    pNew->pFront = P;
                    pNew->pNext->pFront = pNew;
                    pNew->pFront->pNext = pNew;
                    break;
                }
                P = P->pNext;
            }
            P->pNext = pNew;
            pNew->pFront = P;
        }
    }
    else{
        pHead = pNew;
    }

    pTemp = pHead;
    while (pTemp){
        printf("%d\n", pTemp->id);
        if (pTemp->pNext == NULL)
            break;
        pTemp = pTemp->pNext;
    }
    while (pTemp){
        printf("%d\n", pTemp->id);
        pTemp = pTemp->pFront;
    }

    while (pHead){
        pTemp = pHead;
        pHead = pHead->pNext;
        free(pHead);
    }
    pHead = pTemp = NULL;
    return 0;
}

图片说明

1个回答


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

typedef struct MyNode{
    int id;
    struct MyNode* pFront; 
    struct MyNode* pNext; 
}LISTNODE;
int main(){
    LISTNODE* pHead = NULL;
    LISTNODE* pTemp = NULL;

    for (int i = 0; i < 10; i++){
        pTemp = (LISTNODE*)malloc(sizeof(LISTNODE));
        pTemp->id = (i + 1) * 10;
        pTemp->pNext = pHead;
        if (pHead)
            pHead->pFront = pTemp;
        pTemp->pFront = NULL;
        pHead = pTemp;
    }

    int newVal = 112;
    LISTNODE* pNew = (LISTNODE*)malloc(sizeof(LISTNODE));
    pNew->id = newVal;
    pNew->pNext = NULL;
    pNew->pFront = NULL;

    if (pHead){
        if (pNew->id > pHead->id){
            pNew->pNext = pHead;
            pHead->pFront = pNew;
            pHead = pNew;
        }
        else{
            LISTNODE* P = pHead;
            while (P->pNext){
                if (pNew->id > P->pNext->id){
                    pNew->pNext = P->pNext;
                    pNew->pFront = P;
                    pNew->pNext->pFront = pNew;
                    pNew->pFront->pNext = pNew;
                    break;
                }
                P = P->pNext;
            }
            P->pNext = pNew;
            pNew->pFront = P;
        }
    }
    else{
        pHead = pNew;
    }

    pTemp = pHead;
    while (pTemp){
        printf("%d\n", pTemp->id);
        if (pTemp->pNext == NULL)
            break;
        pTemp = pTemp->pNext;
    }
    while (pTemp){
        printf("%d\n", pTemp->id);
        pTemp = pTemp->pFront;
    }

    while (pHead){
        pTemp = pHead;
        pHead = pHead->pNext;
        free(pTemp);
    }
    pHead = pTemp = NULL;
    return 0;
}


图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐