1. 程式人生 > >C++連結串列類模板

C++連結串列類模板

記錄自己寫的一個連結串列類模板,兩個標頭檔案:一個是節點標頭檔案Node.h,一個是連結串列標頭檔案LinkList.h。

Node.h

#pragma once

#include <iostream>

template <typename T>
class Node
{
public:
    void printNode();
    T data;
    Node<T>* next;
};

template <typename T>
void Node<T>::printNode()
{
    std::cout
<< data << std::endl; }

由於遍歷函式中直接實現的節點類中的成員函式printNode,在具體的模板類實現中需要在模板類中過載輸出運算子”<<”。

LinkList.h

#pragma once

#include <iostream>
#include "Node.h"

using namespace std;

template <typename T>
class LinkList
{
public:
    LinkList();
    ~LinkList();
    void ClearLinkList();                                           //清空連結串列
    bool LinkListEmpty();                                           //判空
    int LinkListLength();                                           //得到表長
    bool GetElem(int i, Node<T
> *pNode); //得到i位置節點 int LocateElem(Node<T> *pNode); //定位節點 bool PriorElem(Node<T> *pCurrentNode, Node<T> *pPreNode); //獲得指定元素前驅 bool NextElem(Node<T> *pCurrentNode, Node<T> *pNextNode); //獲得指定元素後繼 void ListTraverse(); //遍歷 bool ListInsert(int i, Node<T
> *pNode); //指定位置插入 bool ListDelete(int i, Node<T> *pNode); //指定位置刪除 bool ListInsertHead(Node<T> *pNode); //頭插 bool ListInsertTail(Node<T> *pNode); //尾插 private: Node<T>* m_pList; int m_iLength; }; template <typename T> LinkList<T>::LinkList() { m_pList = new Node<T>; m_pList->next = NULL; m_iLength = 0; } template <typename T> LinkList<T>::~LinkList() { ClearLinkList(); delete m_pList; m_pList = NULL; } template <typename T> void LinkList<T>::ClearLinkList() { Node<T>* currentNode = m_pList->next; while (currentNode != NULL) { Node<T>* temp = currentNode->next; delete currentNode; currentNode = temp; } m_pList->next = NULL; } template <typename T> bool LinkList<T>::LinkListEmpty() { return 0 == m_iLength; } template <typename T> int LinkList<T>::LinkListLength() { return m_iLength; } template <typename T> bool LinkList<T>::ListInsertHead(Node<T> *pNode) { Node* temp = m_pList->next; Node* newNode = new Node<T>; if (newNode == NULL) { return false; } newNode->data = pNode->data; newNode->next = temp; m_pList->next = newNode; m_iLength++; return true; } template <typename T> bool LinkList<T>::ListInsertTail(Node<T> *pNode) { Node<T>* currentNode = m_pList; while (currentNode->next != NULL) { currentNode = currentNode->next; } Node<T>* newNode = new Node<T>; if (newNode == NULL) { return false; } newNode->data = pNode->data; newNode->next = NULL; currentNode->next = newNode; m_iLength++; return true; } template <typename T> bool LinkList<T>::ListInsert(int i, Node<T> *pNode) { if (i < 0 || i > m_iLength) { return false; } Node<T>* currentNode = m_pList; for (int k = 0; k < i; ++k) { currentNode = currentNode->next; } Node<T>* newNode = new Node<T>; if (newNode == NULL) { return false; } newNode->data = pNode->data; newNode->next = currentNode->next; currentNode->next = newNode; m_iLength++; return true; } template <typename T> bool LinkList<T>::ListDelete(int i, Node<T> *pNode) { if (i < 0 || i >= m_iLength) { return false; } Node<T>* currentNode = m_pList; Node<T>* currentNodeBefore = NULL; for (int k = 0; k <= i; ++k) { currentNodeBefore = currentNode; currentNode = currentNode->next; } currentNodeBefore->next = currentNode->next; pNode->data = currentNode->data; delete currentNode; currentNode = NULL; m_iLength--; return true; } template <typename T> bool LinkList<T>::GetElem(int i, Node<T> *pNode) { if (i < 0 || i >= m_iLength) { return false; } Node<T>* currentNode = m_pList; for (int k = 0; k <= i; ++k) { currentNode = currentNode->next; } pNode->data = currentNode->data; return true; } template <typename T> int LinkList<T>::LocateElem(Node<T> *pNode) { int count = 0; Node<T>* currentNode = m_pList; while (currentNode->next != NULL) { currentNode = currentNode->next; if (currentNode->data == pNode->data) return count; else count++; } return -1; } template <typename T> bool LinkList<T>::PriorElem(Node<T> *pCurrentNode, Node<T> *pPreNode) { Node<T>* currentNode = m_pList; Node<T>* tempNode = NULL; while (currentNode->next != 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; } template <typename T> bool LinkList<T>::NextElem(Node<T> *pCurrentNode, Node<T> *pNextNode) { Node<T>* currentNode = m_pList; while (currentNode->next != NULL) { currentNode = currentNode->next; if (currentNode->data == pCurrentNode->data) { if (currentNode->next == NULL) { return false; } pNextNode->data = currentNode->next->data; return true; } } return false; } template <typename T> void LinkList<T>::ListTraverse() { Node<T>* currentNode = m_pList; while (currentNode->next != NULL) { currentNode = currentNode->next; currentNode->printNode(); } }

模板例項

Person.h

#pragma once

#include <string>
#include <iostream>
using namespace std;

class Person
{
    friend ostream &operator<<(ostream &out, Person &person);
public:
    string name;
    string phone;
    Person& operator =(Person &person);
    bool operator == (Person &person);
};

Person.cpp

#include "Person.h"


ostream &operator<<(ostream &out, Person &person)
{
    out << person.name << "----------" << person.phone << endl;
    return out;
}


Person& Person::operator =(Person &person)
{
    this->name = person.name;
    this->phone = person.phone;
    return *this;
}

bool Person::operator == (Person &person)
{
    return (this->name == person.name && this->phone == person.phone);
}