这个程序链表,在退出之前,如何清理new分配的空间呢

这个程序链表,在退出之前,怎么清理new分配的空间呢?C/C++ code#include conio.h#include stdlib.htype

这个程序链表,在退出之前,怎么清理new分配的空间呢?

C/C++ code
#include <conio.h>#include <stdlib.h>typedef struct tagLIST{    int nNumber;    char szName[10];    tagLIST *pNext;}LIST;LIST *Create(int nCount){    LIST *pHead = NULL;    LIST *pNow = NULL;    LIST *pData = NULL;    int nIndex;        for(nIndex = 0; nIndex < nCount; nIndex++)    {        pData = new LIST;        if(nIndex == 0)        {            printf("输入号数和姓名:");            scanf("%d-%s", &pData->nNumber, pData->szName);            pHead = pData;            pNow = pData;            pData->pNext = NULL;        }        else        {            printf("输入号数和姓名:");            scanf("%d-%s", &pData->nNumber, pData->szName);            pNow->pNext = pData;            pNow = pData;            pNow->pNext = NULL;        }    }    return pHead;}int main(int argc, char* argv[]){    LIST *pMyList = NULL;    pMyList = Create(2);    puts("\n");    while(pMyList)    {        printf("号数:%d 姓名:%s\n", pMyList->nNumber, pMyList->szName);        pMyList = pMyList->pNext;    }    getch();    return 0;}


[解决办法]
C/C++ code
#include <conio.h>#include <stdlib.h>typedef struct tagLIST{    int nNumber;    char szName[10];    tagLIST *pNext;}LIST;LIST *Create(int nCount){    LIST *pHead = NULL;    LIST *pNow = NULL;    LIST *pData = NULL;    int nIndex;        for(nIndex = 0; nIndex < nCount; nIndex++)    {        pData = new LIST;        if(nIndex == 0)        {            printf("输入号数和姓名:");            scanf("%d-%s", &pData->nNumber, pData->szName);            pHead = pData;            pNow = pData;            pData->pNext = NULL;        }        else        {            printf("输入号数和姓名:");            scanf("%d-%s", &pData->nNumber, pData->szName);            pNow->pNext = pData;            pNow = pData;            pNow->pNext = NULL;        }    }    return pHead;}int main(int argc, char* argv[]){    LIST *pMyList = NULL;    LIST *pCurr = NULL;    pMyList = Create(2);    puts("\n");    while(pMyList)    {        printf("号数:%d 姓名:%s\n", pMyList->nNumber, pMyList->szName);        pCurr = pMyList->pNext;        delete pMyList;        pMyList = pCurr;    }    getch();    return 0;}
[解决办法]
你这链表建得有问题
[解决办法]
C/C++ code
void Destory(LIST *pHeader){    LIST *pTemp = NULL;    while (pHeader)    {        pTemp = pHeader;        pHeader = pHeader->pNext;        delete pTemp;    }}
[解决办法]
C/C++ code
 
void Destory(LIST *pHeader)
{
LIST *pTemp = NULL;

while (pHeader)
{
pTemp = pHeader;
pHeader = pHeader->pNext;
delete pTemp;
}
}