Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct nod{
  6.     int info;
  7.     nod *urm;}*prim,*ultim;
  8.  
  9. void adaugare(int n){
  10.     while(n)
  11.     {
  12.         nod *p=new nod;
  13.         ultim->urm=p;
  14.         cout<<"Dati o valoare: ";
  15.         ultim=p;
  16.         p->urm=ultim;
  17.         cin>>ultim->info;
  18.         n--;
  19.         ultim->urm=NULL;
  20.     }
  21. }
  22.  
  23. void afisare(){
  24.     nod *p=prim;
  25.     while(p!=NULL)
  26.     {
  27.         cout<<p->info<<" ";
  28.         p=p->urm;
  29.     }
  30. }
  31.  
  32. int cautare(int x){
  33.     nod *p=new nod;
  34.     p=prim;
  35.     while(p!=NULL){
  36.         if(p->info==x)
  37.             return 1;
  38.         p=p->urm;}
  39.     return 0;
  40. }
  41.  
  42. int stergere(int x){
  43.     nod *p=prim;
  44.     nod *q;
  45.     if(p->info==x){
  46.         prim=p->urm;
  47.         q=p;
  48.         delete q;
  49.         return 0;}
  50.     while(p->urm!=NULL){
  51.         if(p->urm->info==x){
  52.             q=p->urm;
  53.             p->urm=p->urm->urm;
  54.             delete q;
  55.             return 0;}
  56.         q=p;
  57.         p=p->urm;}
  58.     if(p->info==x){
  59.         ultim=q;
  60.         delete p;
  61.         ultim->urm=NULL;
  62.         return 0;}
  63.     return 0;
  64. }
  65.  
  66. int stergerelista(){
  67.     nod *p=prim;
  68.     nod *q;
  69.     while(p->urm!=NULL){
  70.             q=p;
  71.  
  72.             p=p->urm;
  73.             delete q;}
  74. }
  75.  
  76. int main(){
  77.     int n,x;
  78.     cout<<"Dati n: ";
  79.     cin>>n;
  80.     prim=new nod;
  81.     ultim=prim;
  82.     cout<<"Dati o valoare: ";
  83.     cin>>prim->info;
  84.     prim->urm=ultim;
  85.     ultim->urm=NULL;
  86.     adaugare(n-1);
  87.     afisare();
  88.     cout<<endl<<"Dati x: ";cin>>x;
  89.     if(cautare(x))
  90.         cout<<"Am gasit x";
  91.     else cout<<"Nu am gasit x";
  92.     stergere(x);
  93.     cout<<endl<<"Dupa stergere : ";
  94.     afisare();
  95.     stergerelista();
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement