fl0 fl0w Posted March 28, 2007 Report Posted March 28, 2007 Pentru inceput : Stiva este o structura de date abstracta pentru care atat operatia de inserare a unui element in structura,cat si operatia de extragere a unui element se realizeaza la un singur capat,denumit varful stivei. Coada este o structura de date abstracta pentru care operatia de inserare a unui element se realizeaza la un capat, in timp ce operatia de extragere a unui element se realizeaza la celalalt capat. Rezolvarea celor doua probleme clasice :Stiva pentru inceput : #include <conio.h>#include <string.h>#include <iostream.h>struct stiva { int nr; char nume[20]; stiva *leg; }*vf,*p;int n;void creare(){ cout<<"nr de noduri "; cin>>n; for(int i=1;i<=n;i++) { p=new stiva; cout<<"dati nr crt : "; cin>>p->nr; cout<<"dati numele : "; cin>>p->nume; p->leg=vf; vf=p; }}void afis(){ cout<<"lista elementelor din stiva este : "<<endl; p=vf; while(p) { cout<<p->nr<<" "<<p->nume<<endl; p=p->leg; }}void adaugare(){ p=new stiva; cout<<"dati un nod de adaugat : "; cin>>p->nr; cout<<"dati numele : "; cin>>p->nume; p->leg=vf; vf=p;}void eliminare(){ if(!vf) cout<<"nu se poate!"; else { p=vf; vf=vf->leg; delete p; cout<<"s-a eliminat un element"<<endl; }}void sortare(){ for(p=vf;p->leg!=0;p=p->leg) for(stiva *q=p->leg;q!=0;q=q->leg) if(p->nr>q->nr) { int aux=p->nr; p->nr=q->nr; q->nr=aux; char au[20]; strcpy(au,p->nume); strcpy(p->nume,q->nume); strcpy(q->nume,au); }}void inserare(){ int x; char aux[20]; stiva *q; cout<<"datzi nr crt : "; cin>>x; cout<<"datzi numele : "; cin>>aux; if(x<=vf->nr) { p=new stiva; p->nr=x; strcpy(p->nume,aux); p->leg=vf; vf=p; } else { p=vf; while(p->leg->nr<x) p=p->leg; q=new stiva; q->nr=x; strcpy(q->nume,aux); q->leg=p->leg; p->leg=q; }}void extragere(){ int x; stiva *q; cout<<"datzi elementul d eliminat (nr crt) : "; cin>>x; if(vf->nr==x) eliminare(); else { p=vf; while(p->leg->nr!=x) p=p->leg; q=p->leg; p->leg=q->leg; delete q; }}void main(){ clrscr(); creare(); afis(); adaugare(); afis(); adaugare(); sortare(); afis(); eliminare(); afis(); inserare(); afis(); extragere(); afis(); getch();} Coada : #include <conio.h>#include <iostream.h>struct coada { int info; coada *leg; };coada *p,*q,*r;int n,i;void creare(){ cout<<"dati nr d elemente : "; cin>>n; p=new coada; cout<<"dati info primului element : "; cin>>p->info; p->leg=NULL; q=p; for(i=2;i<=n;i++) { r=new coada; cout<<"dati info elementului "<<i<<" : "; cin>>r->info; r->leg=NULL; q->leg=r; q=r; }}void adaugare(){ r=new coada; cout<<"dati info elementului adaugat : "; cin>>r->info; r->leg=NULL; q->leg=r; q=r;}void eliminare(){ cout<<"se sterge elementul d p 1# poz : "<<p->info<<endl; r=p; p=p->leg; delete r;}void sortare(){ coada *s; for(r=p;r->leg!=NULL;r=r->leg) for(s=r->leg;s!=NULL;s=s->leg) if(r->info>s->info) { int aux=r->info; r->info=s->info; s->info=aux; } cout<<"s-a sortat lista "<<endl;}void inserare(){ int x; cout<<"dati elementul de inserat : "; cin>>x; if(x<p->info) { r=new coada; r->leg=p; r->info=x; p=r; } else if(x>q->info) { r=new coada; r->leg=NULL; r->info=x; q->leg=r; q=r; } else { r=p; while(r->leg->info<x) r=r->leg; coada *s=new coada; s->info=x; s->leg=r->leg; r->leg=s; }}void afisare(){ coada *r; for(r=p;r!=NULL;r=r->leg) cout<<r->info<<" "; cout<<endl;}void extragere(){ int x; cout<<"dati elementul care trebuie eliminat : "; cin>>x; if(x==p->info) { coada *r=p; p=p->leg; delete r; } else { coada *r=p; while(r->leg->info!=x && r->leg->leg!=NULL) r=r->leg; if(r->leg->info==x) { coada *s; s=r->leg; r->leg=r->leg->leg; delete s; } else cout<<"elementul nu se gaseste in lista "<<endl; }}void main(){ clrscr(); creare(); afisare(); sortare(); afisare(); adaugare(); afisare(); sortare(); afisare(); eliminare(); afisare(); inserare(); afisare(); extragere(); afisare(); extragere(); afisare(); getch();} Pentru eventuale nelamuriri sau cereri postati aici. flo_flow  Quote
Emerald Posted April 8, 2007 Report Posted April 8, 2007 stiva,coada ... hmm PASCAL Nice post ! Quote
fl0 fl0w Posted April 8, 2007 Author Report Posted April 8, 2007 Don't know pascal,C is my game . Quote