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

本文档介绍了如何使用C++实现链表的基本操作,包括链表的创建、连接、输出节点、遍历输出、删除节点、在末尾添加节点、链表中删除操作以及元素倒序输出。同时提供了main.cpp文件作为测试用例,涵盖了创建新链表、单节点链表和空链表的场景。

头文件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 <stdio.h>
#include <stdlib.h>
#include <iostream>
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<stack>
#include<iostream>
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值