Zero Lee的专栏

One new template implementation of single linked list

One new implementation of single list as below(code: c++):
/**
  * Used to create and manage a single linked list of objects of a common
  * type.  The list of created objects can be examined to find a key by
  * an identifier.
  */
 1
 template <class T, typename K>
 2 class objList {
 3 protected:
 4     static T* objFirst;
 5     T* objNext;
 6     const K objKey;
 7     
 8     objList(const K key) {
 9         objKey = key;
10         objNext = objFirst;
11         objFirst = (T*)this;
12     }
13 public:
14     static T* getObject(const K& key);
15 }; 
16 template <class T, typename K>
17 T* objList<T, K>::objFirst = NULL;
18 
19 template <class T, typename K>
20 objList<T, K>::getObject(const K& key)
21 {
22     T* obj = objList<T, K>::objFirst;
23     while (obj) {
24         if (key==obj->objKey)
25             break;
26         obj = obj->objNext;
27     }
28     return obj;
29 }


posted on 2010-10-17 14:26 Zero Lee 阅读(204) 评论(0)  编辑 收藏 引用 所属分类: CC++ Programming


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理