单链表的创建,查找,删除,顺序打印,倒序打印(剑指offer)
2017-11-29 15:05
357 查看
单链表的创建,查找元素,删除元素,顺序打印单链表元素,逆序(倒序)打印单链表元素。
#include <cassert> #include <iostream> //默认不使用栈实现反序打印链表中的元素 #define USE_STACK 0 #if USE_STACK #include <stack> #endif struct ListNode { int mVal; ListNode* mpNext; }; /** *@pHead 使用指针的指针,使得函数结束后pHead指向的节点有效 *@val 待插入的值 */ void addNodeToTail(ListNode** pHead, int val) { ListNode* pNewNode = new ListNode; pNewNode->mVal = val; pNewNode->mpNext = nullptr; //如果头节点为空 if (*pHead == nullptr) { *pHead = pNewNode; } else { ListNode* pNode = *pHead; while (pNode->mpNext != nullptr) pNode = pNode->mpNext; pNode->mpNext = pNewNode; } } void destroyList(ListNode** pHead) { std::cout << "\nstart free memery..." << std::endl; ListNode* pNode = *pHead; while (*pHead != nullptr) { *pHead = (*pHead)->mpNext; delete pNode; pNode = *pHead; } std::cout << "free memery complete" << std::endl; } //从头到尾打印链表 void printList(ListNode**pHead) { ListNode *pNode =*pHead; while (pNode != nullptr) { std::cout << pNode->mVal << " "; pNode = pNode->mpNext; } } //从尾到头打印链表,使用递归实现且不该变原列表的结构 void p 4000 rintReverseList(ListNode* pHead) { if (pHead != nullptr) { if (pHead->mpNext != nullptr) { printReverseList(pHead->mpNext); } std::cout << pHead->mVal << "\t"; } } #if USE_STACK void printReverseList_stack(ListNode* pHead) { std::stack<ListNode*> nodes; ListNode* pNode = pHead; while (pNode != nullptr) { nodes.push(pNode); pNode = pNode->mpNext; } while (!nodes.empty()) { pNode = nodes.top(); std::cout << pNode->mVal << "\t"; nodes.pop(); } } #endif bool findVal(ListNode** pHead, int val) { ListNode* pNode = *pHead; while(pNode != nullptr && pNode->mVal != val) { pNode = pNode->mpNext; } return pNode == nullptr ? false : true; } /* *作用: 删除指定元素为val的节点 *@pHead 指向头指针 *@val 指定元素为val */ void removeNode(ListNode**pHead, int val) { if (*pHead == nullptr) return; //将要被删除的节点 ListNode* pToBeDeleted = nullptr; //若删除的节点是头节点 if ((*pHead)->mVal == val) { pToBeDeleted = *pHead; *pHead = nullptr; } else { ListNode* pNode = *pHead; //查找要删除的元素,while结束时返回要查找节点的前一个节点 while (pNode->mpNext != nullptr && pNode->mpNext->mVal != val) { pNode = pNode->mpNext; } //找到要删除的节点 if (pNode->mpNext != nullptr && pNode->mpNext->mVal == val) { pToBeDeleted = pNode->mpNext; //指向要删除节点的下一个节点 pNode->mpNext = pNode->mpNext->mpNext; } } if (pToBeDeleted != nullptr) { delete pToBeDeleted; pToBeDeleted = nullptr; } } int main(int argc, char* argv[]) { ListNode* pHead = nullptr; int val; //val == 0为结束标志 while (std::cin >> val && val != 0) addNodeToTail(&pHead, val); std::cout << "打印初始化链表:"; printList(&pHead); #if USE_STACK printReverseList_stack(pHead); #endif /* std::cout << "\n反序打印链表: "; printReverseList(pHead); std::cout << "\n"; printList(&pHead); std::cout << std::endl; */ /* std::cout << "\n请输入要查找的元素: "; int find_val; while (std::cin >> find_val && find_val != 0) { if (findVal(&pHead, find_val)) std::cout << "存在该元素" << std::endl; else std::cout << "不存在该元素" << std::endl; std::cout << "请输入要查找的元素: "; } std::cout << "\n请输入要删除的节点: "; int remove_val; while (std::cin >> remove_val && remove_val != 0) { removeNode(&pHead, remove_val); std::cout << "删除后的节点序列为 : " << std::endl; printList(&pHead); std::cout << "\n请输入要删除的节点: "; } */ //释放申请的内存空间 destroyList(&pHead); return 0; }
相关文章推荐
- 单链表的创建,表长,插入,查找,逆置,中间元素,删除节点,打印
- 静态顺序表的实现创建、查找、删除
- 实现单链表的创建、打印、插入、删除、逆置
- C++单链表的操作(创建,删除,打印,遍历,插入)
- 单链表的测长和打印和节点查找、插入、删除、逆置
- 单链表的创建(头插尾插),表长,输出,插入,删除,查找,逆置,分解长两个链表(奇数偶数链表),查找倒数第k个元素,产出相同元素
- 单链表的创建,删除,插入以及打印。
- 单链表的常用操作,包括单链表的创建、插入、删除、排序、逆置以及打印输出等
- C++单链表的动态创建,查找,遍历,删除,插入,添加,排序
- 顺序表的创建、插入、删除、查找、求表长、输出元素
- 单链表的创建、初始化、插入、删除 、输出、求表长、清空、查找操作
- 单链表的创建、删除与打印
- 编程菜鸟的日记-初学尝试编程-顺序表的类定义及其基本操作算法(创建表、元素插入、元素删除、顺序查找、测表空、求表长、输出等)
- 单链表的创建、测长、打印、插入和删除
- C语言单链表的创建、插入、查找、删除、求长、排序、遍历
- 用c实现HASH表创建、插入、查找、删除、打印
- 单链表的创建,查找,删除,插入。
- 单链表的创建、插入,删除、查找等操作
- 单链表-创建、插入、删除、查找、反转等操作
- java实现单链表的初始化,创建,删除,插入,查找,排序,同项删除,退出等功能