C++实现线性表之单链表

C++实现线性表之单链表

Node.h

#ifndef NODE_H
#define NODE_H
#include <iostream>
using namespace std;

class Node
{
public:
    int data;
    Node *next;
    void printNode();

private:

};

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


#endif // !NODE_H

List.h

#ifndef LIST_H
#define LIST_H
#include <iostream> 
#include "Node.h"

using namespace std;

class List
{
public:
    List();                                     //创建线性表
    ~List();                                           //销毁线性表
    void ClearList();                                   //清空线性表
    bool ListEmpty();                                   //判断线性表是否为空
    int ListLength();                                   //获取线性表的长度
    bool GetElem(int n, Node *pNode);                       //获取指定元素
    int LocateElem(Node *pNode);                                //寻找第一个满足e的数据元素的位序
    bool PriorElem(Node *pCurrentNode, Node *pPreNode);     //获取指定元素的前驱
    bool NextElem(Node *pCurrentNode, Node *pNextNode);     //获取指定元素的后继
    bool ListInsert(int n, Node *pNode);                        //在第n个位置上插入元素
    bool ListDelete(int n, Node *pNode);                        //删除第n个位置上的元素
    bool ListInsertHead(Node *pNode);
    bool ListInsertTail(Node *pNode);
    void ListTraverse();                                //便利线性表

private:
    Node *m_pList;
    int m_iLength;
};

List::List()
{
    m_pList = new Node;
    m_pList->data = 0;
    m_pList->next = NULL;
    m_iLength = 0;
}

List::~List()
{
    ClearList();
    delete m_pList;
    m_pList = NULL;
}

void List::ClearList()
{
    Node *currentNode = m_pList->next;
    while (currentNode != NULL)
    {
        Node *temp = currentNode->next;
        delete currentNode;
        currentNode = temp;
    }
    m_pList->next = NULL;
}

bool List::ListEmpty()
{
    if (m_iLength == 0)
    {
        return true;
    }
    else
    {
        return false;
    }

    //return m_iLength == 0 ? true : false;
}

int List::ListLength()
{
    return m_iLength;
}

bool List::GetElem(int n, Node *pNode)
{
    if (n < 0 || n >= m_iLength)
    {
        return false;
    }
    else
    {
        Node *currentNode = m_pList;
        for (int i = 0; i <= n; i++)
        {
            currentNode = currentNode->next;
        }
        pNode->data = currentNode->data;

        return true;
    }
}

int List::LocateElem(Node *pNode)
{
    Node *currentNode = m_pList;
    int count = 0;
    while (currentNode != NULL)
    {
        currentNode = currentNode->next;

        if (currentNode->data == pNode->data)
        {
            return count;
        }

        count++;
    }
    return -1;
}

bool List::PriorElem(Node *pCurrentNode, Node *pPreNode)
{
    Node *currentNode = m_pList;
    Node *tempNode = NULL;
    while (currentNode != NULL)
    {
        tempNode = currentNode;
        currentNode = currentNode->next;

        if (currentNode->data == pCurrentNode->data)
        {
            if (tempNode == m_pList)
            {
                return false;
            }
            pPreNode->data = tempNode->data;
            return true;
        }
    }
    return false;
}

bool List::NextElem(Node *pCurrentNode, Node *pNextNode)
{
    Node *currentNode = m_pList;
    Node *tempNode = NULL;
    while (currentNode != NULL)
    {
        currentNode = currentNode->next;

        if (currentNode->data == pCurrentNode->data)
        {
            if (currentNode->next == NULL)
            {
                return false;
            }
            pNextNode->data = currentNode->next->data;
            return true;
        }
    }
    return false;
}

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

bool List::ListInsert(int n, Node *pNode)
{
    if (n < 0 || n > m_iLength)
    {
        return false;
    }
    else
    {
        Node *currentNode = m_pList;
        for (int i = 0; i < n ; i++)
        {
            currentNode = currentNode->next;
        }

        Node *newNode = new Node;
        if (newNode == NULL)
        {
            return false;
        }

        newNode->data = pNode->data;
        newNode->next = currentNode->next;
        currentNode->next = newNode;

        m_iLength++;
        return true;
    }

}

bool List::ListDelete(int n, Node *pNode)
{
    if (n < 0 || n >= m_iLength)
    {
        return false;
    }
    else
    {
        Node *currentNode = m_pList;
        Node *currentNodeBefore = NULL;
        for (int i = 0; i <= n; i++)
        {
            currentNodeBefore = currentNode;
            currentNode = currentNode->next;
        }
        currentNodeBefore->next = currentNode->next;
        pNode->data = currentNode->data;
        currentNode = NULL;

        m_iLength--;
        return true;
    }
}

bool List::ListInsertHead(Node *pNode)
{
    Node *temp = m_pList->next;
    Node *newNode = new Node;
    if (newNode == NULL)
    {
        return false;
    }
    newNode->data = pNode->data;
    newNode->next = temp; 
    m_pList->next = newNode;

    m_iLength++;

    return true;
}

bool List::ListInsertTail(Node *pNode)
{
    Node *currentNode = m_pList;
    Node *tempNode = NULL;
    while (currentNode != NULL)
    {
        tempNode = currentNode;
        currentNode = currentNode->next;
    }
    Node *newNode = new Node;
    if (newNode == NULL)
    {
        return false;
    }
    newNode->data = pNode->data;
    newNode->next = NULL;
    tempNode->next = newNode;


    m_iLength++;
    return true;
}

#endif // !LIST_H

main.cpp

#include "List.h"
#include "Node.h"


int main()
{
    List *pList = new List();
    Node e1, e2, e3, e4, e5, e6, e7, e8;
    e1.data = 1;
    e2.data = 2;
    e3.data = 3;
    e4.data = 4;
    e5.data = 5;
    e6.data = 6;
    e7.data = 7;
    pList->ListInsertHead(&e1);
    pList->ListInsertHead(&e2);
    pList->ListInsertHead(&e3);
    pList->ListInsertHead(&e4);
    pList->ListInsertHead(&e5);
    pList->ListInsertHead(&e6);
    pList->ListInsertTail(&e1);
    pList->ListInsertTail(&e2);
    pList->ListInsertTail(&e3);
    pList->ListInsertTail(&e4);
    pList->ListInsertTail(&e5);
    pList->ListInsertTail(&e6);

    pList->ListInsert(6, &e7);
    //pList->ListDelete(6, &e8);
    pList->NextElem(&e4, &e8);

    pList->ListTraverse();

    cout << "& " << pList->ListLength() << " " << e8.data << endl;
    delete pList;
    pList = NULL;

    system("pause");
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值