Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- using namespace std;
- struct elem{
- int dane;
- elem* nast;
- };
- void push(elem* &stos, int x){
- elem* nowy = new elem; //Tworzymy nowy element
- nowy->dane = x; //Przypisujemy x do nowego elementu
- nowy->nast = stos; //Odniesienie do poprzedniego elementu na stosie
- stos = nowy; //Od teraz wierzcholkiem stosu jest nowy
- }
- int pop(elem* &stos){ //Chodzenie po stosie to tylko zdejmowanie i dodawanie el.
- if(stos != NULL){ //Sprawdzamy czy stos w ogole istnieje
- elem * stary = stos; //Tworzymy sobie zmienna stary, do ktorej przypisujemy stos
- int wynik = stos->dane; //do wyniku przepisujemy dane z obecnego stosu
- stos = stos->nast; //stos od teraz jest nastepnym elemenetem
- delete stary; //Usuwamy stos
- return wynik; //Zwracamy wynik
- }
- }
- void removeStos(elem* &stos){ //Usuwanie to wywolywanie popa do poki stos jest rozny od null
- while(stos != NULL){
- cout << pop(stos) << " del" << endl;
- }
- }
- int topElem(elem* &stos){ //topElement bez usuwania (mozna by tez bylo zrobic popem i push
- if(stos != NULL){ //do wyniku zapisujemy to co stos przechowuje w zmiennej dane
- int wynik = stos->dane;
- return wynik;
- }
- else { return -1; }
- }
- bool isEmpty(elem* stos){ //Sprawdzamy czy stos == null
- if(stos == NULL) return true;
- else return false;
- }
- void wyswietlStos(elem* stos){ //Wyswietlanie bez usuwania (fizycznie niemozliwe)
- if(stos != NULL){ //Bo nie mozna chodzic po stosie bez zdejmowania i dodawania
- elem* tmp = stos; //Wartosci. Nie da sie tam zagladac.
- while(tmp != NULL){
- cout << tmp->dane << ", ";
- tmp = tmp->nast;
- }
- }
- else cout << "Stos nie istnieje";
- }
- void odwrocStos(elem* &stos, elem* &docelowy){
- if(stos != NULL){
- while(stos != NULL){
- int x = pop(stos);
- push(docelowy, x);
- }
- }
- }
- void przeniesStos(elem* &stos1, elem* &docelowy){
- if(stos1 != NULL){
- elem* tmp = NULL;
- odwrocStos(stos1, tmp);
- odwrocStos(tmp, docelowy);
- delete tmp;
- }
- }
- int zliczElementy(elem* &stos){
- if(stos != NULL){
- int wynik = 0;
- while(stos != NULL){
- pop(stos);
- wynik++;
- }
- return wynik;
- }
- }
- int main() {
- elem * stos = NULL;
- /*
- push(stos, 5); //5
- push(stos, 3); //3,5
- push(stos, 1); //1,3,5
- pop(stos); //3, 5
- cout << "Top element: " << topElem(stos) << endl; //3, bez usuniecia
- cout << "Stos: " << endl;
- wyswietlStos(stos); //3,5
- cout << "Usuwanie stosu: " << endl;
- removeStos(stos); //usuwanie wszystkiego
- cout << "Czy pusty? " << isEmpty(stos) << endl; //true
- */
- elem* docelowy = NULL;
- push(stos, 1);
- push(stos, 2);
- push(stos, 3);
- /*
- cout << "Oryginalny stos: ";
- wyswietlStos(stos); //3, 2, 1
- odwrocStos(stos, docelowy);
- cout << endl << "Odwrocony stos: ";
- wyswietlStos(docelowy); //1, 2, 3
- cout << endl << "Elementy na stosie: " << zliczElementy(docelowy); //3
- */
- cout << "Oryginalny stos: ";
- wyswietlStos(stos); //3, 2, 1
- przeniesStos(stos, docelowy);
- cout << endl << "Odwrocony stos: ";
- wyswietlStos(docelowy); //1, 2, 3
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement