Jump to content

Recommended Posts

Posted

Buna seara.as avea si eu nevoie de o un program la lista liniara simpla cu meniu care sa aiba in componenta toate operatiile posibile(creare,adaugare in fata primului nod,adaugare in spatele ultimului nod,stergere nod din lista,stergere primu element si stergere ultimul element) am facut eu ceva dar nu imi reuseste daca ati fi draguti sa imi corecti as fi recunascator.

#include<iostream.h>

void meniu()

{

cout<<endl<<endl<<endl<<endl<<endl;

cout<<"                                MENIU          "<<endl<<endl<<endl<<endl<<endl;

cout<<"                   1.Creare                 "<<endl;

cout<<"                   2.Inserare inaintea capului de lista"<<endl;

cout<<"                   3.Inserare dupa ultimul element"<<endl;

cout<<"                   4.Inserare dupa un element cu adresa precizata"<<endl;

cout<<"                   5.Inserare inaintea unui element cu adresa precizata"<<endl;

cout<<"                   6.Eliminarea capului de lista"<<endl;

cout<<"                   7.Eliminarea ultimului nod"<<endl;

cout<<"                   8Eliminarea unui nod cu adresa precizata"<<endl;

}

struct nod{int info;

	   nod *adr;};

nod *prim,*ultim;

void creare(nod *&prim,nod *&ultim,int x)

{cout<<"x=";

cin>>x;

prim=new nod;

prim->info=x;

prim->adr=NULL;

ultim=prim;}

void listare(nod *prim)

{nod *p;

p=prim;

cout<<"   ";

cout<<"Lista  este:";

cout<<"  ";

while(p!=0)

{cout<<p->info<<" ";

p=p->adr;}}

void adaug_fata(nod *&prim,int inf)

	{nod *q;

	 q=new nod;

	 q->info=inf;

	 q->adr=prim;

	 prim=q;}

void adaug_dupa(nod *&prim,nod *&ultim,int x)

	{nod *c;

	 c=new nod;

	 c->info=x;

	 c->adr=NULL;

	 ultim->adr=c;

	 ultim=c;}

void adaug_int(nod *prim,int x,int inf)

	{nod *r,*q;

	 q=prim;

while(q->info!=x)

	q=q->adr;

	r=new nod;

	r->info=inf;

	r->adr=q->adr;

	q->adr=r;}

void adaug_int1(nod *prim,int x,int inf)

	{nod *r,*q;

	 q=prim;

while(q->adr->info!=x)

	q=q->adr;

	r=new nod;

	r->info=inf;

	r->adr=q->adr;

	q->adr=r;}

void sterg_prim(nod *&prim)

	{nod *q;

	 q=prim;

	 prim=prim->adr;

	 delete q;}

void sterg_urm(nod *prim,nod *& ultim)

	{nod *q;

	 q=prim;

while(q->adr!=ultim)

	q=q->adr;

	q->adr=NULL;

	delete ultim;

	ultim=q;}

void stergere_interior(nod *prim,int x)

	{nod *q,*r;

	 int ok=0;

	 q=prim;

while(q->info!=x&&q->adr!=NULL)

	{r=q;

	 q=q->adr;}

if(q->info==x)

 	{r->adr=q->adr;

 	 delete q;

	 ok=1;}

if(ok==0)

	 cout<<"Nu e in lista!";}	 




int main()

