Created
October 25, 2023 12:15
-
-
Save semreh17/1749f4e6f653032b85e6fcb6509b4a73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
//checks if the array is empty | |
bool isEmpty(int arr[], int length) { | |
return(arr[length - 1] == 0); | |
} | |
//adds an element into the array | |
void push(int arr[], int &counter, int length) { | |
if(counter == (length - 1)) { | |
cout << "overflow, the stack is full." << endl; | |
}else { | |
cout << "insert the element to add to the stack: "; | |
cin >> arr[counter]; | |
counter++; | |
} | |
} | |
//"deletes" an element from the array | |
int pop(int arr[], int &counter, int length) { | |
int tmp = 0; | |
if(!isEmpty(arr, length)) { | |
cout << "empty array" << endl; | |
}else { | |
tmp = arr[counter]; | |
arr[counter-1] = 0; | |
counter--; | |
} | |
return(tmp); | |
} | |
void stackPrinter(int arr[], int length) { | |
for (int i = 0; i < length-1; ++i) { | |
cout << arr[i] << "\t"; | |
} | |
} | |
void menuOpzioni(int arr[], int length, int &counter) { | |
int check = 1, tmp; | |
while(check != 0) { | |
cout << endl << "type '1' if you want to add an element to the stack" << endl | |
<< "type '2' if you want to delete an element from the stack" << endl | |
<< "type '3' if you want to check if the stack is empty" << endl | |
<< "type '4' if you want to print all the stack" << endl | |
<< "type '0' if you want to exit from the program: " << endl; | |
cin >> check; | |
switch (check) { | |
case 1: | |
push(arr, counter, length); | |
break; | |
case 2: | |
tmp = pop(arr, counter, length); | |
cout << "this is the element deleted from the stack: " << tmp; | |
break; | |
case 3: | |
if(isEmpty(arr, length)) { | |
cout << "the stack is empty." << endl; | |
}else { | |
cout << "the stack its not empty"; | |
} | |
break; | |
case 4: | |
cout << endl; | |
stackPrinter(arr, length); | |
break; | |
default: | |
cout << "you've entered a wrong number!" << endl; | |
} | |
} | |
} | |
int main() { | |
int counter = 0; | |
int length; | |
cout << "insert the length of the stack: "; | |
cin >> length; | |
int arr[length]; | |
arr[length - 1] = counter; | |
menuOpzioni(arr, length, counter); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment