链表的基本操作C++实现

头文件ListNode.h

struct ListNode{
     
    int m_nValue;
    ListNode* m_pNext;
};

// 用于声明导入导出函数
// __declspec(dllexport) 声明一个导出函数,一般用于dll中
// __declspec(dllimport) 声明一个导入函数,一般用于使用某个dll的exe中

__declspec(dllexport) ListNode* CreateListNode(int value);
__declspec(dllexport) void ConnectListNodes(ListNode* pCurrent, ListNode* pNext);
__declspec(dllexport) void PrintListNode(ListNode* pNode);
__declspec(dllexport) void PrintList(ListNode* pHead);
__declspec(dllexport) void DestroyList(ListNode* pHead);
__declspec(dllexport) void AddToTail(ListNode** pHead, int value);
__declspec(dllexport) void RemoveNode(ListNode** pHead, int value);

源文件List.cpp

链表的创建,链表的连接,输出当前结点,遍历链表并依次输出,删除链表,在链表末尾添加一个节点,链表中删除操作,链表元素倒序输出。

#include "ListNode.h"
#include 
#include 
#include 
using namespace std;

ListNode* CreateListNode(int value){
     
    ListNode* pNode = new ListNode(); //新建一个链表结点
    pNode -> m_pNext = nullptr;
    pNode -> m_nValue = value;

    return pNode; 
}

void ConnectListNodes(ListNode* pCurrent, ListNode* pNext){
     
    if(pCurrent == nullptr){
     
        cout << "Error to connect two nodes." << endl;
        exit(1);
    }
    pCurrent -> m_pNext = pNext;
}

void PrintListNode(ListNode* pNode){
     
    if(pNode == nullptr){
     
        cout << "The node is nullptr" << endl;
    }
    else{
     
        cout << "The key in node is " << pNode ->m_nValue << endl;

    }
}

void PrintList(ListNode* pHead){
     
    cout << "PrintList starts." << endl;

    ListNode* pNode = pHead;
    while(pNode != nullptr){
     
        cout << pNode->m_nValue << " ";
        pNode = pNode->m_pNext;
    }
    cout << endl;
    cout << "PrintList ends." <<endl;
}

void DestroyList(ListNode* pHead){
     
    ListNode* pNode = pHead;
    while(pNode != nullptr){
     
        pHead = pHead ->m_pNext;
        delete pNode;
        pNode = pHead;
    }
}

void AddToTail(ListNode** pHead, int value){
     
    ListNode* pNew = new ListNode();
    pNew ->m_nValue = value;
    pNew -> m_pNext = nullptr;

    if(*pHead == nullptr){
     
        *pHead = pNew;
    }
    else{
     
        ListNode* pNode = *pHead;
        while(pNode -> m_pNext != nullptr)
            pNode = pNode -> m_pNext ;
        pNode -> m_pNext = pNew;
    }
}

void RemoveNode(ListNode** pHead, int value){
     
    if(pHead == nullptr || *pHead == nullptr)
        return;
    
    ListNode* pToBeDeleted = nullptr;
    if((*pHead)-> m_nValue == value){
     
        pToBeDeleted = *pHead;
        *pHead = (*pHead) -> m_pNext;
    }
    else{
     
        ListNode* pNode = *pHead;
        while(pNode -> m_pNext != nullptr && pNode -> m_pNext -> m_nValue != value)
            pNode = pNode -> m_pNext;
        
        if(pNode -> m_pNext != nullptr && pNode -> m_pNext ->m_nValue == value){
     
            pToBeDeleted = pNode ->m_pNext;
            pNode -> m_pNext = pNode -> m_pNext -> m_pNext;
        }
    }
    if(pToBeDeleted != nullptr){
     
        delete pToBeDeleted;
        pToBeDeleted = nullptr;
    }
    
}

void PrintListReversingly_Iteratively(ListNode* pHead){
     
    std::stack<ListNode*> nodes;

    ListNode* pNode = pHead;
    while(pNode != nullptr){
     
        nodes.push(pNode);
        pNode = pNode ->m_pNext;
    }

    while(!nodes.empty()){
     
        pNode = nodes.top();
        cout << pNode->m_nValue << " ";
        nodes.pop();
    }
}

void PrintListReversingly_Recursively(ListNode* pHead){
     
    if(pHead != nullptr){
     
        if(pHead ->m_pNext != nullptr){
     
            PrintListReversingly_Recursively(pHead->m_pNext);
        }
        cout << pHead->m_nValue << " ";
    }
}

main.cpp文件

测试用例:创建新链表,只有一个结点的链表以及空链表。

#include "List.cpp"
#include
#include
using namespace std;
// ==========测试代码==========
void Test(ListNode* pHead){
     
    PrintList(pHead);
    PrintListReversingly_Iteratively(pHead);
    cout << endl;
    PrintListReversingly_Recursively(pHead);
    cout << endl;
}

void Test1(){
     
    cout << "Test1 begins." <<endl;

    ListNode* pNode1 = CreateListNode(65);
    ListNode* pNode2 = CreateListNode(23);
    ListNode* pNode3 = CreateListNode(18);
    ListNode* pNode4 = CreateListNode(45);
    ListNode* pNode5 = CreateListNode(89);

    ConnectListNodes(pNode1,pNode2);
    ConnectListNodes(pNode2,pNode3);
    ConnectListNodes(pNode3,pNode4);
    ConnectListNodes(pNode4,pNode5);

    Test(pNode1);

    DestroyList(pNode1);
}

//只有一个结点的链表:1
void Test2(){
     
    cout << "Test2 begins." <<endl;
    ListNode* pNode1 = CreateListNode(1);
    
    Test(pNode1);
    
    DestroyList(pNode1);
}

//空链表
void Test3(){
     
    cout << "Test3 begins."<<endl;

    Test(nullptr);
}

int main(int argc, char* argv[]){
     
    Test1();
    Test2();
    Test3();

    return 0;
}

你可能感兴趣的:(刷题宝典,链表,单链表,c++)