Implement stack using dynamic array. Make sure you provide following methods within class

 Implement stack using dynamic array. Make sure you provide following methods within class;

1. Constructor: Initialize data members of the class

2. Destructor: Deallocate the memory, if it was allocated dynamically

3. isEmpty: Check whether stack is empty or not

4. push: Insert a node in the stack

5. pop: Delete a node from top of the stack

6. top: Returns data of top from stack

7. display: Print all the elements of stack 


Solution


#include <iostream>

#include <stdlib.h>

using namespace std;

 

class Stack

{

private:

int *StackArray;

int  StackSize;

int  top;

public:

Stack(int);           //Constructor

void Push(int);

void Pop(void);

int  TopElement(void);

bool isFull(void);

bool isEmpty(void);

void Display(void);

~Stack()              //Destructor

{

    delete []StackArray;

    }

};

int Stack::TopElement()

{

return StackArray[top];

}

Stack::Stack(int size)

{

StackArray = new int[size];

StackSize = size;

top = -1;

}

void Stack::Push(int num)

{

if (isFull())

{

cout << "The Stack is Full...\n";

}

else

{

top++;

StackArray[top] = num;

}

}

void Stack::Pop(void)

{

if (isEmpty())

{

cout << "The Stack is Empty...\n";

}

else

{

top--;

}

}

bool Stack::isFull(void)

{

bool status;

if (top == StackSize - 1)

status = true;

else

status = false;

return status;

}

bool Stack::isEmpty(void)

{

bool status;

if (top == -1)

status = true;

else

status = false;

return status;

}

void Stack::Display(void)

{

if (isEmpty())

{

cout << "The Stack is Empty...\n";

}

else

{

int t = top;

while (t >= 0)

{

cout << "StackArray[" << t << "] = " << StackArray[t] << endl;

t--;

}

}

}

int main()

{

int size;

char choice;

system("COLOR 0C");

cout << "\t\t\t\t\t[Programm Information]" << endl;

cout << endl;

cout << "\tEnter Array Size : ";

cin >> size;

 

Stack st(size);

do

{

system("cls");

system("COLOR FC");

cout << "\t\t\t\t\t[Programm Information]" << endl;

cout << endl;

cout << "\t\tPress 1 : For Push  " << endl;

cout << "\t\tPress 2 : For Pop  "<<endl;

cout << "\t\tPress 3 : For Display  " << endl;

cout << "\t\tPress 4 : For Array Top Elements  " << endl;

cout << "\t\tPress Any Button For Exit  " << endl;

cout << endl;

cout << "\t\t\t\t\tYour Choice" << endl;

cout << "\t\tEnter Your Choice : " ;

cin >> choice;

 

switch (choice)

{

case '1':

int val;

cout << endl;

cout << "\tInsert Value : ";

cin >> val;

st.Push(val);

break;

case '2':

cout << endl;

cout << "\tValue Removed : ";

st.Pop();

break;

case '3':

cout << endl;

cout << "\tAll Value Show Store In Stack : " << endl;

st.Display();

break;

case '4':

cout << endl;

cout << "\tArray Top Elements : " <<st.TopElement();

break;

default:

goto Exit;

}

cout << endl;

cout << "\tDo You Want to Run Again (Y/y) : ";

cin >> choice;

} while (choice == 'Y' || choice == 'y');

 

Exit:

cout << endl;

cout << "...THANKS FOR USING MY PROGRAMM... ";

}


                                                 Output

Implement stack using dynamic array. Make sure you provide following methods within class

Implement stack using dynamic array. Make sure you provide following methods within class

Implement stack using dynamic array. Make sure you provide following methods within class

Implement stack using dynamic array. Make sure you provide following methods within class



 random/hot-posts 


PROGRAMMING QUESTIONS/feat-big

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.