剑指offer之反向打印链表值

举报
chenyu 发表于 2021/07/26 23:24:15 2021/07/26
【摘要】 1 问题 反向打印链表值         2 思考 1) 我们利用栈的思想,新进后出,把链表的每个元素分别入栈之后再打印栈 2)既然上面用到了栈,我们应该就会想到用到递归来实现         3 代码实现 #include &l...

1 问题

反向打印链表值

 

 

 

 

2 思考

1) 我们利用栈的思想,新进后出,把链表的每个元素分别入栈之后再打印栈

2)既然上面用到了栈,我们应该就会想到用到递归来实现

 

 

 

 

3 代码实现


  
  1. #include <iostream>
  2. #include <stack>
  3. #include <stdlib.h>
  4. using namespace std;
  5. typedef struct node
  6. {
  7. int value;
  8. struct node *next;
  9. } Node;
  10. /**
  11. *把链表的每一个元素遍历出来放进栈,然后利用
  12. *栈把先进后出的特点,然后打印栈
  13. */
  14. void reverse_printf(Node *head)
  15. {
  16. if (head == NULL)
  17. {
  18. std::cout << "head is NULL" << std::endl;
  19. return;
  20. }
  21. Node *p = head;
  22. stack<Node *> stk;
  23. while (p != NULL)
  24. {
  25. stk.push(p);
  26. p = p->next;
  27. }
  28. while(!stk.empty())
  29. {
  30. Node *node = stk.top();
  31. std::cout << node->value << std::endl;
  32. stk.pop();
  33. }
  34. }
  35. /**
  36. *既然上面用到了栈,那么我们自然而然就会想到
  37. *用递归的思想,我们就自己调用自己直到遍历到最后
  38. *很明显我们递归的条件是最后一个原始的next不能为空
  39. */
  40. void reverse_printf1(Node *head)
  41. {
  42. /**这样也行
  43. if (head == NULL)
  44. {
  45. return;
  46. }
  47. reverse_printf1(head->next);
  48. std::cout << head->value << std::endl;**/
  49. if (head != NULL)
  50. {
  51. reverse_printf1(head->next);
  52. std::cout << head->value << std::endl;
  53. }
  54. }
  55. void printf(Node *head)
  56. {
  57. if (head == NULL)
  58. {
  59. std::cout << "head is NULL" << std::endl;
  60. return;
  61. }
  62. Node *p = head;
  63. while (p != NULL)
  64. {
  65. std::cout << p->value << std::endl;
  66. p = p->next;
  67. }
  68. }
  69. int main()
  70. {
  71. Node *head = NULL;
  72. Node *node1 = NULL;
  73. Node *node2 = NULL;
  74. Node *node3 = NULL;
  75. head = (struct node*)malloc(sizeof(Node));
  76. node1 = (struct node*)malloc(sizeof(Node));
  77. node2 = (struct node*)malloc(sizeof(Node));
  78. node3 = (struct node*)malloc(sizeof(Node));
  79. if (head == NULL || node1 == NULL || node2 == NULL || node3 == NULL)
  80. {
  81. std::cout << "malloc fail" << std::endl;
  82. return -1;
  83. }
  84. head->value = 0;
  85. head->next = node1;
  86. node1->value = 1;
  87. node1->next = node2;
  88. node2->value = 2;
  89. node2->next = node3;
  90. node3->value = 3;
  91. node3->next = NULL;
  92. printf(head);
  93. std::cout << "***************" << std::endl;
  94. reverse_printf(head);
  95. std::cout << "***************" << std::endl;
  96. reverse_printf1(head);
  97. free(head);
  98. free(node1);
  99. free(node2);
  100. free(node3);
  101. return 0;
  102. }

 

 

 

 

4 运行结果


  
  1. 0
  2. 1
  3. 2
  4. 3
  5. ***************
  6. 3
  7. 2
  8. 1
  9. 0
  10. ***************
  11. 3
  12. 2
  13. 1
  14. 0

 

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/87471131

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。