快乐学习,快乐生活!- /**
- * 通用平台逻辑模块组
- * Copyright (c) 2012 xxxx
- *
- * @file linkers.c
- * @brief 二级链表
- * @author
- * @date 2012/10/9
- *
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /**
- * @brief 二级链表
- */
- typedef struct _Menu Menu;
- typedef struct _ChildMenu ChildMenu;
- /// < 第二级链表
- struct _ChildMenu
- {
- char * m_name;
- ChildMenu * cNext;
- };
- /// < 第一级链表
- struct _Menu
- {
- char * m_name;
- Menu * pMenu;
- int count;
- ChildMenu * pChildMenu;
- };
- /// < 初始化链表
- Menu * initMenu()
- {
- Menu * pHead = NULL;
- Menu * pNext = NULL;
- ChildMenu * pcHead = NULL;
- int i , j;
- for (i = 0; i < 2; i)
- {
- Menu * pMenu = (Menu *)malloc(sizeof(Menu));
- if (!pMenu)
- return NULL;
- memset(pMenu, 0, sizeof(Menu));
- pMenu->m_name = (char *)malloc(3);
- if (!pMenu->m_name)
- return NULL;
- memset(pMenu->m_name, 0, 3);
- strcpy(pMenu->m_name, "a");
- pMenu->count = 2;
- if (0 == i)
- {
- pHead = pNext = pMenu;
- pMenu->pMenu = NULL;
- }
- else
- {
- pNext->pMenu = pMenu;
- pMenu->pMenu = NULL;
- pNext = pMenu;
- }
- for (j = 0; j < pMenu->count; j)
- {
- ChildMenu * pChildMenu = (ChildMenu *)malloc(sizeof(ChildMenu));
- if (!pChildMenu)
- return NULL;
- pChildMenu->m_name = (char *)malloc(3);
- if (!pChildMenu->m_name)
- return NULL;
- memset(pChildMenu->m_name, 0, 3);
- strcpy(pChildMenu->m_name, "b");
- if (0 == j)
- {
- pNext->pChildMenu = pChildMenu;
- pcHead = pChildMenu;
- pChildMenu->cNext = NULL;
- }
- else
- {
- pcHead->cNext = pChildMenu;
- pChildMenu->cNext = NULL;
- pcHead = pChildMenu;
- }
- }
- }
- return pHead;
- }
- /// < 二级链表的遍历
- void traverse_linker(const Menu * head)
- {
- const Menu * p = head;
- ChildMenu * q;
- int i;
- if (!head)
- return ;
- while (p)
- {
- printf("-%s\n", p->m_name);
- q = p->pChildMenu;
- for (i = 0; i < p->count; i) /// < 有个数就是好!
- {
- printf(" --%s", q->m_name);
- q = q->cNext;
- }
- putchar('\n');
- p = p->pMenu;
- }
- }
- /// < 二级链表的释放 - 从头开始
- void free_linker(Menu ** head)
- {
- Menu * firstgo = NULL; /// < 负责一级链表
- ChildMenu * childgo = NULL; /// < 负责二级链表
- Menu * firstNext = NULL; /// < 指向一级链表的后一个
- ChildMenu * childNext = NULL; /// < 指向二级链表后一个
- if (!(*head))
- return;
- firstgo = *head;
- while (firstgo)
- {
- firstNext = firstgo->pMenu;
- childgo = firstgo->pChildMenu;
- while (childgo)
- {
- /// < 释放第二级链表
- childNext = childgo->cNext;
-
- free(childgo->m_name);
- free(childgo);
- /// < 下一个
- childgo = childNext;
- }
-
- /// < 释放第一级链表
- free(firstgo->m_name);
- free(firstgo);
- /// < 下一个
- firstgo = firstNext;
- }
- *head = NULL;
- }
- /**
- * @brief 鉴于所使用的环境,carbide.c ;所以用gcc调试,用symbian os application进行内存泄漏检查!
- * 突然发现symbian模拟器运行程序挺爽的,关闭后检查你的内存泄漏。挺好,应该还有很多工具可以检查,目前
- * 这个不错!不过调试的话,看内存,看变量就可以了,不需要去打印哈!你懂的..
- */
- int main()
- {
- Menu * pMenu = initMenu();
- /* printf("%s ", pMenu->m_name);
- printf("%d\n", pMenu->count);
- printf(" -->%s -->", pMenu->pChildMenu->m_name);
- printf("%s\n", pMenu->pChildMenu->cNext->m_name);
- printf("%s ", pMenu->pMenu->m_name);
- printf("%d\n", pMenu->pMenu->count);
- printf(" -->%s -->", pMenu->pMenu->pChildMenu->m_name);
- printf("%s\n", pMenu->pMenu->pChildMenu->cNext->m_name);
- */
- traverse_linker(pMenu);
- free_linker(&pMenu);
- /// < 证明下
- traverse_linker(pMenu);
- return 0;
- }
- /**
- * 通用平台逻辑模块组
- * Copyright (c) 2012 xxxx
- *
- * @file linkers2.c
- * @brief 二级链表2
- * @author
- * @date 2012/10/9
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /**
- * @brief 二级链表2
- */
- typedef struct _Menu Menu;
- struct _Menu
- {
- int id;
- char * name;
- Menu * brother; /// < 兄弟节点 - 作为一级链表
- Menu * child; /// < 儿子节点 - 作为二级或者多级链表(针对节点结构都相同)
- };
- /**
- * @brief 初始化
- * 类似:
- * -文件 --打开
- * --保存
- * --退出
- *
- * -1 --11
- * --11
- * --11
- * -1 --11
- * --11
- * --11
- */
- Menu * initial_link()
- {
- Menu * head;
- Menu * next;
- Menu * cnext;
- int i, j;
- for (i = 0; i < 3; i) /// < 三兄弟
- {
- Menu * pTemp = (Menu *)malloc(sizeof(Menu));
- if (!pTemp)
- return NULL;
- memset(pTemp, 0, sizeof(Menu));
- pTemp->name = (char *)malloc(2);
- if (!pTemp->name)
- return NULL;
- strcpy(pTemp->name, "1");
- pTemp->id = 1;
- if (0 == i)
- {
- head = next = pTemp;
- pTemp->brother = NULL;
- pTemp->child = NULL;
- }
- else
- {
- next->brother = pTemp;
- pTemp->brother = NULL;
- pTemp->child = NULL;
- next = pTemp;
- }
- for (j = 0; j < 3; j) /// < 一个儿子,两个孙子
- {
- Menu * pTempc = (Menu *)malloc(sizeof(Menu));
- if (!pTempc)
- return NULL;
- memset(pTempc, 0, sizeof(Menu));
- pTempc->name = (char *)malloc(3);
- if (!pTempc->name)
- return NULL;
- strcpy(pTempc->name, "11");
- pTempc->id = 11;
- if (0 == j)
- {
- next->child = pTempc;
- cnext = pTempc;
- pTempc->brother = NULL;
- pTempc->child = NULL;
- }
- else
- {
- cnext->child = pTempc;
- pTempc->brother = NULL;
- pTempc->child = NULL;
- cnext = pTempc;
- }
- }
- }
- return head;
- }
- /**
- * @brief 遍历
- */
- void traverse_linker(const Menu * head)
- {
- const Menu * p = head;
- const Menu * q;
- if (!head)
- return ;
- while (p)
- {
- printf("-%d %s\n", p->id, p->name);
- q = p->child;
- while (q)
- {
- printf(" --%d %s\n", q->id, q->name);
- q = q->child;
- }
- p = p->brother;
- }
- }
- /*
- * @brief 二级链表的释放 - 从头开始 和linker一样的方式。不过仔细一看这种方式有问题?儿子还有儿子没考虑!不过从经验来看,我们开发手机软件
- * 所需要的菜单不会非常多,因此我们不必去绞尽脑汁像一个通用遍历的方法,没有必要!你认为了...
- */
- void free_linker(Menu ** head)
- {
- Menu * firstgo = NULL; /// < 负责一级链表
- Menu * childgo = NULL; /// < 负责二级链表
- Menu * firstNext = NULL; /// < 指向一级链表的后一个
- Menu * childNext = NULL; /// < 指向二级链表后一个
- if (!(*head))
- return;
- firstgo = *head;
- while (firstgo)
- {
- firstNext = firstgo->brother;
- childgo = firstgo->child;
- while (childgo)
- {
- /// < 释放第二级链表
- childNext = childgo->child;
- free(childgo->name);
- free(childgo);
- /// < 下一个
- childgo = childNext;
- }
- /// < 释放第一级链表
- free(firstgo->name);
- free(firstgo);
- /// < 下一个
- firstgo = firstNext;
- }
- *head = NULL;
- }
- int main(int argc, char **argv)
- {
- Menu * head = initial_link();
- traverse_linker(head);
- free_linker(&head);
- return 0;
- }
接着哦!- /*
- ============================================================================
- Name : linkerList3.c
- Author : hl
- Version :
- Copyright : Copyright (c) 2012 Tiros
- Description : Linker3 in C, Ansi-style
- ============================================================================
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /**
- * @brief 可扩展性的二级链表 (参考Company)
- */
- typedef struct _linkerList linkerList;
- /**
- * @brief “真实”数据
- */
- typedef struct _Data
- {
- int id;
- char * name;
- }Data;
- /// < 链表结点 - 描述结点的子结点个数和数据信息
- typedef struct _linkerNodeData
- {
- Data * pData; /// < 数据
- int childNodesCount; /// < 子链表的个数
- linkerList * plinkerList;
- }linkerNodeData;
- /// < 二级链表 - 之所以这样封装的原因:如果取消该包装,那么我们要实现二级链表就只能像之前,在linkerNodeData中定义一个brother指针来指向下一个条目,画画图就知道了;我们再linkerNodeData中"回调"linkerList就相当于包含了一个数据类型和指向下一个结点的指针的集合!
- struct _linkerList
- {
- linkerNodeData * pNodeData;
- linkerList * pNext;
- };
- static Data * createData(int id, char * name);
- static linkerList * cratelinkerList(int id, char * name, int count);
- static linkerNodeData * createlinkerNodeData(int id, char * name, int count);
- /**
- * @brief "定值"初始化 问题很严重,代码冗余,逻辑混乱
- */
- /*linkerList * initLinker()
- {
- linkerList * pHead;
- linkerList * pNext;
- int i = 0;
- int j = 0;
- for ( ; i < 3; ++i) /// < 三个一级菜单条目
- {
- linkerList * pTmp = (linkerList *)malloc(sizeof(linkerList));
- if (!pTmp)
- return NULL;
- pTmp->pNodeData = (linkerNodeData *)malloc(sizeof(linkerNodeData));
- if (!pTmp->pNodeData)
- {
- free(pTmp);
- return NULL;
- }
- pTmp->pNodeData->pData = (Data *)malloc(sizeof(Data));
- if (!pTmp->pNodeData->pData)
- {
- free(pTmp);
- free(pTmp->pNodeData);
- return NULL;
- }
- pTmp->pNodeData->pData->id = i;
- pTmp->pNodeData->pData->name = (char *)malloc(10);
- if (!pTmp->pNodeData->pData->name)
- {
- free(pTmp);
- free(pTmp->pNodeData);
- free(pTmp->pNodeData->pData);
- return NULL;
- }
- memset( pTmp->pNodeData->pData->name, 0, 10);
- strcpy(pTmp->pNodeData->pData->name, "文件");
- if (0 == i)
- {
- pHead = pNext = pTmp; ///-------
- pTmp->pNext = NULL;
- }
- else
- {
- pNext->pNext = pTmp;
- pTmp->pNext = NULL;
- pNext = pTmp;
- }
- pTmp->pNodeData->childNodesCount = 1; /// < 一个二级菜单条目
- for ( j = 0; j < pTmp->pNodeData->childNodesCount; ++j)
- {
- linkerList * ppTmp = (linkerList *) malloc(sizeof(linkerList));
- if (!ppTmp)
- return NULL;
- ppTmp->pNodeData = (linkerNodeData *) malloc(sizeof(linkerNodeData));
- if (!ppTmp->pNodeData)
- {
- free(ppTmp);
- return NULL;
- }
- ppTmp->pNodeData->pData = (Data *) malloc(sizeof(Data));
- if (!ppTmp->pNodeData->pData)
- {
- free(ppTmp);
- free(ppTmp->pNodeData);
- return NULL;
- }
- ppTmp->pNodeData->pData->id = j;
- ppTmp->pNodeData->pData->name = (char *) malloc(10);
- if (!ppTmp->pNodeData->pData->name)
- {
- free(ppTmp);
- free(ppTmp->pNodeData);
- free(ppTmp->pNodeData->pData);
- return NULL;
- }
- memset(ppTmp->pNodeData->pData->name, 0, 10);
- strcpy(ppTmp->pNodeData->pData->name, "打开");
- ppTmp->pNodeData->childNodesCount = 0;
- ppTmp->pNodeData->plinkerList = NULL; /// < 无三级了
- pNext->pNext = ppTmp;
- ppTmp->pNext = NULL;
- }
- }
- return pHead;
- }*/
- /**
- * @brief 释放
- */
- void freeListNode(linkerList * head)
- {
- if (!head)
- return ;
- }
- /**
- * @brief 数据创建
- */
- Data * createData(int id, char * name)
- {
- Data * pData = (Data *)malloc(sizeof(Data));
- if (!pData)
- return NULL;
- /// < 项id
- pData->id = id;
- /// < 项名称
- pData->name = (char *)malloc(strlen(name) + 1);
- if (!pData->name)
- {
- free(pData);
- return NULL;
- }
- memset(pData->name, 0, strlen(name) + 1);
- strcpy(pData->name, name);
- return pData;
- }
- /**
- * @brief 链表结点创建
- */
- linkerNodeData * createlinkerNodeData(int id, char * name, int count)
- {
- linkerNodeData * plinkerNodeData = (linkerNodeData *)malloc(sizeof(linkerNodeData));
- int i = 0;
- if (!plinkerNodeData)
- return NULL;
- memset(plinkerNodeData, 0, sizeof(linkerNodeData));
- plinkerNodeData->pData = createData(id, name);
- plinkerNodeData->childNodesCount = count;
- linkerList * next;
- for (i = 0; i < count; ++i)
- {
- linkerList * plinkerList = cratelinkerList(i, name, --count); ///特别注意
- if (!plinkerList)
- return NULL;
- if (!plinkerNodeData->plinkerList)
- {
- plinkerNodeData->plinkerList = plinkerList;
- next = plinkerList;
- plinkerList->pNext = NULL;
- }
- else
- {
- next->pNext = plinkerList;
- next = plinkerList;
- plinkerList->pNext = NULL;
- }
- }
- return plinkerNodeData;
- }
- /**
- * @brief 链表创建
- */
- linkerList * cratelinkerList(int id, char * name, int count)
- {
- linkerList * plinkerList = NULL; /// < 链表头指针
- plinkerList = (linkerList *) malloc(sizeof(linkerList));
- if (!plinkerList)
- return NULL;
- memset(plinkerList, 0, sizeof(linkerList));
- plinkerList->pNodeData = createlinkerNodeData(id, name, count);
- if (!plinkerList->pNodeData)
- return NULL;
- return plinkerList;
- }
- /**
- * @brief "定值"初始化
- */
- linkerList * initLinker()
- {
- linkerList * head = NULL;
- linkerList * next = NULL;
- int i = 0;
- head = (linkerList *) malloc(sizeof(linkerList));
- if (!head)
- return NULL;
- memset(head, 0, sizeof(linkerList));
- for ( ; i < 3; ++i) /// < 3个一级链表
- {
- linkerList * pTmp = cratelinkerList(i, "文件", 2); /// < 每个一级含2个二级的
- if (0 == i)
- {
- next = head = pTmp;
- pTmp->pNext = NULL;
- }
- else
- {
- next->pNext = pTmp;
- next = pTmp;
- pTmp->pNext = NULL;
- }
- }
- return head;
- }
- /**
- * @brief 遍历二级链表
- */
- void traverseLinker(linkerList * head)
- {
- //TODO
- }
- /**
- * @brief 释放
- */
- void releaseLinker(linkerList * head)
- {
- //TODO
- }
- /**
- * @brief 最大的收获就是函数相互调用!感觉挺麻烦的,调试也麻烦,稍微写错了,就死循环!或者失败!
- */
- int main(void)
- {
- linkerList * plinkerList = initLinker();
- traverseLinker(plinkerList);
- return EXIT_SUCCESS;
- }
自拍...
阅读(239) | 评论(0) | 转发(0) |