Advertisement
NAEGAKURE

dvostruko povezana lista pr 2

May 13th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. /* Nadopuniti program koji će raditi s dvostruko povezanom
  2. listom koja ima definiran lažni prvi čvor i k tome je kružno
  3. povezana.
  4. Korisniku se nudi zadani izbornik, svaku od mogućnosti
  5. realizirati u zasebnoj funkciji (funkcija za dodavanje
  6. elementa u listu na odgovarajuću poziciju tako da je lista
  7. sortirana, funkcija za brisanje zadanog elementa liste,
  8. funkcija za ispis liste). */
  9.  
  10. #include <iostream>
  11. #include <iomanip>
  12. using namespace std;
  13.  
  14. struct node
  15. {
  16.     int data;
  17.     node *pre;
  18.     node *post;
  19. };
  20.  
  21. void izbornik();
  22. void create(node*& head);
  23. void add(node *&head);
  24. void deleteNode(node *head);
  25. void write(node *head);
  26.  
  27. int main()
  28. {
  29.     izbornik();
  30.     cin.sync();
  31.     cin.get();
  32.     return 0;
  33. }
  34.  
  35. void izbornik()
  36. {
  37.     int odabir;
  38.     node *head=0;
  39.     do
  40.     {
  41.         cout<<"\n\n\nI Z B O R N I K\n";
  42.         cout<<"1 - Kreiranje liste (samo lazni cvor)\n";
  43.         cout<<"2 - Unos elemenata liste\n";
  44.         cout<<"3 - Brisanje zadanog elementa liste\n";
  45.         cout<<"4 - Ispis liste\n";
  46.         cout<<"0 - K R A J\n";
  47.         cout<<"\n\nUnesite Vas odabir...";
  48.         cin>>odabir;
  49.         switch(odabir)
  50.         {
  51.         case 1:
  52.             if (head==NULL) create(head);
  53.             break;
  54.         case 2:
  55.             if (head) add(head);
  56.             else cout<<"Unos nije moguc!!! Lazni cvor nije kreiran...\n";
  57.             break;
  58.         case 3:
  59.             if (head) deleteNode(head);
  60.             else cout<<"Brisanje nije moguce!!! Lazni cvor nije kreiran...\n";
  61.             break;
  62.         case 4:
  63.             if (head) write(head);
  64.             else cout<<"Lista je prazna...\n";
  65.             break;
  66.         case 0:
  67.             cout<<"\nKraj.\n";
  68.             /*
  69.             DEALOKACIJA
  70.             */
  71.             if (head)
  72.             {
  73.                 node *current=head->post, *tmp;
  74.                 while (current!=head)
  75.                 {
  76.                     tmp=current;
  77.                     current=current->post;
  78.                     delete tmp;
  79.                 }
  80.                 delete head;
  81.                 head=0;
  82.             }
  83.         }
  84.     }
  85.     while(odabir!=0);
  86. }
  87.  
  88. void create(node*& head)
  89. {
  90.     head = new node;
  91.     head->post = head;
  92.     head->pre = head;
  93.     cout<<"Kreiran je lazni node liste."<<endl;
  94. }
  95.  
  96. //element liste dodaje se na poziciju tako da lista bude sortirana
  97. void add(node *&head)
  98. {
  99.     int elt;
  100.     char x;
  101.     do
  102.     {
  103.         cout<<"Unesite element koji zelite dodati u listu...\n";
  104.         cin>>elt;
  105.         node *current=head->post, *newNode;
  106.         newNode=new node;
  107.         newNode->data=elt;
  108.         while (current!=head)
  109.         {
  110.             if (elt<current->data) break;
  111.             current=current->post;
  112.         }
  113.         newNode->post=current;
  114.         newNode->pre=current->pre;
  115.         current->pre->post=newNode;
  116.         current->pre=newNode;
  117.         cout<<"Zelite li dodati jos elemenata? (d/n) \n";
  118.         cin>>x;
  119.     }
  120.     while(x=='d');
  121. }
  122.  
  123. void write(node *head)
  124. {
  125.     node *current=head->post;
  126.     if (current==head)
  127.     {
  128.         cout<<"Lista je prazna...\n";
  129.         return;
  130.     }
  131.     while (current!=head)
  132.     {
  133.         cout<<setw(5)<<current->data;
  134.         current=current->post;
  135.     }
  136.     cout<<endl;
  137. }
  138.  
  139. void deleteNode(node *head)
  140. {
  141.     int elt;
  142.     cout<<"Unesite element koji zelite brisati...";
  143.     cin>>elt;
  144.     node *current=head->post;
  145.     while (current!=head && current->data<elt)
  146.         current=current->post;
  147.     if (current->data==elt)
  148.     {
  149.         current->pre->post=current->post;
  150.         current->post->pre=current->pre;
  151.         delete current;
  152.         current=0;
  153.     }
  154.     else cout<<"Element nije pronadjen...\n";
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement