Add StackArray class

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

View File

@ -1,20 +1,23 @@
#include <lib-datastruct.h>
#include <iostream>
enum OPS {
EXIT, LISTS, STACKS, QUEUES
};
// StackArray menu
void Stack();
// Main menu
int main ()
{
// std::cout << "Running driver program version " << DS_VERSION;
enum OPS {
EXIT, LISTS, STACKS, QUEUES
};
bool exit = false;
LinkedList<int> t;
int choice;
while (!exit) {
std::cout << "Enter a choice below...\n\t0. Exit"
<< "\n\t1. LISTS\n\t2. STACKS\n\t3. QUEUES\n";
std::cout << "\n##### Main Menu #####\n"
<< "Enter a choice below...\n\t0. Exit"
<< "\n\t1. Lists\n\t2. Stacks\n\t3. Queues\n";
std::cin >> choice;
std::cin.clear();
@ -27,6 +30,7 @@ int main ()
break;
case STACKS: // 2
Stack();
break;
case QUEUES: // 3
@ -40,3 +44,56 @@ int main ()
return 0;
}
// StackArray menu
void Stack()
{
enum STACK_OPS {
EXIT, PUSH, POP, TOP, DISPLAY
};
StackArray stack;
bool exit = false;
int choice;
while (!exit) {
std::cout << "\n##### StackArray Menu #####\n"
<< "Enter a choice below...\n\t0. Exit"
<< "\n\t1. Push\n\t2. Pop\n\t3. Top\n\t4. Display\n";
std::cin >> choice;
std::cin.clear();
switch (choice) {
case EXIT: // 0
exit = true;
break;
case PUSH: // 1
char val;
std::cout << "Enter a character to push to our stack: ";
std::cin >> val;
std::cin.clear();
stack.Push(val);
break;
case POP: // 2
std::cout << "[" << stack.Pop() << "] has been removed from our stack\n";
break;
case TOP: // 3
std::cout << "[" << stack.Top() << "] is at the top of our stack\n";
break;
case DISPLAY: // 4
stack.Display();
break;
default:
std::cout << "Invalid option selected\n";
break;
}
}
}

View File

@ -3,36 +3,26 @@
#include <iostream>
enum CONST {
MAX=10
};
class StackArray {
public:
StackArray() : top(EMPTY) {};
void Push(char val);
char Pop();
char Top() const;
void Display() const;
bool isEmpty() const;
template <typename T>
class ListNode {
private:
enum { EMPTY=-1, MAX=10 };
class ListNode {
public:
ListNode() : next(NULL) {};
ListNode(T val) : data(val), next(NULL) {};
// Sneak more of Push() through node concstr?
// ListNode(T val, LinkedList& l) data(val), next(l.Top());
private:
T data;
ListNode(char val) : data(val), next(NULL) {};
char data;
ListNode* next;
};
template <typename T>
class LinkedList {
public:
LinkedList() {};
void Push(T val) {};
T Pop();
T Top();
void Display() const;
private:
// ListNode data[MAX];
};
ListNode list[MAX];
int top;
};
#endif

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;
}