// 面试题6:从尾到头打印链表 // 题目:输入一个链表的头结点,从尾到头反过来打印出每个结点的值。 //此处注意导入顺序 #include <stack> #include "List.h" void PrintListReversingly_Iteratively(ListNode* pHead) { //方法1:用栈实现, 先入后出保存后打印 std::stack<ListNode*> nodes; ListNode* pNode = pHead; while (pNode != nullptr) //寻找链表尾部并保存 { nodes.push(pNode); pNode = pNode->m_pNext; } while (!nodes.empty()) //打印栈 { pNode = nodes.top(); printf("%d\t", pNode->m_nValue); nodes.pop(); } } void PrintListReversingly_Recursively(ListNode* pHead) { //方法2:递归实质上也是一种栈, 访问下一节点并打印本节点值 ListNode* pNode = pHead; if (pNode != nullptr) //当前节点不是尾节点 { if (pNode->m_pNext != nullptr) //下一节点不是尾节点 { PrintListReversingly_Recursively(pNode->m_pNext); //打印下一节点 } printf("%d\t", pNode->m_nValue); //打印当前节点 } }
// ====================测试代码==================== void Test(ListNode* pHead) { PrintList(pHead); PrintListReversingly_Iteratively(pHead); printf("\n"); PrintListReversingly_Recursively(pHead); } // 1->2->3->4->5 void Test1() { printf("\nTest1 begins.\n"); ListNode* pNode1 = CreateListNode(1); ListNode* pNode2 = CreateListNode(2); ListNode* pNode3 = CreateListNode(3); ListNode* pNode4 = CreateListNode(4); ListNode* pNode5 = CreateListNode(5); ConnectListNodes(pNode1, pNode2); ConnectListNodes(pNode2, pNode3); ConnectListNodes(pNode3, pNode4); ConnectListNodes(pNode4, pNode5); Test(pNode1); DestroyList(pNode1); } // 只有一个结点的链表: 1 void Test2() { printf("\nTest2 begins.\n"); ListNode* pNode1 = CreateListNode(1); Test(pNode1); DestroyList(pNode1); } // 空链表 void Test3() { printf("\nTest3 begins.\n"); Test(nullptr); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); return 0; }
//================================================================== // 《剑指Offer——名企面试官精讲典型编程题》代码 // 作者:何海涛 //================================================================== struct ListNode { int m_nValue; ListNode* m_pNext; }; __declspec(dllexport) ListNode* CreateListNode(int value); __declspec(dllexport) void ConnectListNodes(ListNode* pCurrent, ListNode* pNext); __declspec(dllexport) void PrintListNode(ListNode* pNode); __declspec(dllexport) void PrintList(ListNode* pHead); __declspec(dllexport) void DestroyList(ListNode* pHead); __declspec(dllexport) void AddToTail(ListNode** pHead, int value); __declspec(dllexport) void RemoveNode(ListNode** pHead, int value);
#include "list.h" #include <stdio.h> #include <stdlib.h> //创建一个节点,并返回地址 ListNode* CreateListNode(int value) { ListNode* pNode = new ListNode(); //新建节点 pNode->m_nValue = value; //数据赋值 pNode->m_pNext = nullptr; //节点置为表尾 return pNode; } //连接两个节点 void ConnectListNodes(ListNode* pCurrent, ListNode* pNext) { if (pCurrent == nullptr) //当前节点是否存在 { printf("Error to connect two nodes.\n"); exit(1); } pCurrent->m_pNext = pNext; //下一节点地址赋给当前节点 } //打印当前节点值 void PrintListNode(ListNode* pNode) { if (pNode == nullptr) { printf("The node is nullptr\n"); } else { printf("The key in node is %d.\n", pNode->m_nValue); } } //打印整个链表 void PrintList(ListNode* pHead) { printf("PrintList starts.\n"); ListNode* pNode = pHead; while (pNode != nullptr) { printf("%d\t", pNode->m_nValue); pNode = pNode->m_pNext; } printf("\nPrintList ends.\n"); } //删除整个链表 void DestroyList(ListNode* pHead) { ListNode* pNode = pHead; while (pNode != nullptr) { pHead = pHead->m_pNext; //指向第一个节点 delete pNode; //删除当前节点 pNode = pHead; //新头节点 } } //添加节点链表末尾, 头节点为指向指针的指针 void AddToTail(ListNode** pHead, int value) { ListNode* pNew = new ListNode(); pNew->m_nValue = value; pNew->m_pNext = nullptr; if (*pHead == nullptr) { *pHead = pNew; //重要 } else { ListNode* pNode = *pHead; while (pNode->m_pNext != nullptr) pNode = pNode->m_pNext; pNode->m_pNext = pNew; } } //移除值为value的第一个节点, 注意头节点是指向指针的指针 void RemoveNode(ListNode** pHead, int value) { if (pHead == nullptr || *pHead == nullptr) return; ListNode* pToBeDeleted = nullptr; //保存需要删除的节点地址 if ((*pHead)->m_nValue == value) { pToBeDeleted = *pHead; *pHead = (*pHead)->m_pNext; } else { ListNode* pNode = *pHead; while (pNode->m_pNext != nullptr && pNode->m_pNext->m_nValue != value) pNode = pNode->m_pNext; if (pNode->m_pNext != nullptr && pNode->m_pNext->m_nValue == value) { pToBeDeleted = pNode->m_pNext; pNode->m_pNext = pNode->m_pNext->m_pNext; } } if (pToBeDeleted != nullptr) { delete pToBeDeleted; pToBeDeleted = nullptr; } }
分析:还在分析。