Advertisement
NAEGAKURE

stog

Jun 6th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include <iostream>
  2. #include<iomanip>
  3. #include<cstdlib>
  4. using namespace std;
  5.  
  6. struct node
  7. {
  8.     node *link;
  9.     int data;
  10. };
  11.  
  12.  
  13. //DEKLARACIJE FUNKCIJA
  14. bool stackEmpty(node *head); // funkcija vraæa vrijednost 'true' ako je stog prazan
  15. void push(node *&head, int n); // funkcija stavlja novi element na stog
  16. int pop(node *&head); // funkcija brise element sa stoga i vraæa njegov podatkovni dio
  17. void write(node *head); // funkcija ispisuje elemente koji su na stogu
  18. void deleteStack(node *&head); // funkcija brise sve elemente sa stoga
  19.  
  20. int main(){
  21.  
  22.     node *s = NULL;
  23.     int elt=1;
  24.     char odabir;
  25.     do
  26.     {
  27.         cout<<"\n\nI-Z-B-O-R-N-I-K"<<endl;
  28.         cout<<"\n1 - Unos novog primjerka";
  29.         cout<<"\n2 - Preuzimanje artikla";
  30.         cout<<"\n3 - Ispis trenutnog stanja skladista";
  31.         cout<<"\n0 - Kraj";
  32.         cout<<"\n\nUnesite Vas odabir...\n";
  33.         cin>>odabir;
  34.         switch(odabir)
  35.         {
  36.         case '1':
  37.             push(s,elt);
  38.             elt++;
  39.             break;
  40.         case '2':
  41.             if(!stackEmpty(s))
  42.             {
  43.                 cout<<"Preuzet je artikl pod rednim brojem..."<<pop(s)<<endl;
  44.                 elt--;
  45.             }
  46.             else cout<<"Skladiste je prazno. Nemate sto preuzeti"<<endl;
  47.             break;
  48.         case '3':
  49.             if(!stackEmpty(s)) write(s);
  50.             else cout<<"Skladiste je prazno."<<endl;
  51.             break;
  52.         case '0':
  53.             cout<<"Kraj programa.\n";
  54.         }
  55.     }
  56.     while(odabir!='0');
  57.     //DEALOKACIJA
  58.     deleteStack(s);
  59.     s=NULL;
  60.     return 0;
  61. }
  62.  
  63. bool stackEmpty(node *head)
  64. {
  65.     return(head==NULL);
  66. }
  67.  
  68. void push(node *&head, int n)
  69. {
  70.     node *newNode = new node;
  71.     newNode->data=n;
  72.     newNode->link=head;
  73.     head=newNode;
  74. }
  75. int pop(node *&head)
  76. {
  77.     int n=head->data;
  78.     node *current=head;
  79.     current=head;
  80.     head=head->link;
  81.     delete current;
  82.     return n;
  83. }
  84.  
  85. void write(node *head)
  86. {
  87.     node *current=head;
  88.     while(current!=NULL)
  89.     {
  90.         cout<<current->data;
  91.         current=current->link;
  92.     }
  93. }
  94. void deleteStack(node *&head)
  95. {
  96.     node *current=head;
  97.     while(current!=NULL)
  98.     {
  99.         current=current->link;
  100.         delete current;
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement