Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- typedef int adresa;
- struct nod
- {int info;
- adresa urm;
- };
- nod L[1000];
- int nr_elem;
- adresa ocupat[1000],sf,v;
- int Exista_spatiu()
- {
- return nr_elem<1000;
- }
- void Aloca(adresa &x)
- {
- adresa i=1;
- while(ocupat[i]) i++;
- x=i;
- ocupat[i]=1;
- nr_elem++;
- }
- void Elibereaza(adresa x)
- {
- ocupat[x]=0;
- nr_elem--;
- }
- void Adaugare(adresa &v,int val)
- {
- int c;
- if(!v)
- {
- Aloca(v);
- L[v].info=val;
- L[v].urm=0;
- sf=v;
- }
- else
- if(Exista_spatiu)
- {
- Aloca(c);
- L[sf].urm=c;
- L[c].info=val;
- L[c].urm=0;
- sf=c;
- }
- else cout<<"lipsa spatiu"<<endl;
- }
- void Inserare_dupa(adresa v,int val, int val1)
- {
- adresa c,d;
- if(Exista_spatiu())
- {
- c=v;
- while(L[c].info!=val) c=L[c].urm;
- Aloca(d);
- L[d].info=val1;
- L[d].urm=L[c].urm;
- L[c].urm=d;
- if(L[d].urm==0) sf=d;
- }
- else cout<<"lipsa spatiu"<<endl;
- }
- void Inserare_inainte(adresa &v,int val, int val1)
- {
- adresa c,d;
- if(Exista_spatiu())
- if(L[v].info==val)
- {
- Aloca(d);
- L[d].info=val1;
- L[d].urm=v;
- v=d;
- }
- else
- {
- c=v;
- while(L[L[c].urm].info!=val) c=L[c].urm;
- Aloca(d);
- L[d].info=val1;
- L[d].urm=L[c].urm;
- L[c].urm=d;
- }
- else cout<<"nu e spatiu "<<endl;
- }
- void Sterg(adresa &v, int val)
- {
- adresa c,man;
- if(L[v].info==val)
- {
- man=v;
- v=L[v].urm;
- }
- else
- {
- c=v;
- while(L[L[c].urm].info!=val) c=L[c].urm;
- man=L[c].urm;
- L[c].urm=L[man].urm;
- if(man==sf) sf=c;
- }
- Elibereaza(man);
- }
- void Afisare(adresa v)
- {
- adresa c=v;
- while(c)
- {
- cout<<L[c].info<<" ";
- c=L[c].urm;
- }
- cout<<endl;
- }
- int Cautare(int x)
- {
- int p;
- while(p && L[p].info!=x)
- p=L[p].urm;
- return(p);
- }
- int main()
- {
- int i; vvv
- for(i=1;i<=10;i++)
- Adaugare(v,i);
- Afisare(v);
- Inserare_dupa(v,1,15); Afisare(v);
- Inserare_dupa(v,10,25); Afisare(v);
- Inserare_inainte(v,3,29);Afisare(v);
- Sterg(v,5);Afisare(v);
- cout<<"nodul cu info 25 se afla pe pozitia "<<Cautare(25);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement