Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class stackFun
- {
- private:
- int top;
- int arr[];
- public:
- stackFunctions(int n)
- {
- top = -1;
- /*for (int i = 0; i < n; i++)
- {
- arr[i] = 0;
- }*/
- }
- bool isEmpty()
- {
- if (top == -1)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- bool isFull(int &n)
- {
- if (top == n-1)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- void push(int &val, int &n)
- {
- if(isFull(n))
- {
- cout<< "Stack is Full";
- }
- else
- {
- top += 1;
- arr[top] = val;
- }
- }
- int pop()
- {
- if(isEmpty())
- {
- cout << "Stack underflow" << endl;
- }
- else
- {
- int val = arr[top];
- top--;
- return val;
- }
- }
- int display(int &n)
- {
- if (isEmpty())
- {
- cout << "Stack underflow" << endl;
- }
- else
- {
- cout << "The values in the stack are: " << endl;
- for (int i = 0; i < n; i++)
- {
- cout << arr[i] << endl;
- }
- }
- }
- int op()
- {
- cout << endl
- << "Enter" << endl
- << "1 - for push" << endl
- << "2 - for pop" << endl
- << "3 - for display" << endl
- << "4 - for exit" << endl;
- cout << "Choice of Action: ";
- }
- void dataType(int choice, int val)
- {
- if (choice == 1)
- {
- int val;
- }
- else if (choice == 2)
- {
- float val;
- }
- else if (choice == 3)
- {
- char val;
- }
- }
- };
- int main()
- {
- stackFun p1;
- int choice=0, n=0, val=0;
- /*cout << "Enter" << endl
- << "1 - for int" << endl
- << "2 - for float" << endl
- << "3 - for char" << endl;
- cout << "Choice: ";
- cin >> choice;
- cout << endl;
- p1.dataType(choice, val); */
- cout << "Enter the number of elements in Stack:" << endl;
- cin >> n;
- p1.pop();
- while (choice != 4 || choice > 4){
- p1.op();
- cin >> choice;
- if (choice == 1)
- {
- cout << "Enter Value: ";
- cin >> val;
- p1.push(val, n);
- }
- if (choice == 2)
- {
- p1.pop();
- }
- if (choice == 3)
- {
- p1.display(n);
- }
- if (choice == 4)
- {
- cout << "Closing the program..." << endl;
- exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement