当前位置:   article > 正文

数据结构与算法-链栈_数据结构与算法链栈应用

数据结构与算法链栈应用
  1. #include<iostream>
  2. struct stackNode {
  3. int date;
  4. struct stackNode* next;
  5. };
  6. struct stackNode* createStackNode() {
  7. struct stackNode* p = new stackNode;
  8. if (p == nullptr) {
  9. return nullptr;
  10. }
  11. p->next = nullptr;
  12. return p;
  13. }
  14. void push(struct stackNode* node, int x) {
  15. struct stackNode* temp = new stackNode;
  16. if (temp == nullptr) {
  17. return;
  18. }
  19. else {
  20. temp->next = node->next;
  21. temp->date = x;
  22. node->next = temp;
  23. }
  24. }
  25. void pop(struct stackNode* node) {
  26. if (node == nullptr) {
  27. return;
  28. }
  29. else {
  30. struct stackNode* temp = node->next;
  31. node->next = node->next->next;
  32. free(temp);
  33. }
  34. }
  35. bool isEmpty(struct stackNode* node) {
  36. return node->next == NULL;
  37. }
  38. int getSize(struct stackNode* node) {
  39. int count = 0;
  40. struct stackNode* p = node->next;
  41. while (p) {
  42. count++;
  43. p = p->next;
  44. }
  45. return count;
  46. }
  47. int getTop(struct stackNode* node) {
  48. if (node == nullptr) {
  49. return -1;
  50. }
  51. else {
  52. return node->next->date;
  53. }
  54. }
  55. void printStack(struct stackNode* node) {
  56. struct stackNode* p = node->next;
  57. while (p) {
  58. std::cout << p->date<<std::endl;
  59. p = p->next;
  60. }
  61. return;
  62. }
  63. void makeEmpty(struct stackNode* node) {
  64. if (node == nullptr) {
  65. return;
  66. }
  67. while (!isEmpty(node)) {
  68. pop(node);
  69. }
  70. }
  71. int main() {
  72. return 0;
  73. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/425031
推荐阅读
相关标签
  

闽ICP备14008679号