Skip to content

Instantly share code, notes, and snippets.

@MahadMuhammad
Last active October 23, 2022 08:26
Show Gist options
  • Save MahadMuhammad/74f0cbc1a1f6f5be8f9014d5603f8bfd to your computer and use it in GitHub Desktop.
Save MahadMuhammad/74f0cbc1a1f6f5be8f9014d5603f8bfd to your computer and use it in GitHub Desktop.
Implementation of Stack in C++ by abstract data type using dynamic array in C++ with the help of classes
#include <iostream>
using namespace std;
template<typename T>
class StackByArray
{
private:
int top;
int max_size;
T* array;
public:
//1
StackByArray()
{
top = -1;
max_size = 10;
array = new T[max_size];
}
//2
bool IsEmpty()
{
if (top == -1) { return true; }
else
return false;
}
//3
bool IsFull()
{
if (top == max_size - 1) { return true; }
else
return false;
}
//4
void push(T num)
{
if (IsFull())
{
cout << "\nError! Stack Overflow\n";
return;
}
else
{
top++;
array[top] = num;
}
}
//5
void pop()
{
if (IsEmpty())
{
cout << "\nError! Stack Underflow\n";
return ;
}
else
{
cout << array[top] << " is Popped\n";
top--;
}
}
//6
int count() { return top + 1; }
//7
T peek(int num)
{
// top == -1
if(IsEmpty())
{
cout << "\nError! Stack Underflow\n";
}
else
{
return array[num];
}
}
//8
void change(int index, T value)
{
if (index < 0)
{
cout << "\nError! Stack Underflow\n";
}
else if (index > top)
{
cout << "\nError! Stack Overflow\n";
return;
}
else
{
array[index] = value;
}
}
//9
void display()
{
cout << "values in stack are : \n";
for (int i = top; i > -1; i--)
{
cout << " " << array[i] << endl;
}
}
//10
bool Top(T& VALUE)
{
if (IsEmpty()) {
cout << "\nNo element in stack\n";
return false;
}
else {
VALUE = array[top];
return true;
}
}
~StackByArray()
{
//delete array;
array = NULL;
delete array;
}
};
int main()
{
StackByArray<int> stack;
int option=-1, maxsize=0, value=0;
cout << "Welcome to Stack ADT using array\n"
<< "1) Push value in Stack\n"
<< "2) Pop value in Stack\n"
<< "3) Check Stack is Empty\n"
<< "4) Check Stack is Full\n"
<< "5) Count the Elements in Stack\n"
<< "6) Peek an element in Stack\n"
<< "7) Change a specfic value in Stack\n"
<< "8) Display the whole Stack\n"
<< "9) Show the TOP element\n"
<< "10) Clear the Screen\n"
<< "0) For terminating the program\n";
while (option != 0)
{
/*cout << "Welcome to Stack ADT using array\n"
<< "1) Push value in Stack\n"
<< "2) Pop value in Stack\n"
<< "3) Check Stack is Empty\n"
<< "4) Check Stack is Full\n"
<< "5) Count the Elements in Stack\n"
<< "6) Peek an element in Stack\n"
<< "7) Change a specfic value in Stack\n"
<< "8) Display the whole Stack\n"
<< "9) Clear the Screen\n"
<< "0) For terminating the program\n";
cin >> option;*/
cin >> option;
switch (option)
{
case 0:
cout << "\nHave a Good Day\n";
//stack.~StackByArray();
break;
case 1:
cout << "\nEnter the value you want to push : ";
cin >> value;
stack.push(value);
break;
case 2:
stack.pop();
//cout << " Value popped\n";
break;
case 3:
if (stack.IsEmpty())
{
cout << "\nStack is Empty\n";
}
else
cout << "\nStack is NOT empty\n";
break;
case 4:
if (stack.IsFull())
{
cout << "\nStack is Full\n";
}
else
cout << "\nStack is NOT Full\n";
break;
case 5:
cout << "\nElements in stack are : " << stack.count() << "\n";
break;
case 6:
cout << "\nEnter the index you want to peek : ";
cin >> value;
cout << "\nThe value is : " << stack.peek(value);
break;
case 7:
cout << "\nEnter the value you want to change : ";
cin >> value;
cout << "\nEnter the index : ";
cin >> maxsize;
stack.change(maxsize, value);
break;
case 8:
cout << "\nThe values in Stack are : \n";
stack.display();
break;
case 9:
if(stack.Top(value))
cout << "The element on top is : " << value;
break;
case 10:
system("clear");
//system("Cls"); //for windows visual C++
cout << "Welcome to Stack ADT using array\n"
<< "1) Push value in Stack\n"
<< "2) Pop value in Stack\n"
<< "3) Check Stack is Empty\n"
<< "4) Check Stack is Full\n"
<< "5) Count the Elements in Stack\n"
<< "6) Peek an element in Stack\n"
<< "7) Change a specfic value in Stack\n"
<< "8) Display the whole Stack\n"
<< "9) Clear the Screen\n"
<< "0) For terminating the program\n";
break;
default:
cout << "\nWrong Input\n";
break;
}
}
cout << "--------Program-Ended--------";
return 0;
}
@MahadMuhammad
Copy link
Author

Stack using dynamic arrays in C++

  • This code is implemented with the concepts of OOP's in C++

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment