Advertisement
Dani_info

Lista dublu inlantuita

Oct 28th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct nod{
  6.     int info;
  7.     nod *anterior;
  8.     nod *urmator;
  9. };
  10.  
  11. nod *creare (nod* &ultim){
  12.     nod *prim=new nod;
  13.     int n;
  14.     cin>>n;
  15.     if (n==0)
  16.         return 0;
  17.     else{
  18.         prim->info=n;
  19.         prim->anterior=NULL;
  20.         nod *p=prim;
  21.         cin>>n;
  22.         while (n){
  23.             nod *q=new nod;
  24.             q->info=n;
  25.             q->anterior=p;
  26.             p->urmator=q;
  27.             p=q;
  28.             cin>>n;
  29.         }
  30.         p->urmator=NULL;
  31.         ultim=p;
  32.         return prim;
  33.     }
  34. }
  35.  
  36. void parcurgere_normal(nod *p){
  37.     while (p){
  38.         cout<<p->info<<endl;
  39.         p=p->urmator;
  40.     }
  41. }
  42. void parcurgere_invers(nod *p){
  43.     while (p){
  44.         cout<<p->info<<endl;
  45.         p=p->anterior;
  46.     }
  47. }
  48.  
  49. int main()
  50. {
  51.     nod *ultim, *prim;
  52.     prim=creare(ultim);
  53.     cout<<"Lista parcursa normal este"<<endl;
  54.     parcurgere_normal(prim);
  55.     cout<<endl;
  56.     cout<<"Lista parcursa invers este"<<endl;
  57.     parcurgere_invers (ultim);
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement