Add StackArray class

This commit is contained in:
2020-03-23 12:42:02 +00:00
parent 5bbca3d0e9
commit 44d4e2265c
3 changed files with 131 additions and 32 deletions

View File

@@ -1,3 +1,55 @@
#include <lib-datastruct.h>
void StackArray::Push(char val)
{
if (top < MAX && !isEmpty()) {
this->list[++top].data = val;
}
else if (isEmpty()) {
this->list[++top].data = val;
}
else {
std::cout << "Error: List is full!\n";
}
return;
}
char StackArray::Pop()
{
if (!isEmpty()) {
char temp = this->Top();
this->top -= 1;
return temp;
}
return '\0';
}
char StackArray::Top() const
{
return this->list[top].data;
}
void StackArray::Display() const
{
if (isEmpty()) {
std::cout << "Error: Stack is empty\n";
return;
}
int tempTop = this->top;
while (tempTop >= 0) {
std::cout << "Value at list [" << tempTop << "]: "
<< this->list[tempTop].data << std::endl;
tempTop--;
}
return;
}
bool StackArray::isEmpty() const
{
return this->top <= EMPTY;
}