国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++ 使用模板實現一個List的實例

2020-01-26 14:07:33
字體:
來源:轉載
供稿:網友

C ++使用模板寫的一個List

template<class T> class List { private:   struct Node   {     T data;     Node *next;   };   //head   Node *head;   //size   int length;    //process   Node *p;    //temp   Node *q; public:   List()   {     head = NULL;     length = 0;     p = NULL;   }   void add(T t)   {     if(head == NULL)     {       q = new Node();       q->data = t;       q->next = NULL;       length ++ ;       head = q ;       p = head;     }     else     {       q = new Node();       q->data = t;       q->next = NULL;       length ++;       p -> next = q;       p = q;     }   }    void remove(int n)   {     if(n >= length )     {       return;     }     length -- ;      //刪除頭節點     if(n == 0)     {       q = head ;       head = head -> next;       delete(q);     }     else     {       q = head;       for(int i = 0 ; i < n-1 ; i++)       {         q = q -> next;       }       Node *t = q ->next;       q->next = q->next ->next;       delete(t);      }      //     p = head;     if (p != NULL)     {       while(p->next != NULL)       {         p = p->next;       }     }    }    int getSize()   {     return length;   }    int getLength()   {     return getSize();   }    T get(int n)   {     q = head;     for (int i = 0 ;i < n ; i++)     {       q = q->next;     }     return q->data;   }   }; 

調用方式如下

List<Stu>list;   Stu stu1;   Stu stu2;   Stu stu3;   stu1.username = "1";   stu2.username = "2";   stu3.username = "3";    list.add(stu1);   list.remove(0);   list.add(stu2);   list.add(stu3);    for (int i = 0 ;i < list.getSize() ; i ++)   {     cout << list.get(i).username;   } 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 建瓯市| 通道| 平阳县| 彩票| 涞水县| 新竹市| 金沙县| 碌曲县| 晋江市| 石家庄市| 敦煌市| 上虞市| 郸城县| 合江县| 应城市| 定边县| 姚安县| 红桥区| 盐池县| 宜章县| 噶尔县| 台中县| 上栗县| 临沭县| 枞阳县| 巴青县| 岚皋县| 比如县| 炎陵县| 内黄县| 河南省| 盘锦市| 廊坊市| 奉化市| 浏阳市| 科技| 马公市| 宁明县| 海伦市| 巴东县| 洱源县|