fl0 fl0w
31-12-07, 11:11
Description :
This is a program that uses the stack structure ,the way it behaves I mean,so what does it do ?
In the first line of the file "citire.txt" it is the number of elements;
Next you give a CRT number and a name ,after that you can put one more element,but because the stack works LIFO mode,you will see that it is on top;
Next you can delete a element,the element deleted will be the last entered and yes you guessed right it will be the first you see;
Next it will be a sorting of the elements by the CRT number ,it will be increasing;
Next you ca insert a element in the stack ,so it will be inserted whereever you want;
Next you can delete a element from whereever you want.
So try the program and if you have any questions ask here,and I'll answer.
So here's the executable and the files for input and output ("citire.txt && afisare.txt").
http://rapidshare.com/files/80255202/stiva_lista_.rar.html
#include<fstream.h>
#include<string.h>
#include<stdio.h>
//declarare fisiere
fstream f("citire.txt",ios::in);
fstream g("afisare.txt",ios::out);
//structura
struct stiva {
int nr;
char nume[20];
stiva *leg;
} *vf,*p;
//function prototypes
void banner();
void header();
void creare();
void afisare();
void adaugare();
void eliminare();
void sortare();
void inserare();
void extragere();
int main()
{
/*
Descriere a executiei propusa de mine in main(si care poate fi modificata la alegere)
Prima data dam un nr de criteriu si afiseaza stiva;apoi adaugarea presupune
ca elementul este pus ultimul dar datorita structurii stivei LIFO el va
fi afisat primul;are loc o sortare dupa numarul de criteriu (crescatoare) si
o afisare a noii stive;apoi eliminarea si anume ultimul element va fi scos;
o afisare a noii stive apoi inserare ,asta presupune ca elementul va vi
inserat dupa alegege oriunde in stiva in functie de numarul de criteriu ales
de tine,o afisare bineinteles a noii stive;si o extragere si anume orice element
oriunde s-ar afla se extrage din stiva si o afisare finala;
*/
banner();
header();
creare();
afisare();
adaugare();
afisare();
sortare();
afisare();
eliminare();
afisare();
inserare();
afisare();
extragere();
afisare();
banner();
return 0;
}
void creare()
{ int n;
//numarul de noduri
f>>n;
g<<"Numarul de noduri introdus este"<<endl;
//for instruction
for(int i=1;i<=n;i++)
{ p=new stiva;
//dati nr de criteriu
f>>p->nr;
//dati numele
f>>p->nume;
p->leg=vf;
vf=p;
}
}
void afisare()
{ g<<"lista elementelor din stiva este"<<endl;
p=vf;
while(p)
{ g<<p->nr<<" "<<p->nume<<endl;
p=p->leg;
}
}
void adaugare()
{ p=new stiva;
//dati un nod de adaugat
f>>p->nr;
//dati numele
f>>p->nume;
p->leg=vf;
vf=p;
}
void eliminare()
{
//if selection
if(!vf) g<<"nu se poate!\n\n";
else
{ p=vf;
vf=vf->leg;
delete p;
g<<"s-a eliminat un element "<<endl;
//end if
}
}
void sortare()
{ for(p=vf;p->leg!=0;p=p->leg)
for(stiva *q=p->leg;q!=0;q=q->leg)
//if selection
if(p->nr>q->nr)
{ int aux=p->nr;
p->nr=q->nr;
q->nr=aux;
//end if
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;
//dati nr de criteriu
f>>x;
//dati numele
f>>aux;
if(x<=vf->nr)
{ p=new stiva;
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;
//dati elementul de eliminat adica nr de criteru
f>>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 banner()
{ printf("-------------------------------------------------------------------------\n\n");
}
void header()
{
printf("author fl0 fl0w\n\n");
printf("flo[underscore]flow[underscore]supremacy[at]yahoo[dot]com\n\n");
}
This is a program that uses the stack structure ,the way it behaves I mean,so what does it do ?
In the first line of the file "citire.txt" it is the number of elements;
Next you give a CRT number and a name ,after that you can put one more element,but because the stack works LIFO mode,you will see that it is on top;
Next you can delete a element,the element deleted will be the last entered and yes you guessed right it will be the first you see;
Next it will be a sorting of the elements by the CRT number ,it will be increasing;
Next you ca insert a element in the stack ,so it will be inserted whereever you want;
Next you can delete a element from whereever you want.
So try the program and if you have any questions ask here,and I'll answer.
So here's the executable and the files for input and output ("citire.txt && afisare.txt").
http://rapidshare.com/files/80255202/stiva_lista_.rar.html
#include<fstream.h>
#include<string.h>
#include<stdio.h>
//declarare fisiere
fstream f("citire.txt",ios::in);
fstream g("afisare.txt",ios::out);
//structura
struct stiva {
int nr;
char nume[20];
stiva *leg;
} *vf,*p;
//function prototypes
void banner();
void header();
void creare();
void afisare();
void adaugare();
void eliminare();
void sortare();
void inserare();
void extragere();
int main()
{
/*
Descriere a executiei propusa de mine in main(si care poate fi modificata la alegere)
Prima data dam un nr de criteriu si afiseaza stiva;apoi adaugarea presupune
ca elementul este pus ultimul dar datorita structurii stivei LIFO el va
fi afisat primul;are loc o sortare dupa numarul de criteriu (crescatoare) si
o afisare a noii stive;apoi eliminarea si anume ultimul element va fi scos;
o afisare a noii stive apoi inserare ,asta presupune ca elementul va vi
inserat dupa alegege oriunde in stiva in functie de numarul de criteriu ales
de tine,o afisare bineinteles a noii stive;si o extragere si anume orice element
oriunde s-ar afla se extrage din stiva si o afisare finala;
*/
banner();
header();
creare();
afisare();
adaugare();
afisare();
sortare();
afisare();
eliminare();
afisare();
inserare();
afisare();
extragere();
afisare();
banner();
return 0;
}
void creare()
{ int n;
//numarul de noduri
f>>n;
g<<"Numarul de noduri introdus este"<<endl;
//for instruction
for(int i=1;i<=n;i++)
{ p=new stiva;
//dati nr de criteriu
f>>p->nr;
//dati numele
f>>p->nume;
p->leg=vf;
vf=p;
}
}
void afisare()
{ g<<"lista elementelor din stiva este"<<endl;
p=vf;
while(p)
{ g<<p->nr<<" "<<p->nume<<endl;
p=p->leg;
}
}
void adaugare()
{ p=new stiva;
//dati un nod de adaugat
f>>p->nr;
//dati numele
f>>p->nume;
p->leg=vf;
vf=p;
}
void eliminare()
{
//if selection
if(!vf) g<<"nu se poate!\n\n";
else
{ p=vf;
vf=vf->leg;
delete p;
g<<"s-a eliminat un element "<<endl;
//end if
}
}
void sortare()
{ for(p=vf;p->leg!=0;p=p->leg)
for(stiva *q=p->leg;q!=0;q=q->leg)
//if selection
if(p->nr>q->nr)
{ int aux=p->nr;
p->nr=q->nr;
q->nr=aux;
//end if
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;
//dati nr de criteriu
f>>x;
//dati numele
f>>aux;
if(x<=vf->nr)
{ p=new stiva;
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;
//dati elementul de eliminat adica nr de criteru
f>>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 banner()
{ printf("-------------------------------------------------------------------------\n\n");
}
void header()
{
printf("author fl0 fl0w\n\n");
printf("flo[underscore]flow[underscore]supremacy[at]yahoo[dot]com\n\n");
}