Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int S[100], n, baza, varf, i, nr;
- void initializare() {
- cout << "Valoare pentru capacitatea stivei, n = "; cin >> n;
- for (i = 1; i <= n; i++)
- S[i] = 0;
- varf = 0;
- baza = 0;
- }
- void creare() {
- if (varf == 0) {
- varf = 1;
- cout << "Introduceti valoare pentru elementul din varful stivei S[" << varf << "]="; cin >> S[varf];
- }
- else
- cout << "Operatie fara sens : stiva nu este vida" << endl;
- }
- void intrare() {
- varf = varf + 1;
- if (varf <= n) {
- cout << "Introduceti valoarea pentru elementul din varful stivei S[" << varf << "]="; cin >> S[varf];
- }
- else
- cout << "Operatie imposibila : stiva este plina" << endl;
- }
- void iesire() {
- if (varf == 0)
- cout << "Operatie imposibila : stiva este goala" << endl;
- else {
- cout << "Iese elementul din varful stivei S[" << varf << "]" << endl;
- S[varf] = 0;
- varf = varf - 1;
- }
- }
- void parcurgere() {
- if (varf == 0)
- cout << "Operatie imposibila : stiva este goala" << endl;
- else {
- baza = 1;
- nr = 0;
- for (i = varf; i >= baza; i--) {
- cout << S[i] << " ";
- nr = nr + 1;
- }
- cout << "Stiva contine " << nr << " elemente" << endl;
- }
- }
- int main()
- {
- initializare();
- int optiune = 0;
- do {
- do {
- cout << "Operatii asupra stivei : " << endl;
- cout << "1 creare" << endl;
- cout << "2 intrare element nou" << endl;
- cout << "3 iesirea unui element" << endl;
- cout << "4 parcurgere" << endl;
- cout << "5 sfarsit program" << endl;
- cout << "Introduceti optiunea (1, 2, 3, 4, 5) : "; cin >> optiune;
- } while (optiune > 5);
- switch (optiune) {
- case 1: creare(); break;
- case 2: intrare(); break;
- case 3: iesire(); break;
- case 4: parcurgere(); break;
- case 5: cout << "Sfarsit program"; break;
- }
- } while (optiune != 5);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement