Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- typedef struct elem {
- int info;
- struct elem *pred,*urm;
- } element;
- typedef struct {
- element *st,*cr,*sf;
- int lung;
- } dubla;
- dubla L;
- int n;
- int printme(dubla lista,int direction)
- {
- cout<<endl;
- if (direction==1)
- {
- element *plimb;
- plimb=lista.st;
- while (plimb!=NULL) {
- cout<<plimb->info<<" ";plimb=plimb->urm;
- }
- return 1;
- }
- else
- if (direction==0)
- {
- element *plimb;
- plimb=lista.sf;
- while (plimb!=NULL) {
- cout<<plimb->info<<" ";plimb=plimb->pred;
- }
- return 1;
- }
- else return 0;
- }
- dubla initlist()
- {
- dubla somelist;
- somelist.lung=0;
- somelist.st=somelist.cr=somelist.sf=NULL;
- return somelist;
- }
- int isempty(dubla lista)
- {
- return (lista.lung==0);
- }
- int foundme(dubla *lista, int valoare)
- {
- element *plimb;
- if (!isempty(*lista))
- {
- plimb=lista->st;
- int gasit=0;
- while ((!gasit)&&(plimb!=NULL))
- {
- if (plimb->info==valoare) { gasit=1;}
- else plimb=plimb->urm;
- }
- if (gasit) { lista->cr=plimb; return 1;}
- else return 0;
- }
- else return 0;
- }
- dubla addme(dubla lista,int nou,int direction)
- {
- if (direction==1)
- {
- if (!isempty(lista))
- {
- element *adaugat=new element;
- adaugat->info=nou;
- if (lista.cr!=lista.sf)
- {
- element *stanga,*dreapta;
- stanga=lista.cr;
- dreapta=lista.cr->urm;
- stanga->urm=adaugat;
- adaugat->pred=stanga;
- adaugat->urm=dreapta;
- dreapta->pred=adaugat;
- lista.lung++;
- lista.cr=adaugat;
- return lista;
- }
- else
- {
- element *stanga;
- stanga=lista.cr;
- stanga->urm=adaugat;
- adaugat->pred=stanga;
- adaugat->urm=NULL;
- lista.cr=adaugat;
- lista.sf=adaugat;
- lista.lung++;
- return lista;
- }
- }
- else
- { // vida !
- element *adaugat=new element;
- adaugat->info=nou;
- adaugat->pred=NULL;
- adaugat->urm=NULL;
- lista.st=lista.cr=lista.sf=adaugat;
- lista.cr=adaugat;
- lista.lung++;
- return lista;
- }
- }
- else
- {
- // la stanga curentului !
- }
- }
- dubla delme(dubla lista,int direction)
- {
- if (direction==1)
- {
- element *stanga,*dreapta;
- stanga=lista.cr->pred;
- dreapta=lista.cr->urm;
- delete lista.cr;
- stanga->urm=dreapta;
- dreapta->pred=stanga;
- lista.cr=dreapta;
- lista.lung--;
- return lista;
- }
- else
- if (direction==0)
- {
- // to be done !
- return lista;
- }
- else return lista;
- }
- int main()
- {
- int somevalue;
- int right=1;
- int left=0;
- L=initlist();
- cout<<"N=";cin>>n;
- for(int i=1;i<=n;i++) L=addme(L,i,right);
- printme(L,right);
- printme(L,left);
- cout<<" Value = ";cin>>somevalue;
- foundme(&L,somevalue);
- cout<<endl<<" Current "<<L.cr->info<<endl;
- L=addme(L,0,right);
- printme(L,right);
- printme(L,left);
- cout<<endl<<" Current "<<L.cr->info<<endl;
- delme(L,1);
- printme(L,right);
- printme(L,left);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement