数据结构c++实现代码-链表


/*
节点Node的相关声明与定义*/ //Node.h #include<iostream> using namespace std; template<typename T> class Node { public: Node(); Node(T data); ~Node(); void setData(T data); T getData(); void setNext(Node<T>*next); Node* getNext(); void printData(); private: T *m_tpData; Node<T> *m_tpNext; }; template<typename T> Node<T>::Node() { m_tpData = new T; m_tpNext = NULL; } template<typename T> Node<T>::~Node() { delete m_tpData; m_tpNext = NULL; } template<typename T> void Node<T>::setData(T data) { *m_tpData = data; } template<typename T> T Node<T>::getData() { return *m_tpData; } template<typename T> void Node<T>::setNext(Node<T> *next) { m_tpNext = next; } template<typename T> Node<T>*Node<T>::getNext() { return m_tpNext; } template<typename T> void Node<T>::printData() { cout << *m_tpData << endl; }
/*链表中相关功能的函数的定义与实现*/
//LinkList.h
#include<iostream>
#include "Node.h"
using namespace std;

template<typename T>
class LinkList
{
public:
    LinkList();
    ~LinkList();
    bool isListEmpty();
    bool clearList();
    int getListLength();
    int getElemIndex(T &elem);
    bool  getListElem(int index, T* elem);
    bool ListInsert(int index, T &elem);
    bool ListDelete(int index, T *elem);
    void ListPrint(void);
private:
    Node<T> *m_pList;
    int m_iLength;
};
template<typename T>
LinkList<T>::LinkList()
{
    m_pList = new Node<T>;
    m_pList->setData(NULL);
    m_pList->setNext(NULL);
    m_iLength = 0;
}
template<typename T>
LinkList<T>::~LinkList()
{
    Node<T>*nextNode = m_pList;
    while (nextNode->getNext() != NULL)
    {
        nextNode = m_pList->getNext();
        delete m_pList;
        m_pList = nextNode;
    }
    delete m_pList;//delete last Node
    m_pList = NULL;
}
template<typename T>
bool LinkList<T>::isListEmpty()
{
    if (m_iLength == 0)
    {
        return true;
    }
    return false;
}
template<typename T>
bool LinkList<T>::clearList()
{
    if (isListEmpty())
    {
        cout << "List empty clear fail" << endl;
        return false;
    }
    Node<T>*nowNode = m_pList->getNext();
    Node<T>*nextNode = m_pList->getNext();
    while (nextNode->getNext() != NULL)
    {
        nextNode = nowNode->getNext();
        delete nowNode;
        nowNode = nextNode;
    }
    delete nowNode;
    m_iLength = 0;
    m_pList->setNext(NULL);
    return true;
}
template <typename T>
int LinkList<T>::getListLength()
{
    return m_iLength;
}
template <typename T>
int LinkList<T>::getElemIndex(T &elem)
{
    Node<T> *tempNode = m_pList;
    for (int i = 0; i < m_iLength; i++)
    {
        tempNode = tempNode->getNext();
        if (elem == tempNode->getData())
        {
            return i;
        }
    }
    return -1;
}
template<typename T>
bool LinkList<T>::getListElem(int index, T *elem)
{
    if (index < 0 || index >= m_iLength)
    {
        return false;
    }
    Node<T>*tempNode = m_pList;
    for (int i = 0; i <= index; i++)
    {
        tempNode = tempNode->getNext();
    }
    *elem = tempNode->getData();
    return true;
}

template<typename T>
bool LinkList<T>::ListInsert(int index, T &elem)
{
    if (index<0 || index>m_iLength)
    {
        return false;
    }
    Node<T>*tempPreNode = m_pList;
    for (int i = 0; i < index; i++)
    {
        tempPreNode = tempPreNode->getNext();
    }
    Node<T>*newnode = new Node<T>;
    if (newnode == NULL)
    {
        cout << "new node create fail" << endl;
        return false;
    }
    Node<T>*tempNode = tempPreNode->getNext();
    tempPreNode->setNext(newnode);
    newnode->setNext(tempNode);
    newnode->setData(elem);
    m_iLength++;
    return true;
}
template<typename T>
bool LinkList<T>::ListDelete(int index, T *elem)
{
    if (index < 0 || index >= m_iLength)
    {
        return false;
    }
    Node<T>* tempPreNode = m_pList;
    for (int i = 0; i < index; i++)
    {
        tempPreNode = tempPreNode->getNext();
    }
    Node<T>*tempNode = tempPreNode->getNext();
    tempPreNode->setNext(tempNode->getNext());
    *elem = tempNode->getData();
    delete tempNode;
    m_iLength--;
    return true;
}
template<typename T>
void LinkList<T>::ListPrint(void)
{
    if (isListEmpty())
    {
        cout << "List empty" << endl;
        return;
    }
    Node<T>*tempNode = m_pList->getNext();
    while (tempNode->getNext() != NULL)
    {
        tempNode->printData();
        tempNode = tempNode->getNext();
    }
    tempNode->printData();
    cout << "end" << endl;
}
//主程序main.cpp//
#include<iostream>
#include<string>
#include"LinkList.h"
using namespace std;

int main(void)
{
    /*insert data check */
    int data[10] = { 0,1,2,3,4,5,6,7,8,9 };
    LinkList<int>*linklist = new LinkList<int>;
    for (int i = 0; i < 5; i++)
    {
        linklist->ListInsert(i, data[i]);
    }
    linklist->ListPrint();
    /*getElemIndex check*/
    cout << "getElemIndex:" << linklist->getElemIndex(data[4]) << endl;
    /*getListElem check*/
    int getdata;
    linklist->getListElem(2, &getdata);
    cout << "getListElem:" << getdata << endl;
    /*delete data check*/
    int deletedata;
    linklist->ListDelete(0, &deletedata);
    cout << "delete data:" << deletedata << endl;
    linklist->ListPrint();
    /*clearList check*/
    linklist->clearList();
    linklist->ListPrint();

    delete linklist;
    linklist = NULL;
    system("pause");
    return 0;
}

结果图:

 

参考:https://www.cnblogs.com/HongYi-Liang/p/7172345.html?utm_source=itdadao&utm_medium=referral

2018-04-2215:46:49

posted @ 2018-04-22 15:47  fourmii  阅读(1094)  评论(0编辑  收藏  举报