Advertisement
przemo539

STOS TABLICOWY

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