{int x;

do{

   meniu();

   prim=0;

   cout<<endl<<endl<<endl;

   cout<<"       Alegeti o optiune:"<<" ";

	cin>>x;

        cout<<endl<<endl;

	switch(x)

		{case 1:{ creare(prim,ultim,x);listare(prim);}break;

		 case 2:{cout<<"  2)Inserare inaintea capului de lista"<<endl;

			int nr,i;

				for(i=1;i<10;i++)

				{cin>>nr;

				 adaug_fata(prim,nr);}listare(prim);

				 }break;

		 case 3:{cout<<"  3)Inserare dupa ultimul element"<<endl; creare(prim,ultim,x);

			int nr,i;

				for(i=1;i<10;i++)

				{cin>>nr;

				 adaug_dupa(prim,ultim,nr);}listare(prim);}break;

		 case 4:{cout<<"  4)Inserare dupa un element cu adresa precizata"<<endl;

			int nr1,nr2,nr,i;

			creare(prim,ultim,x);

			for(i=1;i<10;i++)

				{cin>>nr;

				 adaug_dupa(prim,ultim,nr);}

				listare(prim);

				cout<<endl;

				cout<<"nr1= "; 

				cin>>nr1;

				cout<<endl;

				cout<<"nr2= "; 

				cin>>nr2;

				 adaug_int(prim,nr1,nr2);

				 listare(prim);}break;

		 case 5:{cout<<"  5)Inserare inaintea unui element cu adresa precizata"<<endl;

			int nr1,nr2,nr,i;

			creare(prim,ultim,x);

			for(i=1;i<10;i++)

				{cin>>nr;

				 adaug_dupa(prim,ultim,nr);}

				listare(prim);

				cout<<endl;

				cout<<"nr1= "; 

				cin>>nr1;

				cout<<endl;

				cout<<"nr2= "; 

				cin>>nr2;

				 adaug_int1(prim,nr1,nr2);

				 listare(prim);

		 }break;

		 case 6:{cout<<"  6)Eliminarea capului de lista"<<endl;

			 int nr1,nr2,nr,i;

			creare(prim,ultim,x);

			for(i=1;i<10;i++)

				{cin>>nr;

				 adaug_dupa(prim,ultim,nr);}

				listare(prim);

				cout<<endl;

				sterg_prim(prim);

				 listare(prim);

		 }break;

		 case 7:{cout<<"  7)Eliminarea ultimului nod"<<endl;

		 int nr,i;

			creare(prim,ultim,x);

			for(i=1;i<10;i++)

				{cin>>nr;

				 adaug_dupa(prim,ultim,nr);}

				listare(prim);

				cout<<endl;

				sterg_urm(prim,ultim);

				 listare(prim);}break;

		 case 8:{cout<<"  8)Eliminarea unui nod cu adresa precizata";

		 int nr,i,nr1;

			creare(prim,ultim,x);

			for(i=1;i<10;i++)

				{cin>>nr;

				 adaug_dupa(prim,ultim,nr);}

				listare(prim);

				cout<<"nr1= "<<" ";cin>>nr1;

				cout<<endl;

				stergere_interior(prim,nr1);

				 listare(prim);}break;

	case 9:{ x=9;break;}

		}

}


while(x<=8);

return 1;

}

Posted

prima buba. u ai o lista simplu inlantuita.

Adica memorezi adresa urmatorului element. asa ca ai nevoie de adresa primului, dar adresa ultimului element nu mai are nici un sens practic, decat daca iti trebuie in mod special sa lucrezi cu un ultim.. intelegi? o sa completez pe masura ce ma mai uit peste cod.

ah da,acu vad la ceti trebe ultimu salvat :D sorry.

ca sami fie mai usor..compileaza? citeste valorile bine? care e buba mai exact?

love is a verb
Love is a doing word

Posted

incearca functiile din graphics..

lista:

//legaturile ptr inserare dupa, inainte, etc.. sunt gresite putin, d aia nu da bine..

//si nr1 nr2 stergele ca nu vad sa le folosesti p undeva.

#include<iostream>

struct nod{

	int info;

	nod* adr_urm;

};

void adaugare(nod*& v, nod*& sf, int val)

{


	nod* c;

	if(v==0)

	{


		c=new nod;

		v->info=val;

		sf->adr_urm=c;

    c->info=val;

		c->adr_urm=0;

		sf=c;

	}

	else

	{


		c=new nod;

		sf->adr_urm=c;

		c->info=val;

		c->adr_urm=0;

		sf=c;

	}

}

void inserare_dupa(nod* v, nod*& sf, int val, ina vall)

{


	nod*c=v;

	nod* d;

	while(c->info!=val)

		c=c->adr_urm;

	d=new nod;

	d->info=vall;

	d->adr_urm=c->adr_urm;

	c->adr_urm=d;

	if(d->adr_urm==0)

		sf=d;

}

void inserare_inainte(nod*& v, int val, int vall)

{


	nod* c, *d;

	if(v->info==val)

	{


		d=new nod;

		d->info=val;

		d->adr_urm=v;

		v=d;


	}

	else

	{


		c=v;

		while(c->adr_urm->info!=val)

			c=c->adr_urm;

		d=new nod;

		d->info=vall;

		d->adr_urm=c->adr_urm;

		c->adr_urm=d;


	}

}

void sterg(nod*& v, nod*& sf, int val)

{


	nod* c, *man;

  if(v->info==val)

	{


		man=v;

		v=v->adr_urm;

	}

	else

	{

		c=v;

		while(c->adr_urm->info!=val)

			c=c->adr_urm;

		man=c->adr_urm;

		c->adr_urm=man->adr_urm;

		if(man==sf)

			sf=c;

	}

	delete man;

}

void listeaza(nod* v)

{


	nod* c=v;

	while(c)

	{


		cout<<c->info<<endl;

		c=c->adr_urm;

	}

}

#include "lista_u.cpp"

nod *v, *sf;

int i;

int main()

{


	for(i=1; i<=10; i++)

		adaugare(v, sf, i);

	listare(v);

	inserare_dupa(v, sf, 7, 11);

	inserare_dupa(v, sf, 10, 12);

	inserare_dupa(v, sf, 1, 13);

	listare(v);

	inserare_inainte(v, 13, 14);

	inserare_inainte(v, 1, 15);

	listare(v);

	sterg(v, sf, 15);

	sterg(v, sf, 13);

	sterg(v, sf, 12);

	listare(v);

}

  • Upvote 1

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.