Advertisement
nicuvlad76

Untitled

Nov 13th, 2023
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /**
  4.  Liste liniare simplu inlantuite=LLSI
  5.  
  6.  1. adaugare la final =adaugare
  7.  2. adaugare in fata primului nod = adaugareInainte
  8.  3. afisarea liste =afisare
  9. */
  10. struct nod
  11. {
  12.   int info;
  13.   nod *urm;
  14. };
  15. nod *p;
  16.  
  17. void adaugare(nod * & p , int x)
  18. {
  19.   ///adaugare nod la final
  20.   nod* q=new nod;
  21.   q->info=x;
  22.   q->urm=NULL;
  23.  
  24.   if(p==0)///lista vida
  25.     p=q;
  26.   else
  27.   {
  28.       nod* u;
  29.       for(u=p; u->urm!=NULL; u=u->urm);
  30.       u->urm=q;///leg elemente existe de noul nod
  31.   }
  32. }
  33. void adaugareInainte(nod * & p , int x)
  34. {
  35.    nod*q=new nod;
  36.    q->info=x;
  37.    q->urm=p;
  38.    p=q;
  39. }
  40. void Creare(nod*&p)
  41. {
  42.    ifstream fin("lista.in");
  43.    int x;
  44.    while(fin>>x)
  45.         ///adaugare(p,x);
  46.         adaugareInainte(p,x);
  47.     fin.close();
  48. }
  49.  
  50. void afisare(nod*p)
  51. {
  52.     for(nod *q=p;q; q=q->urm)
  53.         cout<<q->info<<" ";
  54.     cout<<"\n";
  55. }
  56.  
  57.  
  58. int main()
  59. {
  60.   Creare(p);
  61.   afisare(p);
  62.   return 0;
  63. }
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement