Advertisement
Guest User

sitva

a guest
Apr 19th, 2018
76
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 <cstring>
  3.  
  4. using namespace std;
  5. struct nod
  6. {
  7.     int x;
  8.     nod *adr;
  9. };
  10. nod *p, *prim;
  11. void push (nod *&prim, int nr)
  12. {
  13.     if (prim==NULL)
  14.     {
  15.         prim = new nod;
  16.         prim->x=nr;
  17.         prim->adr=NULL;
  18.     }
  19.     else
  20.     {
  21.         p = new nod;
  22.         p->x=nr;
  23.         p->adr=prim;
  24.         prim=p;
  25.     }
  26. }
  27. void pop (nod *&prim)
  28. {
  29.         p=prim;
  30.         prim=prim->adr;
  31.         delete p;
  32. }
  33. int top (nod *&prim)
  34. {
  35.     cout<<prim->x<<endl;;
  36. }
  37. void afisare (nod *&prim)
  38. {
  39.     p=prim;
  40.     while (p)
  41.     {
  42.         cout<<p->x<<endl;
  43.         p=p->adr;
  44.     }
  45. }
  46.  
  47. int main()
  48. {
  49.     int n, x;
  50.     cin>>n;
  51.     char c[100];
  52.     for (int i=0;i<n;++i)
  53.     {
  54.         cin>>c;
  55.         if (strcmp(c,"push")==0)
  56.             {
  57.                 cin>>x;
  58.                 push(prim,x);
  59.             }
  60.         if (strcmp(c,"pop")==0&&prim!=NULL)
  61.             pop(prim);
  62.         if (strcmp(c,"top")==0&&prim!=NULL)
  63.             top(prim);
  64.     }
  65.     afisare(prim);
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement