Advertisement
Guest User

Stack

a guest
Mar 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #define STACK_ERR -1000
  3. using namespace std;
  4.  
  5. struct stos
  6. {
  7.     int liczba;
  8.     stos *next;
  9. };
  10.  
  11. void init(stos *head)
  12. {
  13.     head->next = NULL;
  14. }
  15.  
  16. void Push(int x, stos *l)
  17. {
  18.     stos *nowy = new stos;
  19.     nowy->liczba = x;
  20.     nowy->next = l->next;
  21.     l->next = nowy;
  22. }
  23.  
  24. int Pop(stos *l)
  25. {
  26.     if (l->next == nullptr){
  27.         cout << "Stos jest pusty." << endl;
  28.         return STACK_ERR;
  29.     }
  30.     else
  31.     {
  32.         int el = l->next->liczba;
  33.         void* x = l->next;
  34.         l->next = l->next->next;
  35.         delete x;
  36.         return el;
  37.     }
  38. }
  39.  
  40. void Print(stos *h)
  41. {
  42.     h = h->next;
  43.     while (h != NULL)
  44.     {
  45.         cout << h->liczba << " ";
  46.         h = h->next;
  47.     }
  48.     cout << endl;
  49. }
  50.  
  51. int main()
  52. {
  53.     stos stack;
  54.     init(&stack);
  55.     for (int i = 0; i < 10; i++)
  56.     {
  57.         Push(i, &stack);
  58.     }
  59.     Print(&stack);
  60.     for (int i = 0; i < 4; i++)
  61.     {
  62.         Pop(&stack);
  63.     }
  64.     Print(&stack);
  65.     system("pause");
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement