单链表的C++实现

以下是数据结构中单链表的C++实现,如有需要,请访问我的Github获取完整源码。

//Node类的定义
class Node
{
public:
    int data;
    Node *next;              //指向下一个元素
    void printNode();

    //没有构造函数,使用默认构造函数
};

//Node类成员函数的实现
#include"Node.h"
#include <iostream>
using namespace std;

void Node::printNode()
{
    cout << data <<endl;
}

//List类的定义
#include"Node.h"

class List
{
public:
    List();                                    //构造函数
    ~List();                                   //析构函数
    void ClearList();                          //清空链表
    bool ListEmpty();                          //链表判空
    int ListLength();                          //链表长度
    bool GetElem(int i, Node *pNode);          //获取第i个元素,存入结点pNode
    int LocateElem(Node *pNode);               //获取数据域和结点pNode相等的结点
    bool PriorElem(Node *currentNode, Node *preNode); //前驱
    bool NextElem(Node *currentNode, Node *nextNode); //后继
    void ListTraverse();                              //遍历链表
    bool ListInsertHead(Node *pNode);                 //插入元素到头结点之后
    bool ListInsertTail(Node *pNode);                 //插入元素到尾节点之后
    bool ListInsert(int i, Node *pNode);              //插入元素到第i个位置
    bool ListDelete(int i, Node *pNode);              //删除第i个位置的元素

private:
    Node *m_pList;
    int m_iLength;
};

//List类中成员函数的实现
#include"List.h"
#include <iostream>
using namespace std;
//#include"Node.h"

/************************************************************************/
/*List类各成员函数的实现*/

List::List()
{
    //申请一个空间存放结点
    m_pList = new Node;                 
    m_pList->data = 0;
    m_pList->next = NULL;
    m_iLength = 0;
}

//清空链表
void List::ClearList()
{
    //将当前结点指向头结点的next
    Node *currentNode = m_pList->next;
    //如果当前结点不为空,就删除,直到当前结点为空
    while(currentNode != NULL)
    {
        Node *temp = currentNode->next;
        delete currentNode;
        currentNode = temp;
    }
    //将头结点的next置为空
    m_pList->next = NULL; 
}

List::~List()
{
    //先清空链表,只剩下头结点
    ClearList();
    //删除头结点,释放内存
    delete m_pList;
    m_pList = NULL;
}


//判断链表是否为空:只需判断链表的当前长度m_iLength是否为0
bool List::ListEmpty()
{
    if(m_iLength == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//获取链表的当前长度:直接返回当前长度m_iLength
int List::ListLength()
{
    return m_iLength;
}

//插入结点到头结点之后
bool List::ListInsertHead(Node *pNode)
{
    Node *temp = m_pList->next;
    Node *newNode = new Node;
    if(newNode == NULL)
    {
        return false;
    }
    //先处理数据域
    newNode->data = pNode->data;
    //再处理前面的结点
    m_pList->next = newNode;
    //最后处理自己的指针
    newNode->next = temp;
    //链表长度增加1
    m_iLength++;
    return true;
}

//插入结点到尾节点之后
bool List::ListInsertTail(Node *pNode)
{
    //遍历链表找到最后一个结点
    Node *currentNode = m_pList;
    while(currentNode->next != NULL)
    {
        currentNode = currentNode->next;
    }
    //为插入的结点申请空间
    Node *newNode = new Node;
    if(newNode == NULL)
    {
        return false;
    }
    //先处理数据域
    newNode->data = pNode->data;
    //再处理自己的指针
    newNode->next = NULL;
    //最后处理前面的结点
    currentNode->next = newNode;
    //链表长度增加1
    m_iLength++;
    return true;
}

//在第i个位置插入结点
bool List::ListInsert(int i, Node *pNode)
{
    if(i < 0 || i > m_iLength)
    {
        return false;
    }
    Node *newNode = new Node;
    if(newNode == NULL)
    {
        return false;
    }
    //遍历找到第i个结点
    Node *currentNode = m_pList;
    for(int k = 0; k < i; k++)
    {
        currentNode = currentNode->next;
    }

    //先处理数据域
    newNode->data = pNode->data;
    //再处理自己的指针
    newNode->next = currentNode->next;
    //最后处理前面的结点
    currentNode->next = newNode;
    //链表长度增加1
    m_iLength++;
    return true;
}

bool List::ListDelete(int i,Node *pNode)
{
    if(i < 0 || i >= m_iLength)
    {
        return false;
    }
    //遍历找到第i个结点和前一个结点
    Node *currentNode = m_pList;
    Node *currentNodeBefore = NULL;
    for(int k = 0; k <= i; k++)
    {
        currentNodeBefore = currentNode;
        currentNode = currentNode->next;
    } 

    //先处理数据域
    pNode->data = currentNode->data;
    //再处理自己的指针删除
    currentNodeBefore->next = currentNode->next;
    //删除结点
    delete currentNode;
    currentNode = NULL;
    //链表长度减1
    m_iLength--;
    return true;
}

//获取第i个结点的值复制给pNode
bool List::GetElem(int i, Node *pNode)
{
    if(i < 0 || i >= m_iLength)
    {
        return false;
    }
    //遍历找到第i个结点
    Node *currentNode = m_pList;
    //Node *currentNodeBefore = NULL;
    for(int k = 0; k <= i; k++)
    {
        //currentNodeBefore = currentNode;
        currentNode = currentNode->next;
    } 
    pNode->data = currentNode->data;
    return true;
}

//返回第一个值等于pNode的值的位置
//在链表中,头结点不是位置为0的结点,头结点的下一个结点才是
int List::LocateElem(Node *pNode)
{
    //找到头结点
    Node *currentNode = m_pList;
    //保存位置的变量
    int count = 0;
    //遍历链表找到值
    while(currentNode->next != NULL)
    {
        currentNode = currentNode->next;
        if(currentNode->data == pNode->data)
        {
            return count;
        }
        count++;
    }
    //若未找到,返回-1
    return -1;
}

//寻找currentNode结点的前驱
bool List::PriorElem(Node *currentNode, Node *preNode)
{
    //获取头结点
    Node *currentNodeNew = m_pList;
    //定义当前结点的前一个结点
    Node *currentNodeBefore = NULL;

    //遍历链表
    while(currentNodeNew->next != NULL)
    {
        currentNodeBefore = currentNodeNew;
        currentNodeNew = currentNodeNew->next;
        //若找到当前结点currentNode
        if(currentNode->data == currentNodeNew->data)
        {
            //若当前结点的前驱为头结点,返回false
            if(currentNodeBefore == m_pList)
            {
                return false;
            }
            //将前驱的值给preNode的数据域
            preNode->data = currentNodeBefore->data;
            return true;
        }
    }
    return false;
}

//寻找currentNode的后继
bool List::NextElem(Node *currentNode, Node *nextNode)
{
    //获取头结点
    Node *currentNodeNew = m_pList;
    //遍历链表
    while(currentNodeNew->next != NULL)
    {
        currentNodeNew = currentNodeNew->next;
        if(currentNodeNew->data == currentNode->data)
        {
            if(currentNodeNew->next == NULL)
            {
                return false;
            }
            //将找到的结点的next的值赋值给nextNode的数据域
            nextNode->data = currentNodeNew->next->data;
            return true;
        }
    }
    return false;
}

//遍历链表
void List::ListTraverse()
{
    Node *currentNode = m_pList;
    while(currentNode->next != NULL)
    {
        currentNode = currentNode->next;
        currentNode->printNode();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值