当前位置:网站首页>模板链表类学习

模板链表类学习

2020-11-08 01:56:00 初吻给了烟灬

//模板类的函数声明和实现要在一个文件中,分成两个就出错了,我晕了好久。
<myList.h>文件

#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; }; //设置指定下标的数据 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; } //查找指定的数据,返回下标索引 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; } //获得指定下标索引的数据 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; } //获得头节点 template <class T> Node<T>* myList<T>::GetHead() const { return pHead; } //删除指定的数据 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("没有要删除的数据!\n"); } //获得链表中项的个数 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(); } //添加数据到链表中 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>文件

 

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

#define IsSex(sex) (sex=='M'?"男":"女")

#define TES_NAME1 "张三"
#define TES_NAME2 "赵小红"
//学生数据类

class Student
{
private:
    int num;  //编号
    char name[20];//姓名

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;
    }
    
};

 

测试类

int main()
{
    myList<Student> list;    

    Student stu2;    
    stu2.SetNum(1);
    stu2.SetName("李小梅");
    list.Add(stu2);

    Student stu1;
    stu1.SetNum(2);
    stu1.SetName("张三");
    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("*****删除数据*********\n");

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

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

    printf_s("*****查找并修改数据*********\n");
    Student st;
    st.SetName(TES_NAME1);
    int n = list.FindAt(st);
    if (n < 0)
        printf_s("没有找到数据\n");
    else
    {
        printf_s("找到数据,并修改其值\n");
        st = list.GetAt(n);
        st.SetNum(112);
        st.SetName("张三女");
        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());
        }
    }
}

 

 

版权声明
本文为[初吻给了烟灬]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/greenleaf1976/p/13943239.html