NAEGAKURE

povezane liste 1

Apr 26th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. /* Napišimo program koji korisniku nudi izbornik u
  2. kojem može odabrati želi li unijeti element u listu,
  3. ispisati je ili brisati određeni element. Elementi se u
  4. listu dodaju na odgovarajuću poziciju tako da je lista u
  5. svakom trenutku sortirana uzlazno. */
  6.  
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. struct node
  11. {
  12.     int data;
  13.     node *link;
  14.  
  15. };
  16.  
  17.  
  18. void write(node *head);
  19. void input(node *&head, int elt);
  20. void deleteNode(node *&head, int elt);
  21.  
  22. int main()
  23. {
  24.     int izb, x;
  25.     node *head=0;
  26.  
  27.  
  28.     do
  29.     {
  30.  
  31.         cout<<"\n1. unos elementa\n2. ispis elementa\n3. brisanje\n0. kraj"<<endl;
  32.         cin>>izb;
  33.  
  34.  
  35.         switch(izb)
  36.         {
  37.  
  38.             case 1:
  39.                 cout<<"Unesite element u listu."<<endl;
  40.                 cin>>x;
  41.                 input(head,x);
  42.                 break;
  43.  
  44.             case 2:
  45.                 write(head);
  46.                 break;
  47.  
  48.             case 3:
  49.                 cout<<"Unesite element za brisanje."<<endl;
  50.                 cin>>x;
  51.                 deleteNode(head,x);
  52.                 break;
  53.  
  54.             case 0:
  55.                 cout<<"Kraj."<<endl;
  56.         }
  57.  
  58.     }while(izb!=0);
  59.  
  60.     // DEALOKACIJA
  61.  
  62.     node *temp=head, *eraseNode;
  63.  
  64.     while(temp)
  65.     {
  66.         eraseNode=temp;
  67.         temp=temp->link;
  68.         delete eraseNote;
  69.  
  70.     }
  71.  
  72.     head=0;
  73.  
  74.  
  75.     return 0;
  76. }
  77.  
  78. void write(node *head)
  79. {
  80.     while(head) // MOŽE I: while(head!=0)
  81.     {
  82.         cout<<head->data<<" ";
  83.         head=head->link;
  84.  
  85.     }
  86. }
  87.  
  88. void input(node *&head, int elt)
  89. {
  90.     node *current=new node;
  91.     current->data=elt;
  92.     current->link=0;
  93.     node *temp=head, *pre=0;
  94.     while(temp && temp->data<elt)
  95.     {
  96.         pre=temp;
  97.         temp=temp->link;
  98.  
  99.     }
  100.     if(pre) //if(pre!=0)
  101.     {
  102.         current->link=temp;
  103.         pre->link=current;
  104.     }
  105.     else {
  106.         current->link = head;
  107.         head=current;
  108.     }
  109. }
  110.  
  111. void deleteNode(node *&head, int elt)
  112. {
  113.     node *temp=head, *pre=0;
  114.     while(temp)
  115.     {
  116.         if(temp->data==elt)
  117.             break;
  118.  
  119.         pre=temp;
  120.         temp=temp->link;
  121.  
  122.  
  123.     }
  124.  
  125.     if(temp)
  126.     {
  127.         if(pre==0)
  128.             head=head->link;
  129.         else
  130.             pre->link=temp->link;
  131.         delete temp;
  132.         temp=0;
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment