zoukankan      html  css  js  c++  java
  • 模板链表类学习

    //模板类的函数声明和实现要在一个文件中,分成两个就出错了,我晕了好久。
    <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("没有要删除的数据! "); } //获得链表中项的个数 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
    ", list[i].GetNum(), list[i].GetName());
        }
    
        printf_s("*****删除数据*********
    ");
    
        /*Student delStu;
        delStu.SetName(TES_NAME1);
        list.DelAt(delStu);
        showList(list);*/
    
        /*int n = list.FindAt(delStu);
        ShowNode(list[n]);*/
    
        printf_s("*****查找并修改数据*********
    ");
        Student st;
        st.SetName(TES_NAME1);
        int n = list.FindAt(st);
        if (n < 0)
            printf_s("没有找到数据
    ");
        else
        {
            printf_s("找到数据,并修改其值
    ");
            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
    ", list[i].GetNum(), list[i].GetName());
            }
        }
    }

     

    签名:GreenLeaf1976
  • 相关阅读:
    需求调研-用户访谈
    kettle监控销售人员当月每天任务完成率_20161107周一
    MySQL学习_计算用户支付方式占比_20161104
    MySQL 数据底部出现总计字样 第二种办法 纵向合并 20161103
    MySQL 单笔订单满6个及以上产品且金额>=300赠送优惠券_20161103
    MySQL学习_查看各仓库产品的销售情况_20161102
    MySQL与EXCEL sum sumif sumifs 函数结合_品牌汇总_20161101
    MySQL_活动期间单笔订单最高的且满600元 判别是重激活客户还是10月注册客户_20161031
    MySQL活动期间订单满600元并且在活动日期之前超过30天没有下过单_20161030
    MySQL活动期间制定月份注册用户下单情况_20161029
  • 原文地址:https://www.cnblogs.com/greenleaf1976/p/13943239.html
Copyright © 2011-2022 走看看