1 2 3 4 5 6 7 8 9 10 11 12 13 | struct Struct_Object_A { int a; int b; Struct_Object_A *next; } OBJECT_A; typedef struct Struct_Object_B { int a; int b; int c; Struct_Object_B *next; } OBJECT_B; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | typedef struct liststruct { liststruct *next; } LIST, *pLIST; pLIST Head = NULL; pLIST AddToList( pLIST Head, void * data, size_t datasize ) { pLIST newlist=NULL; void *p; // 分配节点内存和数据内存 newlist = (pLIST) malloc( datasize + sizeof( LIST ) ); // 为这块数据缓冲区指定一个指针 p = (void *)( newlist + 1 ); // 复制数据 memcpy( p, data, datasize ); // 将这个节点指定给链表的表头 if( Head ) { newlist->next = Head; } else newlist->next = NULL; Head = newlist; return Head; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | typedef void (*ListNodeDestructor)( void * ); typedef struct liststruct { ListNodeDestructor DestructFunc; liststruct *next; } LIST, *pLIST; pLIST AddToList( pLIST Head, void * data, size_t datasize, ListNodeDestructor Destructor ) { pLIST newlist=NULL; void *p; // 分配节点内存和数据内存 newlist = (pLIST) malloc( datasize + sizeof( LIST ) ); // 为这块数据缓冲区指定一个指针 p = (void *)( newlist + 1 ); // 复制数据 memcpy( p, data, datasize ); newlist->DestructFunc = Destructor; // 将这个节点指定给链表的表头 if( Head ) { newlist->next = Head; } else newlist->next = NULL; Head = newlist; return Head; } void DeleteList( pLIST Head ) { pLIST Next; while( Head ) { Next = Head->next; Head->DestructFunc( (void *) Head ); free( Head ); Head = Next; } } typedef struct ListDataStruct { LPSTR p; } LIST_DATA, *pLIST_DATA; void ListDataDestructor( void *p ) { // 对节点指针进行类型转换 pLIST pl = (pLIST)p; // 对数据指针进行类型转换 pLIST_DATA pLD = (pLIST_DATA) ( pl + 1 ); delete pLD->p; } pLIST Head = NULL; void TestList() { pLIST_DATA d = new LIST_DATA; d->p = new char[24]; strcpy( d->p, "Hello" ); Head = AddToList( Head, (void *) d, sizeof( pLIST_DATA ), ListDataDestructor ); // 该对象已被复制,现在删除原来的对象 delete d; d = new LIST_DATA; d->p = new char[24]; strcpy( d->p, "World" ); Head = AddToList( Head, (void *) d, sizeof( pLIST_DATA ), ListDataDestructor ); delete d; // 释放链表 DeleteList( Head ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | // 定义解除函数指针 typedef void (*ListNodeDestructor)( void * ); // 未添加解除函数指针的链表 typedef struct ndliststruct { ndliststruct *next; } ND_LIST, *pND_LIST; // 定义处理一种数据类型的链表类 class CList : public ND_LIST { public: CList(ListNodeDestructor); ~CList(); pND_LIST AddToList( void * data, size_t datasize ); void *GetCurrentData(); void DeleteList( pND_LIST Head ); private: pND_LIST m_HeadOfList; pND_LIST m_CurrentNode; ListNodeDestructor m_DestructFunc; }; // 用正确的起始值构造这个链表对象 CList::CList(ListNodeDestructor Destructor) : m_HeadOfList(NULL), m_CurrentNode(NULL) { m_DestructFunc = Destructor; } // 在解除对象以后删除链表 CList::~CList() { DeleteList(m_HeadOfList); } // 向链表中添加一个新节点 pND_LIST CList::AddToList( void * data, size_t datasize ) { pND_LIST newlist=NULL; void *p; // 分配节点内存和数据内存 newlist = (pND_LIST) malloc( datasize + sizeof( ND_LIST ) ); // 为这块数据缓冲区指定一个指针 p = (void *)( newlist + 1 ); // 复制数据 memcpy( p, data, datasize ); // 将这个节点指定给链表的表头 if( m_HeadOfList ) { newlist->next = m_HeadOfList; } else newlist->next = NULL; m_HeadOfList = newlist; return m_HeadOfList; } // 将当前的节点数据作为 void 类型返回,以便调用函数能够将它转换为任何类型 void * CList::GetCurrentData() { return (void *)(m_CurrentNode+1); } // 删除已分配的链表 void CList:eleteList( pND_LIST Head ) { pND_LIST Next; while( Head ) { Next = Head->next; m_DestructFunc( (void *) Head ); free( Head ); Head = Next; } } // 创建一个要在链表中创建和存储的结构 typedef struct ListDataStruct { LPSTR p; } LIST_DATA, *pND_LIST_DATA; // 定义标准解除函数 void ClassListDataDestructor( void *p ) { // 对节点指针进行类型转换 pND_LIST pl = (pND_LIST)p; // 对数据指针进行类型转换 pND_LIST_DATA pLD = (pND_LIST_DATA) ( pl + 1 ); delete pLD->p; } // 测试上面的代码 void MyCListClassTest() { // 创建链表类 CList* pA_List_of_Data = new CList(ClassListDataDestructor); // 创建数据对象 pND_LIST_DATA d = new LIST_DATA; d->p = new char[24]; strcpy( d->p, "Hello" ); // 创建指向链表顶部的局部指针 pND_LIST Head = NULL; //向链表中添加一些数据 Head = pA_List_of_Data->AddToList( (void *) d, sizeof( pND_LIST_DATA ) ); // 该对象已被复制,现在删除原来的对象 delete d; // 确认它已被存储 char * p = ((pND_LIST_DATA) pA_List_of_Data->GetCurrentData())->p; d = new LIST_DATA; d->p = new char[24]; strcpy( d->p, "World" ); Head = pA_List_of_Data->AddToList( (void *) d, sizeof( pND_LIST_DATA ) ); // 该对象已被复制,现在删除原来的对象 delete d; // 确认它已被存储 p = ((pND_LIST_DATA) pA_List_of_Data->GetCurrentData())->p; // 删除链表类,析构函数将删除链表 delete pA_List_of_Data; } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |