c/c++ 链表实现
//链表的基本用法代码实现
/************************************************************************/ /* Created: 2014-03-02 /* Author: http://weibo.com/3088919800/profile /************************************************************************/ #include "stdio.h" #include "stdlib.h" struct ListNode { int value; ListNode * next; }; ListNode* CreateListNode(const int& nNodeValue) { ListNode* pNode; pNode = (ListNode*)malloc(sizeof(ListNode)); if (!pNode) return NULL; pNode->value = nNodeValue; pNode->next = NULL; return pNode; } //we need the head node value. ListNode * CreateList(const int& nListHeadVal) { ListNode * li; li = CreateListNode(nListHeadVal); if (!li) return NULL; return li; } //free all nodes in the list void FreeList(ListNode *li) { ListNode * pListNode, *pTmpNode; pListNode = li; while (pListNode) { pTmpNode = pListNode; pListNode = pListNode->next; free(pTmpNode); } li = NULL; } ListNode* AppendNode(ListNode* pList, const int &nVal) { ListNode* node; ListNode* header; node = CreateListNode(nVal); if (!node) return pList; header = pList; if (!header) return node; header = pList; while(header->next) header = header->next; header->next = node; return pList; } //if find return the node, else return null ListNode* Find(ListNode* pList, const int& nVal) { ListNode* pListNode = NULL; pListNode = pList; while (pListNode) { if (pListNode->value == nVal) break; pListNode = pListNode->next; } return pListNode; } //insert decreasing 增减 ListNode* Insert(ListNode* pList, const int& nVal) { ListNode * pNewNode = NULL; ListNode* pIterNode = NULL; pNewNode = CreateListNode(nVal); if (!pNewNode) return pList; if (!pList) return pNewNode; pIterNode = pList; while (pIterNode->value > nVal && pIterNode->next && pIterNode->next->value > nVal) pIterNode = pIterNode->next; //if true ,it is the first node if (pIterNode->value <= nVal) { pNewNode->next = pIterNode; return pNewNode; } if (pIterNode->next) pNewNode->next = pIterNode->next; pIterNode->next = pNewNode; return pList; } //insert Ascending,递增 ListNode* Insert2(ListNode* pList, const int& nVal) { ListNode * pNewNode = NULL; ListNode* pIterNode = NULL; pNewNode = CreateListNode(nVal); if (!pNewNode) return pList; if (!pList) return pNewNode; pIterNode = pList; while (pIterNode->value < nVal && pIterNode->next && pIterNode->next->value < nVal) pIterNode= pIterNode->next; //if true ,it is the first node if (pIterNode->value > nVal) { pNewNode->next = pIterNode; return pNewNode; } // insert the node. if (pIterNode->next) pNewNode->next = pIterNode->next; pIterNode->next = pNewNode; return pList; } void PrintList(ListNode* list) { ListNode* pIterNode; pIterNode = list; while (pIterNode) { printf("%d ", pIterNode->value); pIterNode = pIterNode->next; } } //链表倒置 ListNode* InvertList(ListNode* list) { ListNode *pIterNode, *pNewHead, *pTmpNode; pTmpNode = list; if (!pTmpNode || !(pTmpNode->next)) return list; pNewHead = list; pIterNode = list; while(pIterNode->next) { pTmpNode = pIterNode->next->next; pIterNode->next->next = pNewHead; pNewHead = pIterNode->next; pIterNode->next = pTmpNode; } return pNewHead; } int main(int argc, char* argv[]) { ListNode* list = CreateList(12); ListNode* pNode; if (!list) { printf("create list error!"); exit(-1); } int i = 0; for (int i = 0; i< 20; i++) { list = Insert2(list, i); } printf("\nAfter insert list: \n"); PrintList(list); list = InvertList(list); printf("\nAfter invert the list\n"); PrintList(list); //test find . pNode = Find(list, 8); if (pNode) { printf("\nfind it:%d\n", pNode->value); } else { printf("\nnot find it:%d\n", 8); } //free all the list node FreeList(list); getchar(); return 0; }
posted on 2014-04-09 16:01 algorithmic 阅读(514) 评论(0) 收藏 举报
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个 java 空指针异常的解决过程
· 揭开 SQL Server 和 PostgreSQL 填充因子的神秘面纱
· 没有调度器的协程不是好协程,零基础深入浅出 C++20 协程
· 别做抢活的导演:代码中的抽象层次原则
· 从 Redis 客户端超时到 .NET 线程池挑战
· 会Vibe Coding的同事:我一个人干掉整个技术部!
· 回答准确率从60%飙至95%!AI知识库救命方案
· 揭开SQL Server和PostgreSQL填充因子的神秘面纱
· dotnetty 内存泄漏的BUG修复了
· 20250709 - GMX V1 攻击事件: 重入漏洞导致的总体仓位价值操纵