Scrieti o functie care concateneaza doua stive in prima stiva. (adresele varfurilor vor fi param. in functie)
#include<iostream>Un fisier text contine mai multe linii de forma: POP x sau PUSH y, reprezentand operatii care trebuie executate asupra unei stive:
#include<fstream>
using namespace std;
struct nod{
int info;
nod *urm;};
void afisare(nod* vf)
{while(vf!=NULL){
cout<<vf->info<<" ";
vf=vf->urm;}
}
void adaug(int x, nod* &vf)
{nod* p;
p=new nod;
p->info=x;
p->urm=vf;
vf=p;}
void creare(nod* &vf, char n[])
{ifstream f(n);
int x;
while(f>>x)
adaug(x,vf);
}
void concat(nod* vf1, nod* vf2)
{nod*p=vf1;
while(p->urm != NULL)
p=p->urm;
p->urm=vf2;
}
int main()
{nod *vf1=NULL,*vf2=NULL,*p;
creare (vf1,"date");
creare (vf2,"date2");
afisare(vf1);
cout<<endl;
afisare(vf2);
cout<<endl;
concat(vf1,vf2);
cout<<endl;
afisare(vf1);
}
POP x => se scot din stiva x numere
PUSH y => se pune in stiva numarul y.
Sa se creeze o stiva asupra careia sa se efectueze toate operatiile specificate.
Sa se verifice daca la sfarsit stiva ramane vida. Obs. Daca nu se poate efectua operatia POP, programul se termina si se afiseaza EROARE.
#include<iostream>Fisier
#include<fstream>
using namespace std;
struct nod{
int info;
nod *urm;};
void adaug(int x, nod* &vf)
{nod* p;
p=new nod;
p->info=x;
p->urm=vf;
vf=p;}
void afisare(nod* vf)
{if(vf==NULL)
cout<<"Stiva vida";
while(vf!=NULL){
cout<<vf->info<<" ";
vf=vf->urm;}
}
void POP(int x, nod* &vf)
{int i=0;
nod*p;
while(i<x)
{p=vf;
vf=vf->urm;
i++;
delete p;}
}
int main()
{nod *vf=NULL,*p,*vf2=NULL;
int x,i=0,nr=0;
char s[100];
ifstream f("date");
while(f>>s>>x)
{if(strcmp(s,"POP")==0)
if(x>=nr)
{cout<<"EROARE";return 0;}
else POP(x,vf);
else
if(strcmp(s,"PUSH")==0)
adaug(x,vf);nr++;
}
afisare(vf);
}
PUSH 1Se citesc dintr-un fisier text linii de forma: elev clasa numar_absente
PUSH 1
PUSH 1
PUSH 1
PUSH 1
POP 1
PUSH 6
POP 3
PUSH 1
POP 3
a) Creati 4 stive cu elevii si absentele lor, pentru fiecare nivel. (9,10,11,12)
b) Afisati numarul de absente pe nivele
c) Afisati numarul de elevi de pe fiecare nivel
Obs: clasa poate fi: 9A,9B, …12D
#include<iostream>
#include<fstream>
using namespace std;
struct nod{
char n[20],c[4];
int na;
nod *urm;};
void adaug(char n[], char c[], int na, nod* &vf)
{nod* p;
p=new nod;
strcpy(p->n,n);
strcpy(p->c,c);
p->na=na;
p->urm=vf;
vf=p;}
void afisare(nod* vf)
{if(vf==NULL)
cout<<"Stiva vida";
while(vf!=NULL){
cout<<vf->n<<" "<<vf->c<<" "<<vf->na<<" ";
vf=vf->urm;
cout<<endl;}
}
int nr_abs(nod* vf)
{int i=0;
while(vf!=NULL)
{i+=vf->na;
vf=vf->urm;}
return i;
}
int nr_el(nod* vf)
{int i=0;
while(vf!=NULL)
{i++;
vf=vf->urm;}
return i;
}
int main()
{nod *vf=NULL,*p,*vf2=NULL;
int na;
char n[20],pr[20],c[4];
nod *vf9=NULL,*vf10=NULL,*vf11=NULL,*vf12=NULL;
ifstream f("date");
while(f>>n)
{f>>c>>na;
if(strncmp(c,"9",1)==0)
adaug(n,c,na,vf9);
else if(strncmp(c,"10",2)==0)
adaug(n,c,na,vf10);
else if(strncmp(c,"11",2)==0)
adaug(n,c,na,vf11);
else if(strncmp(c,"11",2)==0)
adaug(n,c,na,vf12);
}
cout<<"Clasa 9"<<endl;
afisare(vf9);
cout<<nr_abs(vf9)<<" absente"<<endl;
cout<<nr_el(vf9)<<" elevi"<<endl;
cout<<endl;
cout<<"Clasa 10"<<endl;
afisare(vf10);
cout<<nr_abs(vf10)<<" absente"<<endl;
cout<<nr_el(vf10)<<" elevi"<<endl;
cout<<endl;
cout<<"Clasa 11"<<endl;
afisare(vf11);
cout<<nr_abs(vf11)<<" absente"<<endl;
cout<<nr_el(vf11)<<" elevi"<<endl;
cout<<endl;
cout<<"Clasa 12"<<endl;
afisare(vf12);
cout<<nr_abs(vf12)<<" absente"<<endl;
cout<<nr_el(vf12)<<" elevi"<<endl;
cout<<endl;
}