Guest User

Untitled

a guest
Jan 7th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. struct LIFOElem
  7. {
  8.     int key;
  9.     LIFOElem *next;
  10. };
  11.  
  12. LIFOElem *top;
  13.  
  14. void zeruj()
  15. {
  16.     top=NULL;
  17. }
  18.  
  19. void wstaw(int key)
  20. {
  21.         cout<<"nowy element do stosu"<<endl;
  22.         LIFOElem *pnowy=new LIFOElem;
  23.         pnowy->key=key;
  24.         pnowy->next=top;
  25.         top = pnowy;
  26. }
  27.  
  28. LIFOElem* zwrocElement(int k,LIFOElem* top)
  29. {
  30.  
  31.     if(top!=NULL)
  32.     {
  33.        LIFOElem *top2=NULL,*ptr=top,*ptr2=top2;
  34.        while(ptr!=NULL)
  35.        {
  36.             if(ptr->key==k)
  37.             {
  38.                 cout<<"znaleziono klucz";
  39.                 return ptr;
  40.             }
  41.             else
  42.             {// to powoduje blad
  43.                 top2=ptr;
  44.                 top2->next=ptr2;
  45.                 ptr2=top2;
  46.                 ptr=ptr->next;
  47.             }
  48.        }
  49.     }
  50.  
  51.     cout<<"brak klucza";
  52.     return NULL;
  53. }
  54.  
  55. int main()
  56. {
  57.     zeruj();
  58.  
  59.     wstaw(1);
  60.     wstaw(2);
  61.     wstaw(3);
  62.     wstaw(4);
  63.  
  64.     int K=1;
  65.     zwrocElement(K,top);
  66.  
  67.     }
Advertisement
Add Comment
Please, Sign In to add comment