- 论坛徽章:
- 0
|
目的并不是为了使用标准的STL库,而是在为了Linux/Embeded系统中动态的存放需要的各种类型数据,所以自己定制一个tiny STL list库,代码如下:
节点:
- #include <stdlib.h>
- #include <stdio.h>
- /* list's node implementation */
- template<class T>
- struct _list_node {
- struct _list_node *prev;
- struct _list_node *next;
- T data;
- };
复制代码
迭代器:
- /* iterator implementation */
- template<class T>
- struct _list_iterator {
- typedef _list_node<T> NODE;
- typedef _list_iterator<T> self;
-
- /* the member node can be used kept down the NODE's address */
- NODE *node;
-
- self& operator = (const self& x)
- {
- if (node == x.node)
- return *this;
- node = x.node;
- node->data = x.node->data;
- node->next = x.node->next;
- /* as for iterator, it's address never be altered or midified */
- return *this;
- }
- bool operator != (const self& x) const
- {
- return node != x.node;
- }
- bool operator == (const self& x) const
- {
- return node == x.node;
- }
-
- self& operator++()
- {
- node = node->next;
- return *this;
- }
-
- self operator++(int)
- {
- self tmp = *this;
- node = node.next;
- return tmp;
- }
- T& operator*() const
- {
- return node->data;
- }
- };
复制代码
链表:
测试:
- typedef AList<char*> strLoader;
- typedef AList<char*>::iterator strIte;
- strLoader x;
- strIte xi;
- typedef AList<int> intLoader;
- typedef AList<int>::iterator intIte;
- intLoader y;
- intIte yi;
- typedef AList<AString> asLoader;
- typedef AList<AString>::iterator asIte;
- asLoader z;
- asIte zi;
- typedef AList<DataDirectHeadend> lHeadend;
- typedef AList<DataDirectHeadend>::iterator iteHeadend;
- lHeadend HeadendLoader;
- iteHeadend ite;
- int
- main(int argc, char** argv)
- {
- DataDirectHeadend headend;
- sprintf(headend.zipcode, "%s", "510010");
- sprintf(headend.headendname, "%s", "GZTV");
- headend.channels = 64;
- for (int j=0; j<5; j++)
- {
- headend.headendid = j;
- HeadendLoader.push_back(headend);
- }
- for (ite = HeadendLoader.begin(); ite != HeadendLoader.end(); ++ite)
- {
- printf("zipcode:%s,headendid:%d,headendname:%s,channels:%d\n",\
- (*ite.node).data.zipcode, \
- (*ite.node).data.headendid, \
- (*ite.node).data.headendname, \
- (*ite.node).data.channels);
- }
- printf("===========headend==============\n");
- char context[][10] = {
- "aaaaaa",
- "bbbbbb",
- "aaaaaa",
- "cccccc",
- "dddddd"
- };
- for (int i=0; i<5; i++)
- {
- x.push_back(context[i]);
- }
- for (xi = x.begin(); xi != x.end(); ++xi)
- {
- printf("strIte:%s\n", (*xi.node).data);
- }
- x.remove("dddddd");
- while (1)
- {
- if (!x.eof())
- printf("pop:%s\n", x.pop_back());
- else {
- printf("Is empty\n");
- break;
- }
- }
- printf("==========char*==============\n");
- for (int k=0; k<3; k++)
- y.push_back(k);
- for (yi = y.begin(); yi != y.end(); ++yi)
- printf("intIte:%d\n", (*yi.node).data);
- y.remove(1);
- while (1)
- {
- if (!y.eof())
- printf("pop:%d\n", y.pop_back());
- else {
- printf("Is empty\n");
- break;
- }
- }
- printf("==========int=============\n");
- char context2[10];
- for (int m=0; m<5; m++)
- {
- sprintf(context2, "%d.mp3", m);
- AString as(context2);
- z.push_back(as);
- }
- for (zi = z.begin(); zi != z.end(); ++zi)
- printf("asIte:%s\n", (*zi.node).data.c_str());
- #if 0
- while (1)
- {
- if (!z.eof())
- printf("pop:%s\n", (z.pop_back()).c_str());
- else {
- printf("Is empty\n");
- break;
- }
- }
- #endif
- exit(0);
- }
复制代码
其他都可以,除了char*类型无法正确匹配.
[ 本帖最后由 SybaseLU 于 2007-8-13 17:18 编辑 ] |
|