[关闭]
@bintou 2017-12-07T02:43:37.000000Z 字数 4246 阅读 1672

Stack in C/C++

Source Code


修改了一下代码,分成三个文件:stack.h 、stack.cpp和调用stack.cpp的main.cpp文件.

练习:使用Linked list实现一种动态Stack,即,Stack的Size可以根据应用的需求增大,而无需预先定死。

stack.h

  1. /* C++ head file to define a class of stack.
  2. Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.
  3. Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.
  4. Peek or Top: Returns top element of stack.
  5. isEmpty: Returns true if stack is empty, else fals.
  6. */
  7. #define MAX 1000
  8. class Stack
  9. {
  10. int top;
  11. public:
  12. int a[MAX]; //Maximum size of Stack
  13. Stack() { top = -1; }
  14. bool push(int x);
  15. int pop();
  16. int peek();
  17. bool isEmpty();
  18. };

stack.cpp

  1. /* C++ program to implement a basic stack. */
  2. #include <stack_oo.h>
  3. bool Stack::push(int x)
  4. {
  5. if (top >= MAX)
  6. {
  7. //cout << "Stack Overflow";
  8. return false;
  9. }
  10. else
  11. {
  12. a[++top] = x;
  13. return true;
  14. }
  15. }
  16. int Stack::pop()
  17. {
  18. if (top < 0)
  19. {
  20. //cout << "Stack Underflow";
  21. return 0;
  22. }
  23. else
  24. {
  25. int x = a[top--];
  26. return x;
  27. }
  28. }
  29. int Stack::peek()
  30. {
  31. if (top < 0)
  32. {
  33. //cout << "Stack Underflow";
  34. return -1;
  35. }
  36. else
  37. {
  38. return a[top];
  39. }
  40. }
  41. bool Stack::isEmpty()
  42. {
  43. return (top < 0);
  44. }

main.cpp

  1. /* C++ program to test basic stack operations */
  2. #include <stack_oo.h>
  3. #include <iostream>
  4. int main()
  5. {
  6. Stack s;
  7. s.push(10);
  8. std::cout << " Push a value to stack\n";
  9. s.push(20);
  10. std::cout << " Push a value to stack\n";
  11. s.push(30);
  12. std::cout << " Push a value to stack\n";
  13. std::cout << s.pop() << " Popped from stack\n";
  14. return 0;
  15. }

老文件

  1. // C program for array implementation of stack
  2. /*
  3. Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.
  4. Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.
  5. Peek or Top: Returns top element of stack.
  6. isEmpty: Returns true if stack is empty, else fals.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <limits.h>
  11. // A structure to represent a stack
  12. struct Stack
  13. {
  14. int top;
  15. unsigned capacity;
  16. int* array;
  17. };
  18. // function to create a stack of given capacity. It initializes size of
  19. // stack as 0
  20. struct Stack* createStack(unsigned capacity)
  21. {
  22. struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
  23. stack->capacity = capacity;
  24. stack->top = -1;
  25. stack->array = (int*) malloc(stack->capacity * sizeof(int));
  26. return stack;
  27. }
  28. // Stack is full when top is equal to the last index
  29. int isFull(struct Stack* stack)
  30. { return stack->top == stack->capacity - 1; }
  31. // Stack is empty when top is equal to -1
  32. int isEmpty(struct Stack* stack)
  33. { return stack->top == -1; }
  34. // Function to add an item to stack. It increases top by 1
  35. void push(struct Stack* stack, int item)
  36. {
  37. if (isFull(stack))
  38. return;
  39. stack->array[++stack->top] = item;
  40. printf("%d pushed to stack\n", item);
  41. }
  42. // Function to remove an item from stack. It decreases top by 1
  43. int pop(struct Stack* stack)
  44. {
  45. if (isEmpty(stack))
  46. return INT_MIN;
  47. return stack->array[stack->top--];
  48. }
  49. // Driver program to test above functions
  50. int main()
  51. {
  52. struct Stack* stack = createStack(100);
  53. push(stack, 10);
  54. push(stack, 20);
  55. push(stack, 30);
  56. printf("%d popped from stack\n", pop(stack));
  57. return 0;
  58. }
  1. /* C++ program to implement basic stack
  2. operations */
  3. /*
  4. Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.
  5. Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.
  6. Peek or Top: Returns top element of stack.
  7. isEmpty: Returns true if stack is empty, else fals.
  8. */
  9. #include<bits/stdc++.h>
  10. using namespace std;
  11. #define MAX 1000
  12. class Stack
  13. {
  14. int top;
  15. public:
  16. int a[MAX]; //Maximum size of Stack
  17. Stack() { top = -1; }
  18. bool push(int x);
  19. int pop();
  20. bool isEmpty();
  21. };
  22. bool Stack::push(int x)
  23. {
  24. if (top >= MAX)
  25. {
  26. cout << "Stack Overflow";
  27. return false;
  28. }
  29. else
  30. {
  31. a[++top] = x;
  32. return true;
  33. }
  34. }
  35. int Stack::pop()
  36. {
  37. if (top < 0)
  38. {
  39. cout << "Stack Underflow";
  40. return 0;
  41. }
  42. else
  43. {
  44. int x = a[top--];
  45. return x;
  46. }
  47. }
  48. bool Stack::isEmpty()
  49. {
  50. return (top < 0);
  51. }
  52. // Driver program to test above functions
  53. int main()
  54. {
  55. struct Stack s;
  56. s.push(10);
  57. s.push(20);
  58. s.push(30);
  59. cout << s.pop() << " Popped from stack\n";
  60. return 0;
  61. }
  1. /*
  2. The functions associated with stack are:
  3. empty() – Returns whether the stack is empty
  4. size() – Returns the size of the stack
  5. top() – Returns a reference to the top most element of the stack
  6. push(g) – Adds the element ‘g’ at the top of the stack
  7. pop() – Deletes the top most element of the stack
  8. */
  9. #include <iostream>
  10. #include <stack>
  11. using namespace std;
  12. void showstack(stack <int> gq)
  13. {
  14. stack <int> g = gq;
  15. while (!g.empty())
  16. {
  17. cout << '\t' << g.top();
  18. g.pop();
  19. }
  20. cout << '\n';
  21. }
  22. int main ()
  23. {
  24. stack <int> gquiz;
  25. gquiz.push(10);
  26. gquiz.push(30);
  27. gquiz.push(20);
  28. gquiz.push(5);
  29. gquiz.push(1);
  30. cout << "The stack gquiz is : ";
  31. showstack(gquiz);
  32. cout << "\ngquiz.size() : " << gquiz.size();
  33. cout << "\ngquiz.top() : " << gquiz.top();
  34. cout << "\ngquiz.pop() : ";
  35. gquiz.pop();
  36. showstack(gquiz);
  37. return 0;
  38. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注