Advertisement
majczel23000

[AiSD] 4. Stos

Nov 29th, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. //=================
  5. //TABLICOWO
  6. //=================
  7.  
  8. const int N= 5;
  9. int sp = 0;
  10. int stos[N];
  11.  
  12. void pushTab(int key)
  13. {
  14.     if(sp<N)
  15.     {
  16.         stos[sp] = key;
  17.         sp++;
  18.         cout<<"[+]: "<<key<<endl;
  19.     }
  20.     else
  21.         cout<<"[+]: Stos pelny"<<endl;
  22. }
  23.  
  24. int popTab()
  25. {
  26.     int key;
  27.     if(sp>0)
  28.     {
  29.         sp--;
  30.         key = stos[sp];
  31.         cout<<"[-]: "<<key<<endl;
  32.     }
  33.     else
  34.         cout<<"[-]: Stos pusty"<<endl;
  35. }
  36.  
  37. void drukujTab()
  38. {
  39.     if(sp == 0)
  40.         cout<<"[STOS] Pusty"<<endl;
  41.     else
  42.     {
  43.         cout<<"[STOS] ";
  44.         for(int i = 0; i< sp; i++)
  45.             cout<< stos[i]<< " ";
  46.         cout<<endl;
  47.     }
  48. }
  49.  
  50. void usunTab()
  51. {
  52.     if(sp==0)
  53.         cout<<"[USUN] Stos pusty"<<endl;
  54.     else
  55.     {
  56.         cout<<"[USUN] Usunieto caly stos"<<endl;
  57.         for(int i = sp; i>0 ; i--)
  58.             sp--;
  59.     }
  60. }
  61.  
  62. //=================
  63. //DOWIAZANIOWO
  64. //=================
  65.  
  66. struct element
  67. {
  68.     int key;
  69.     element *next;
  70. };
  71.  
  72. element *head = 0;
  73.  
  74. void pushList(int key)
  75. {
  76.     element *n = new element;
  77.     n->key = key;
  78.     n->next = head;
  79.     head = n;
  80.     cout<<"[+]: "<< key<< endl;
  81. }
  82.  
  83. void popList()
  84. {
  85.     if(head == NULL)
  86.         cout<<"[-]: Stos pusty"<<endl;
  87.     else
  88.     {
  89.         cout<<"[-]: "<<head->key<<endl;
  90.         element *n = head;
  91.         head = head->next;
  92.         delete n;
  93.     }
  94. }
  95.  
  96. void drukujList()
  97. {
  98.     if(head==NULL)
  99.         cout<<"[STOS] Pusty"<<endl;
  100.     else
  101.     {
  102.         cout<<"[STOS]: ";
  103.         element *p = head;
  104.         while(p != NULL)
  105.         {
  106.             cout<<p->key<< " ";
  107.             p = p->next;
  108.         }
  109.         cout<<endl;
  110.     }
  111.  
  112. }
  113.  
  114. void usunList()
  115. {
  116.     if(head == NULL)
  117.         cout<<"[USUN] Stos pusty"<<endl;
  118.     else
  119.     {
  120.         element *p;
  121.         while(head!=0)
  122.         {
  123.             p = head->next;
  124.             delete head;
  125.             head = p;
  126.         }
  127.         cout<<"[USUN] Usunieto caly stos"<<endl;
  128.     }
  129. }
  130.  
  131. int main(){
  132.  
  133.     pushTab(5);
  134.     pushTab(4);
  135.     pushTab(-2);
  136.     pushTab(1);
  137.     pushTab(8);
  138.     drukujTab();
  139.     usunTab();
  140.     pushTab(1);
  141.     pushTab(6);
  142.     pushTab(5);
  143.     pushTab(4);
  144.     pushTab(1);
  145.     popTab();
  146.     pushTab(1);
  147.     pushTab(8);
  148.     drukujTab();
  149.      usunTab();
  150.     drukujTab();
  151.     cout<<endl<<endl<<endl;
  152.     //=========
  153.  
  154.     pushList(5);
  155.     pushList(-2);
  156.     pushList(10);
  157.     pushList(9);
  158.     pushList(4);
  159.     pushList(5);
  160.     drukujList();
  161.     popList();
  162.     popList();
  163.     drukujList();
  164.     usunList();
  165.     drukujList();
  166.     pushList(10);
  167.     pushList(9);
  168.     drukujList();
  169.     popList();
  170.     drukujList();
  171.  
  172.     return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement