[关闭]
@quinn 2015-03-20T09:28:30.000000Z 字数 2616 阅读 3408

栈的实现——链表和数组

数据结构


C语言(打印函数采用的c++):
栈的链表实现—— 栈的初始化(创建||清空)、入栈、出栈(获取栈顶元素)
栈的数组实现——初始化、入栈、出栈、清空栈
参考资料:《数据结构与算法分析——C语言描述》 P46

一. 栈的链表实现

StackLinkList.cpp

  1. /*
  2. 功能:栈的链表实现: 栈的初始化(创建||清空)、入栈、出栈(获取栈顶元素)
  3. Date: 2015/01/23
  4. Author : quinn
  5. */
  6. #include <iostream>
  7. #include <stdlib.h>
  8. using namespace std;
  9. typedef int Item;
  10. typedef struct Node Node;
  11. typedef Node* Stack;
  12. struct Node
  13. {
  14. Item item;
  15. Node* next;
  16. };
  17. void StackPop(Stack s);
  18. Stack StackInit(Stack s) //创建或清空(初始化)
  19. {
  20. if (s == NULL) //创建
  21. {
  22. s = (Stack)malloc(sizeof(*s));
  23. s->next = NULL;
  24. }
  25. else //清空
  26. while(s->next != NULL)
  27. StackPop(s);
  28. cout << "初始化成功!" <<endl;
  29. return s;
  30. }
  31. void StackPush(Stack s, Item item) //入栈
  32. {
  33. Node* tmpNode = (Node*)malloc(sizeof(*tmpNode));
  34. tmpNode->item = item;
  35. tmpNode->next = s->next;
  36. s->next = tmpNode;
  37. cout << "PUSH: " << item <<endl;
  38. }
  39. Item StackPopAndTop(Stack s) //出栈并返回栈顶元素值
  40. {
  41. if (s->next == NULL)
  42. {
  43. cout << "空栈,POPAndTop失败" <<endl;
  44. return -1; //返回-1作警告
  45. }
  46. else
  47. {
  48. Node* firstNode = s->next;
  49. s->next = s->next->next;
  50. return firstNode->item;
  51. }
  52. }
  53. void StackPop(Stack s) //出栈
  54. {
  55. if (s->next == NULL)
  56. {
  57. cout << "空栈,POP失败" <<endl;
  58. }
  59. else
  60. {
  61. Node* firstNode = s->next;
  62. s->next = s->next->next;
  63. free(firstNode);
  64. }
  65. }
  66. Item StackTop(Stack s) //仅获取栈顶元素
  67. {
  68. if (s->next == NULL)
  69. {
  70. cout << "空栈,获取栈顶元素失败" <<endl;
  71. }
  72. return (s->next)->item;
  73. }
  74. int main()
  75. {
  76. Stack s = NULL;
  77. s = StackInit(s);
  78. for(int i = 0; i < 10; i++)
  79. StackPush(s, i);
  80. for(int i = 0; i < 5; i++)
  81. cout << StackPopAndTop(s) <<endl;
  82. StackInit(s);
  83. cout << StackPopAndTop(s) <<endl;
  84. system("pause");
  85. return 0;
  86. }

运行结果:
此处输入图片的描述

二. 栈的数组实现

StackArray.cpp

  1. /*
  2. 功能:栈的数组实现——初始化、入栈、出栈、清空栈
  3. 注: 定义栈为空时,栈顶index为 -1;栈满时,栈顶index为栈的长度-1
  4. Date:2015/01/23
  5. Author : quinn
  6. */
  7. #include <iostream>
  8. #include "item.h"
  9. #define STACK_SIZE 10 //认为设定栈的长度为10
  10. #define FULL_STACK (STACK_SIZE - 1)
  11. #define EMPTY_STACK (-1)
  12. using namespace std;
  13. typedef struct stack stack;
  14. typedef int Item;
  15. struct stack
  16. {
  17. Item *stackItem;
  18. int stackTop;
  19. };
  20. void Error(const char* str) //异常时输出提示
  21. {
  22. cout << "Error: " << str << endl;
  23. }
  24. int IsFull(stack* s) //判断是否栈满
  25. {
  26. if (s->stackTop == FULL_STACK)
  27. return 1;
  28. else
  29. return 0;
  30. }
  31. int IsEmpty(stack* s) //判断是否空栈
  32. {
  33. if (s->stackTop == EMPTY_STACK)
  34. return 1;
  35. else
  36. return 0;
  37. }
  38. stack* StackInit( stack* s, int maxN) //初始化栈空间
  39. {
  40. s->stackTop = -1; //空栈初始化 Top = -1
  41. s->stackItem = (Item*)malloc(maxN * sizeof(Item)); //分配栈空间
  42. return s;
  43. }
  44. void StackMakeEmpty(stack* s) //清空栈
  45. {
  46. if (!IsEmpty(s))
  47. s->stackTop = EMPTY_STACK;
  48. }
  49. void StackPush(stack* s, Item item) //入栈
  50. {
  51. if (!IsFull(s))
  52. {
  53. s->stackItem[++(s->stackTop)] = item;
  54. cout << "PUSH: " << item <<endl;
  55. }
  56. else
  57. Error("栈已满,push失败");
  58. }
  59. Item StackPop(stack *s) //出栈
  60. {
  61. if (!IsEmpty(s))
  62. {
  63. return s->stackItem[(s->stackTop)--];
  64. }
  65. else
  66. Error("空栈,pop失败");
  67. }
  68. int main()
  69. {
  70. stack *s = (stack*)malloc(sizeof(*s));
  71. StackInit(s, STACK_SIZE);
  72. for (int i = 0; i < 11; i++)
  73. {
  74. StackPush(s, i);
  75. }
  76. cout << "POP: " << StackPop(s) << endl;
  77. cout << "POP: " << StackPop(s) << endl;
  78. StackPush(s, 20);
  79. cout << "POP: " << StackPop(s) << endl;
  80. StackMakeEmpty(s);
  81. StackPop(s);
  82. StackPush(s, 30);
  83. cout << "POP: " << StackPop(s) << endl;
  84. system("pause");
  85. return 0;
  86. }

运行结果:
此处输入图片的描述

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注