Advertisement
tonibiro

ASD lab6 prob4

Nov 8th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct nod
  6. {
  7.     int info;
  8.     nod *ante, *urm;
  9. };
  10.  
  11. struct lista
  12. {
  13.     nod *prim, *ultim;
  14. };
  15.  
  16. void init(lista &c)
  17. {
  18.     c.prim = c.ultim = NULL;
  19. }
  20.  
  21. void push(lista &c, nod *&start, int x)
  22. {
  23.     nod *p = new nod;
  24.     p->info = x;
  25.     p->ante = p->urm = NULL;
  26.     if(c.prim == NULL)
  27.     {
  28.         c.prim = c.ultim = p;
  29.         start->urm = p;
  30.         p->ante = start;
  31.         p->urm = start;
  32.     }
  33.     else
  34.     {
  35.         c.ultim->urm = p;
  36.         p->ante = c.ultim;
  37.         p->urm = start;
  38.         c.ultim = p;
  39.     }
  40. }
  41.  
  42. void pop(lista c, nod *start, int x)
  43. {
  44.     nod *r = start;
  45.     r = r->urm;
  46.     while(r != start && x != r->info)
  47.         r = r->urm;
  48.     if(r != start)
  49.     {
  50.         r->ante->urm = r->urm;
  51.         r->urm->ante = r->ante;
  52.         delete r;
  53.     }
  54.  
  55. }
  56.  
  57. void afis_lista(lista c, nod *start)
  58. {
  59.     nod *r = start;
  60.     r = r->urm;
  61.     while(r != start)
  62.     {
  63.         cout << r->info << ' ';
  64.         r = r->urm;
  65.     }
  66.     cout << "\n";
  67. }
  68.  
  69. int main()
  70. {
  71.     lista c;
  72.     init(c);
  73.  
  74.     nod *start = new nod;
  75.     start->urm = NULL;
  76.     start->urm = c.prim;
  77.     push(c, start, 3);
  78.     push(c, start, 4);
  79.     push(c, start, 5);
  80.     push(c, start, 9);
  81.     afis_lista(c, start);
  82.     pop(c, start, 3);
  83.     pop(c, start, 5);
  84.     pop(c, start, 4);
  85.     afis_lista(c, start);
  86.  
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement