当前位置:网站首页>Template linked list learning

Template linked list learning

2020-11-08 01:56:00 The first kiss to Yan Yan

// The function declaration and implementation of template class should be in a file , It's a mistake to split into two , I've been dizzy for a long time .
<myList.h> file

#pragma
once #include <stdio.h> template<class T> class Node { public: Node(); T Data; Node *pNext; }; template <class T> class myList { public: myList(); ~myList(); //friend class Node<T>; public: void Add(T &t); T operator[](int index); int GetCount()const; Node<T>* GetHead()const; void DelAt(T &t); int FindAt(T &t); T GetAt(int index)const; void SetAt(int index,T &t); protected: Node<T> *pHead; int nCount; }; // Set the data of the specified subscript template <class T> void myList<T>::SetAt(int index,T &t) { Node<T> *p = pHead; for (int i=0;i<index;i++) { p = p->pNext; } p->Data = t; } // Find the specified data , Returns the subscript index template <class T> int myList<T>::FindAt(T &t) { Node<T> *p = pHead; int i = 0; while (p != NULL) { if (p->Data == t) { return i; } p = p->pNext; i++; } return -1; } // Get the data of the specified index template<class T> inline T myList<T>::GetAt(int index) const { Node<T> *p = pHead; for (int i=0;i<index;i++) { p = p->pNext; } return p->Data; } // Get the head node template <class T> Node<T>* myList<T>::GetHead() const { return pHead; } // Delete the specified data template<class T> inline void myList<T>::DelAt(T & t) { Node<T> *p = pHead; Node<T> *p2 = pHead; while (p != NULL) { if (p->Data == t) { if (p == pHead) pHead = p->pNext; else p2->pNext = p->pNext; nCount -= 1; return; } p2 = p; p = p->pNext; } printf_s(" There is no data to delete !\n"); } // Get the number of items in the linked list template <class T> int myList<T>::GetCount() const { return nCount; } template <class T> T myList<T>::operator[](int index) { Node<T> *p = pHead; if (index >= 0 && index < nCount) { for (int i = 0; i < index; i++) { p = p->pNext; } return p->Data; } return T(); } // Add data to the list template <class T> void myList<T>::Add(T &t) { Node<T> *p = new Node<T>; p->Data = t; p->pNext = NULL; if (pHead == NULL) { pHead = p; pHead->pNext = NULL; } else { p->pNext = pHead; pHead = p; } nCount += 1; } template <class T> myList<T>::~myList() { delete pHead; nCount = 0; } template <class T> myList<T>::myList() { pHead = new Node<T>; nCount = 0; } template<class T> inline Node<T>::Node() { Data = T(); pNext = NULL; }

 

<test.cpp> file

 

#include <windows.h>
#include <stdio.h>
#include "myList.h"

#define IsSex(sex) (sex=='M'?" male ":" Woman ")

#define TES_NAME1 " Zhang San "
#define TES_NAME2 " Zhao Xiaohong "
// Student data class 

class Student
{
private:
    int num;  // Number 
    char name[20];// full name 

public:

    BOOL operator==(const char * sName)
    {
        if (strcmp(name, sName) == 0)
            return TRUE;
        return FALSE;
    }
    void SetNum(int data)
    {
        num = data;
    }
    int GetNum()const
    {
        return num;
    }
    void SetName(const char *pName)
    {
        strcpy_s(name, pName);
    }
    const char* GetName()const
    {
        return name;
    }
    
};

 

Test class

int main()
{
    myList<Student> list;    

    Student stu2;    
    stu2.SetNum(1);
    stu2.SetName(" Li Xiaomei ");
    list.Add(stu2);

    Student stu1;
    stu1.SetNum(2);
    stu1.SetName(" Zhang San ");
    list.Add(stu1);

    for (int i = 0; i < list.GetCount(); i++)
    {
        printf_s("num=%d,name=%s\n", list[i].GetNum(), list[i].GetName());
    }

    printf_s("***** Delete data *********\n");

    /*Student delStu;
    delStu.SetName(TES_NAME1);
    list.DelAt(delStu);
    showList(list);*/

    /*int n = list.FindAt(delStu);
    ShowNode(list[n]);*/

    printf_s("***** Find and modify data *********\n");
    Student st;
    st.SetName(TES_NAME1);
    int n = list.FindAt(st);
    if (n < 0)
        printf_s(" No data found \n");
    else
    {
        printf_s(" Find the data , And modify its value \n");
        st = list.GetAt(n);
        st.SetNum(112);
        st.SetName(" Zhang sannu ");
        list.SetAt(n, st);
    }
        

    showList(list);
    

    system("pause");
    return 0;
}

//ShowList()

void showList(myList<Student> &list)
{
    if (list.GetCount() > 0)
    {
        for (int i = 0; i < list.GetCount(); i++)
        {
            printf_s("num=%d,name=%s\n", list[i].GetNum(), list[i].GetName());
        }
    }
}

 

 

版权声明
本文为[The first kiss to Yan Yan]所创,转载请带上原文链接,感谢