Advertisement
Talar97

[AISD] Stos

Jan 30th, 2019
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct elem{
  7.     int dane;
  8.     elem* nast;
  9. };
  10.  
  11. void push(elem* &stos, int x){
  12.     elem* nowy = new elem;          //Tworzymy nowy element
  13.     nowy->dane = x;                 //Przypisujemy x do nowego elementu
  14.     nowy->nast = stos;              //Odniesienie do poprzedniego elementu na stosie
  15.     stos = nowy;                    //Od teraz wierzcholkiem stosu jest nowy
  16. }
  17.  
  18. int pop(elem* &stos){               //Chodzenie po stosie to tylko zdejmowanie i dodawanie el.
  19.     if(stos != NULL){               //Sprawdzamy czy stos w ogole istnieje
  20.         elem * stary = stos;        //Tworzymy sobie zmienna stary, do ktorej przypisujemy stos
  21.         int wynik = stos->dane;     //do wyniku przepisujemy dane z obecnego stosu
  22.         stos = stos->nast;          //stos od teraz jest nastepnym elemenetem
  23.         delete stary;               //Usuwamy stos
  24.         return wynik;               //Zwracamy wynik
  25.     }
  26. }
  27.  
  28. void removeStos(elem* &stos){       //Usuwanie to wywolywanie popa do poki stos jest rozny od null
  29.     while(stos != NULL){
  30.         cout << pop(stos) << " del" << endl;
  31.     }
  32. }
  33.  
  34. int topElem(elem* &stos){           //topElement bez usuwania (mozna by tez bylo zrobic popem i push
  35.     if(stos != NULL){               //do wyniku zapisujemy to co stos przechowuje w zmiennej dane
  36.         int wynik = stos->dane;
  37.         return wynik;
  38.     }
  39.     else { return -1; }
  40. }
  41.  
  42. bool isEmpty(elem* stos){           //Sprawdzamy czy stos == null
  43.     if(stos == NULL) return true;
  44.     else return false;
  45. }
  46.  
  47. void wyswietlStos(elem* stos){      //Wyswietlanie bez usuwania (fizycznie niemozliwe)
  48.     if(stos != NULL){               //Bo nie mozna chodzic po stosie bez zdejmowania i dodawania
  49.         elem* tmp = stos;           //Wartosci. Nie da sie tam zagladac.
  50.         while(tmp != NULL){
  51.             cout << tmp->dane << ", ";
  52.             tmp = tmp->nast;
  53.         }
  54.     }
  55.     else cout << "Stos nie istnieje";
  56. }
  57.  
  58. void odwrocStos(elem* &stos, elem* &docelowy){
  59.     if(stos != NULL){
  60.         while(stos != NULL){
  61.             int x = pop(stos);
  62.             push(docelowy, x);
  63.         }
  64.     }
  65. }
  66.  
  67. void przeniesStos(elem* &stos1, elem* &docelowy){
  68.     if(stos1 != NULL){
  69.         elem* tmp = NULL;
  70.         odwrocStos(stos1, tmp);
  71.         odwrocStos(tmp, docelowy);
  72.         delete tmp;
  73.     }
  74. }
  75.  
  76. int zliczElementy(elem* &stos){
  77.     if(stos != NULL){
  78.         int wynik = 0;
  79.         while(stos != NULL){
  80.             pop(stos);
  81.             wynik++;
  82.         }
  83.         return wynik;
  84.     }
  85. }
  86.  
  87. int main() {
  88.     elem * stos = NULL;
  89.    
  90.     /*
  91.     push(stos, 5);                                      //5
  92.     push(stos, 3);                                      //3,5
  93.     push(stos, 1);                                      //1,3,5
  94.     pop(stos);                                          //3, 5
  95.    
  96.     cout << "Top element: " << topElem(stos) << endl;   //3, bez usuniecia
  97.     cout << "Stos: " << endl;
  98.     wyswietlStos(stos);                                 //3,5
  99.     cout << "Usuwanie stosu: " << endl;
  100.     removeStos(stos);                                   //usuwanie wszystkiego
  101.     cout << "Czy pusty? " << isEmpty(stos) << endl;     //true
  102.     */
  103.    
  104.     elem* docelowy = NULL;
  105.     push(stos, 1);
  106.     push(stos, 2);
  107.     push(stos, 3);
  108.    
  109.     /*
  110.     cout << "Oryginalny stos: ";
  111.     wyswietlStos(stos);                 //3, 2, 1
  112.     odwrocStos(stos, docelowy);    
  113.     cout << endl << "Odwrocony stos: ";
  114.     wyswietlStos(docelowy);             //1, 2, 3
  115.    
  116.     cout << endl << "Elementy na stosie: " << zliczElementy(docelowy); //3
  117.      */
  118.    
  119.     cout << "Oryginalny stos: ";
  120.     wyswietlStos(stos);                 //3, 2, 1
  121.     przeniesStos(stos, docelowy);    
  122.     cout << endl << "Odwrocony stos: ";
  123.     wyswietlStos(docelowy);             //1, 2, 3
  124.    
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement