Advertisement
NAEGAKURE

povezane liste pr 1

Apr 26th, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. /*
  2. Napišimo program u kojem se dinamički kreira i
  3. ispisuje povezana lista cijelih brojeva. Brojeve unosi
  4. korisnik sve dok se ne učita 0. Unos brojeva i
  5. kreiranje liste realizirajte u funkciji main(). Podaci
  6. neka se spremaju u listu obrnutim redoslijedom,
  7. tako da 0 bude na početku liste. Napišite i pozovite
  8. funkciju za ispis elemenata liste.
  9. */
  10.  
  11. #include <iostream>
  12. using namespace std;
  13.  
  14. struct node
  15. {
  16.     int data;
  17.     node *link;
  18.  
  19. };
  20.  
  21. void write(node *head);
  22.  
  23. int main()
  24. {
  25.     int x;
  26.     node *head=0;
  27.     do{
  28.             cin>>x;
  29.             node *current=new node;
  30.             current->data=x;
  31.             current->link=head; //pokazuje na pocetak liste
  32.             head=current;
  33.  
  34.     }while(x!=0);
  35.  
  36.     write(head);
  37.  
  38.  
  39.     return 0;
  40. }
  41.  
  42. void write(node *head)
  43. {
  44.     while(head) // MOŽE I: while(head!=0)
  45.     {
  46.         cout<<head->data<<" ";
  47.         head=head->link;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement