Advertisement
szmelu

stos tablicowy

Mar 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. bool isfull(int top, int size)
  4. {
  5. return ((top+1)==size);
  6. }
  7. bool isempty(int top)
  8. {
  9. return (top==-1);
  10. }
  11. bool push(int &top, int *stack, int size, int x)
  12. {
  13. if(isfull(top, size))
  14. return 0;
  15. top++;
  16. stack[top]=x;
  17. return 1;
  18. }
  19. bool pop(int &top, int *stack, int &y)
  20. {
  21. if(isempty(top))
  22. return 0;
  23. y=stack[top];
  24. top--;
  25. return 1;
  26. }
  27. void showtop(int *stack, int top)
  28. {
  29. if(isempty(top))
  30. cout<<"Pusty"<<endl;
  31. else
  32. cout<<"top: "<<stack[top]<<endl;
  33. }
  34.  
  35.  
  36.  
  37.  
  38. int main()
  39. {
  40. int size=5;
  41. int stack[5];
  42. int top=-1;
  43. int y=0;
  44. push(top, stack, size, 1);
  45. showtop(stack, top);
  46. push(top, stack, size, 2);
  47. showtop(stack, top);
  48. push(top, stack, size, 3);
  49. showtop(stack, top);
  50. push(top, stack, size, 4);
  51. showtop(stack, top);
  52. push(top, stack, size, 5);
  53. showtop(stack, top);
  54. push(top, stack, size, 6);
  55. pop(top, stack, y);
  56. cout<<y<<endl;
  57. pop(top, stack, y);
  58. cout<<y<<endl;
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. system("pause");
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement