当前位置:   article > 正文

数据结构 -- 栈的基本操作(入栈、出栈、取栈顶元素)

取栈顶元素

目录

栈的基本操作(顺序栈)

栈的基本操作(链栈)


栈的基本操作(顺序栈)

初始化栈:

  1. #define MAXSIZE 5
  2. typedef struct
  3. {
  4. int *top;
  5. int *base;
  6. int stacksize;
  7. }SqStack;
  8. void CreateStack(SqStack &S)
  9. {
  10. S.base=new int[MAXSIZE];
  11. if(S.base==NULL){
  12. cout<<"分配内存失败!";
  13. return ;
  14. }
  15. S.top=S.base;
  16. S.stacksize=MAXSIZE;
  17. }

入栈:

  1. void Push(SqStack &S,int e){
  2.         if(S.top-S.base==maxsize) { cout << "栈满!"; return ; }
  3.         *S.top=e;
  4.         S.top++;
  5. }

出栈:

  1. void Pop(SqStack &S)
  2. {
  3. if(S.top==S.base){
  4. cout<<"栈空"<<endl;
  5. return;
  6. }
  7. S.top--;
  8. }

取栈顶元素:

  1. int GetTop(SqStack &S)
  2. {
  3. if(S.top==S.base)
  4. {
  5. cout<<"栈空!"<<endl;
  6. return ;
  7. }
  8. return *(S.top--);
  9. }

完整代码:

  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4. #define maxsize 5
  5. typedef struct
  6. {
  7. int* base;
  8. int* top;
  9. int stactsize;
  10. }sqstack;
  11. //顺序栈的初始化
  12. void initstack(sqstack& s)
  13. {
  14. s.base = new int[maxsize];
  15. if (!s.base) {
  16. cout << "分配内存失败!" << endl;
  17. system("pause");
  18. return;
  19. }
  20. s.top = s.base;
  21. s.stactsize = maxsize;
  22. }
  23. //入栈
  24. void push(sqstack& s, int e)
  25. {
  26. if ((s.top - s.base)==maxsize) {
  27. cout << "栈满!" << endl;
  28. system("pause");
  29. return;
  30. }
  31. *s.top = e;
  32. s.top++;
  33. system("pause");
  34. }
  35. //出栈
  36. void pop(sqstack& s)
  37. {
  38. if (s.top == s.base) {
  39. cout << "栈空!" << endl;
  40. system("pause");
  41. return;
  42. }
  43. s.top--;
  44. system("pause");
  45. }
  46. int gettop(sqstack s)
  47. {
  48. if (s.top == s.base) {
  49. cout << "栈空!" << endl;
  50. system("pause");
  51. }
  52. return *(s.top - 1);
  53. }
  54. void menu(sqstack& s)
  55. {
  56. cout << "************************" << endl;
  57. cout << "| 1.入栈 |" << endl;
  58. cout << "| 2.出栈 |" << endl;
  59. cout << "| 3.取栈顶元素 |" << endl;
  60. cout << "| 4.退出 |" << endl;
  61. cout << "************************" << endl;
  62. cout << "input your choice(1~4):";
  63. int i;
  64. cin >> i;
  65. int e;
  66. int e1;
  67. switch (i) {
  68. case 1:
  69. cout << "请输入你要入栈的元素:";
  70. cin >> e;
  71. push(s, e);
  72. break;
  73. case 2:
  74. pop(s);
  75. break;
  76. case 3:
  77. e1 = gettop(s);
  78. if (e1 < 0) break;
  79. cout << "栈顶元素为:" << e1;
  80. system("pause");
  81. break;
  82. case 4:
  83. exit(0);
  84. break;
  85. default:
  86. cout << "非法输入!";
  87. break;
  88. }
  89. }
  90. //实现顺序结构栈的基本操作(初始化栈,入栈,出栈,获取栈顶元素)
  91. int main()
  92. {
  93. sqstack s;
  94. initstack(s);
  95. while (1) {
  96. menu(s);
  97. system("cls");
  98. }
  99. }

栈的基本操作(链栈)

初始化栈

  1. typedef struct StackNode
  2. {
  3. int data;
  4. struct StackNode *next;
  5. }StackNode,*LinkStack;
  6. void InitStack(LinkStack &S)
  7. {
  8. S=NULL;
  9. }

链栈的入栈

  1. void Push(LinkStack &S,int e)
  2. {
  3. LinkStack p=new StackNode;
  4. //或者 LinkStack p=(StackNode*)malloc(sizeof(StackNode));
  5. p->data=e;
  6. p->next=S;
  7. S=p;
  8. }

链栈的出栈

  1. void Pop(LinkStack &L,int &e)
  2. {
  3. if(S==NULL){
  4. cout<<"栈空";
  5. return ;
  6. }
  7. LinkStack p=S;
  8. e=p->data; //用e保存要出栈数据的信息
  9. S=S->next;
  10. delete p;
  11. //或者free(p);
  12. }

取链栈的栈顶元素

  1. int GetTop(LinkStack)
  2. {
  3. if(S==NULL){
  4. cout<<"栈空";
  5. }
  6. return S->data;
  7. }

完整代码

  1. //链栈的表示和实现
  2. typedef struct StackNode
  3. {
  4. int data;
  5. struct StackNode* next;
  6. }StackNode,*LinkStack;
  7. //链栈的初始化
  8. void InitStack(LinkStack& S)
  9. {
  10. S = NULL;
  11. }
  12. //链栈的入栈
  13. void Push(LinkStack& S, int e)
  14. {
  15. LinkStack p = new StackNode;
  16. p->data = e;
  17. p->next = S;
  18. S = p;
  19. }
  20. //链栈的出栈
  21. void Pop(LinkStack& S,int &e)
  22. {
  23. if (S == NULL) {
  24. cout << "栈空!" << endl;
  25. system("pause");
  26. return;
  27. }
  28. LinkStack p = S;
  29. e = p->data;
  30. S = S->next;
  31. delete p;
  32. }
  33. //取链栈的栈顶元素
  34. int GetTop(LinkStack S)
  35. {
  36. if (S == NULL) {
  37. cout << "栈空!" << endl;
  38. }
  39. return S->data;
  40. }
  41. //菜单
  42. void Mune(LinkStack& S)
  43. {
  44. int e;
  45. cout << "************************" << endl;
  46. cout << "| 1.入栈 |" << endl;
  47. cout << "| 2.出栈 |" << endl;
  48. cout << "| 3.取栈顶元素 |" << endl;
  49. cout << "| 4.退出 |" << endl;
  50. cout << "************************" << endl;
  51. cout << "input your choice(1~4):";
  52. int i;
  53. cin >> i;
  54. switch (i)
  55. {
  56. case 1:
  57. cout << "请输入你要入栈的元素:";
  58. cin >> e;
  59. Push(S, e);
  60. system("pause");
  61. break;
  62. case 2:
  63. Pop(S,e);
  64. if (e > 0) {
  65. cout << "你出栈的元素是:" << e << endl;
  66. system("pause");
  67. }
  68. break;
  69. case 3:
  70. cout << "栈顶元素为:" << GetTop(S) << endl;
  71. system("pause");
  72. break;
  73. case 4:
  74. exit(0);
  75. default:
  76. cout << "输入非法!";
  77. system("pause");
  78. break;
  79. }
  80. }
  81. //主函数
  82. int main()
  83. {
  84. LinkStack S = new StackNode;
  85. InitStack(S);
  86. while (1) {
  87. Mune(S);
  88. system("cls");
  89. }
  90. }

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

闽ICP备14008679号