1. 程式人生 > >malloc的連結串列,建立連結串列,遍歷連結串列,插入節點,刪除節點

malloc的連結串列,建立連結串列,遍歷連結串列,插入節點,刪除節點

#include <iostream>
#include <malloc.h>
#include <stdlib.h>

using namespace std;
//節點 
typedef struct Node
{
    int date;//資料域 
    Node * pNext;//指標域 
}NODE, * PNODE;

PNODE creat(void);//動態的建立一個連結串列 
bool print(PNODE);//輸出連結串列的資料 
bool insert(PNODE, int, int);//用法:insert(pHead, 2, 3);表示在第二個位置上插入資料3。
bool delet(PNODE, int);//用法:delet(pHead, 2);表示在刪除第二個位置上的資料。
int count(PNODE);//返回連結串列的有效長度 
////程式的入口//// 
int main()
{
    PNODE pHead;
    cout << "測試連結串列的建立:" << endl;
    pHead = creat();
    cout << "測試連結串列的輸出:" << endl;
    if (print(pHead))//測試輸出是否成功 
    {
        cout << "輸出完畢!" << endl;
    }
    else
    {
        cout << "沒有資料!" << endl;
    }
    cout << "測試返回連結串列的長度:" << endl;
    cout << count(pHead) << endl;
    cout << "測試插入連結串列:" << endl;
    if (insert(pHead, 1, 1))
    {
        print(pHead);
    }
    else 
    {
        cout << "插入失敗!" << endl;
    }
    cout << "測試刪除連結串列:" << endl;
    if (delet(pHead, 3))
    {
        print(pHead);
    }
    else 
    {
        cout << "刪除失敗!" << endl;
    }
    system("pause");
    return 0;    
}

PNODE creat(void)
{
    int i, val, len;
    PNODE pHead = (PNODE)malloc(sizeof(NODE));//動態的建立節點 
    if (NULL==pHead)//測試節點是否建立成功 
    {
        cerr << "動態分配記憶體失敗!" << endl;
        system("pause");
        exit(-1);//退出程式,必須包含標頭檔案 stdlib.h 
    }
    PNODE pTail = pHead;//Tail為尾節點 
    pTail->pNext = NULL;
    cout << "請輸入節點的個數:" << endl;
    cout << "length = ";
    cin >> len;
    if (len<=0)
    {
        cout << "連結串列長度不能為負數或零!" << endl;
        system("pause");
        exit(-1);
    }
    for (i=0; i<len; ++i)
    {
        cout << "請輸入節點的資料(整數):" << endl;
        cout << "第" << i+1 << "個節點的資料:" << endl;
        cout << "number = ";
        cin >> val;
        PNODE pNew = (PNODE)malloc(sizeof(NODE));
        if (NULL==pNew)
        {
            cerr << "動態分配記憶體失敗!" << endl;
            system("pause");
            exit(-1);
        }
        pNew->date = val;
        pTail->pNext = pNew;
        pNew->pNext = NULL;
        pTail = pNew;
    }
    return pHead;
}

bool print(PNODE pHead)
{
     bool success = false;
     for (; NULL!=pHead->pNext; pHead=pHead->pNext)
     {
         cout << pHead->pNext->date << " ";
         success = true;
     }
     cout << endl;
     return success;
}

int count(PNODE pHead)
{
    int count = 0;
    while (NULL!=pHead->pNext)
    {
        ++count;
        pHead = pHead->pNext;
    }
    
    return count;
}

bool insert(PNODE pHead, int postion, int val)
{
     int success = false;
     int i = 1;
     if (postion<1 || postion>count(pHead)+1)
     {
         cout << "請輸入有效的位置!" << endl;
         system("pause");
         exit(-1);
     }
     while (i<postion && NULL!=pHead->pNext)
     {
         pHead = pHead->pNext;
         ++i;
     }; 
     PNODE p = NULL;
     p = pHead->pNext;
     PNODE pNew = (PNODE)malloc(sizeof(NODE));
     if (NULL==pNew)
     {
         cerr << "動態分配記憶體失敗!" << endl;
         system("pause");
         exit(-1);
     }
     pNew->date = val;
     pNew->pNext = pHead->pNext;
     pHead->pNext = pNew;
     success = true;
     return success;
}

bool delet(PNODE pHead, int postion)
{
    int success = false;
    int i = 1;
    if (postion<1 || postion>count(pHead))
    {
        cout << "請輸入有效的位置!" << endl;
        system("pause");
        exit(-1);
    }
    while (i<postion && NULL!=pHead->pNext)
    {
        pHead = pHead->pNext;
        ++i;
    }; 
    PNODE p = NULL;
    p = pHead->pNext;
    pHead->pNext = p->pNext;
    free(p);
    success = true;
    return success;
}