Advertisement
NAEGAKURE

povezane liste pr 3

Apr 26th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. /*Sada napravimo modifikaciju prethodnog primjera
  2. tako da se podaci spremaju redoslijedom kojim se
  3. unose (0 je na kraju liste).*/
  4.  
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. struct node
  9. {
  10.     int data;
  11.     node *link;
  12.  
  13. };
  14.  
  15. void write(node *head);
  16. void input(node *&head, int elt);
  17.  
  18. int main()
  19. {
  20.     int x;
  21.     node *head=0;
  22.     do{
  23.             cin>>x;
  24.             input(head,x);
  25.  
  26.     }while(x!=0);
  27.  
  28.     write(head);
  29.  
  30.  
  31.     return 0;
  32. }
  33.  
  34. void write(node *head)
  35. {
  36.     while(head) // MOŽE I: while(head!=0)
  37.     {
  38.         cout<<head->data<<" ";
  39.         head=head->link;
  40.     }
  41. }
  42.  
  43. void input(node *&head, int elt)
  44. {
  45.     node *current=new node;
  46.     current->data=elt;
  47.     current->link=0;
  48.  
  49.     if(head==0)
  50.     {
  51.         head=current;
  52.     }
  53.     else
  54.     {
  55.         node *temp=head;
  56.         while(temp->link)
  57.             temp=temp->link;
  58.         temp->link=current;
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement