Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4. #include <cstdio>
  5. struct el_LIFO
  6. {
  7.     int x;
  8.     struct el_LIFO* nast;
  9. };
  10.  
  11.  
  12. struct el_LIFO* push(struct el_LIFO *top, int el)
  13. {
  14.     struct el_LIFO * new_el_LIFO=(struct el_LIFO*)malloc(sizeof(struct el_LIFO));
  15.     new_el_LIFO->x=el;
  16.     new_el_LIFO->nast=top;
  17.     return new_el_LIFO;
  18. }
  19.  
  20. struct el_LIFO* pop(struct el_LIFO *top, int *el)
  21. {
  22.   struct el_LIFO* temp;
  23.   if(top!=NULL)
  24.   {
  25.       *el=top->x;
  26.       temp = top-> nast;
  27.       free(top);
  28.       return temp;
  29.   }
  30.   else {return NULL;}
  31. }
  32.  
  33. struct el_LIFO* display(struct el_LIFO *top)
  34. {
  35.  
  36.     if(top==NULL)
  37.     std::printf("Brak elementów\n");
  38.     else
  39.         do{
  40.             std::printf("%5d\n",top->x);
  41.             top=top->nast;
  42.         }while (top!=NULL);
  43.  
  44. }
  45. struct el_LIFO cleaning(struct el_LIFO *top)
  46. {
  47.      if (top==NULL) return ;
  48.      stack *e;
  49.      while(top)
  50.      {
  51.          e = (top)->nast;
  52.          delete top;
  53.          top = e;
  54.      }
  55. }
  56. int top_el(struct el_LIFO *top, int *el)
  57. {
  58.     if(top!=NULL)
  59.     {
  60.         *el=top->x;
  61.         return 1;
  62.     }
  63.     else{return 0;}
  64. }
  65.  
  66. int main()
  67. {
  68.          struct el_LIFO *top_LIFO=NULL;
  69. for(;;)
  70. {   int choose;
  71.     std::cout<<"MENU"<<std::endl;
  72.     std::cout<<"1. Stwórz stos"<<std::endl;
  73.     std::cout<<"2. Dodaj element na stos"<<std::endl;
  74.     std::cout<<"3. zdejmij element ze stosu"<<std::endl;
  75.     std::cout<<"4. Pokaż wierzchołek stosu"<<std::endl;
  76.     std::cout<<"5. Pokaż stos"<<std::endl;
  77.     std::cout<<"6. Koniec programu"<<std::endl;
  78.     std::cin>>choose;
  79.  
  80.     switch(choose)
  81.     {
  82.         case 1:
  83.  
  84.         break;
  85.         case 2:
  86.             int toTop;
  87.             std::cout<<"Podaj liczbe ktora chcesz wrzucic na szczyt" <<std::endl;
  88.             std::cin>>toTop;
  89.             top_LIFO=push(top_LIFO,toTop);
  90.  
  91.         break;
  92.         case 3:
  93.             if(top_LIFO!=0)
  94.             {
  95.             int toPop,d;
  96.             top_LIFO=pop(top_LIFO,&d);
  97.             std::cout<< "usunieto: "<<d<<std::endl;
  98.             system("PAUSE");
  99.             }
  100.         break;
  101.         case 4:
  102.             int t;
  103.             if(top_el(top_LIFO,&t))std::cout<<"wierzcholek: "<< top_LIFO->x<<std::endl;
  104.             else std::cout<<"Stos pusty."<<std::endl;
  105.             system("PAUSE");
  106.         break;
  107.         case 5:
  108.             display(top_LIFO);
  109.             system("PAUSE");
  110.         break;
  111.         case 6:
  112.             return 0;
  113.         break;
  114.  
  115.     }
  116.     system("CLS");
  117. }
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement