Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- struct nod
- {
- int x;
- nod *adr;
- };
- nod *p, *prim;
- void push (nod *&prim, int nr)
- {
- if (prim==NULL)
- {
- prim = new nod;
- prim->x=nr;
- prim->adr=NULL;
- }
- else
- {
- p = new nod;
- p->x=nr;
- p->adr=prim;
- prim=p;
- }
- }
- void pop (nod *&prim)
- {
- p=prim;
- prim=prim->adr;
- delete p;
- }
- int top (nod *&prim)
- {
- cout<<prim->x<<endl;;
- }
- void afisare (nod *&prim)
- {
- p=prim;
- while (p)
- {
- cout<<p->x<<endl;
- p=p->adr;
- }
- }
- int main()
- {
- int n, x;
- cin>>n;
- char c[100];
- for (int i=0;i<n;++i)
- {
- cin>>c;
- if (strcmp(c,"push")==0)
- {
- cin>>x;
- push(prim,x);
- }
- if (strcmp(c,"pop")==0&&prim!=NULL)
- pop(prim);
- if (strcmp(c,"top")==0&&prim!=NULL)
- top(prim);
- }
- afisare(prim);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement