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 <lib-datastruct.h>
#include <iostream> #include <iostream>
enum OPS { // StackArray menu
EXIT, LISTS, STACKS, QUEUES void Stack();
};
// Main menu
int main () int main ()
{ {
// std::cout << "Running driver program version " << DS_VERSION; // std::cout << "Running driver program version " << DS_VERSION;
enum OPS {
EXIT, LISTS, STACKS, QUEUES
};
bool exit = false; bool exit = false;
LinkedList<int> t;
int choice; int choice;
while (!exit) { while (!exit) {
std::cout << "Enter a choice below...\n\t0. Exit" std::cout << "\n##### Main Menu #####\n"
<< "\n\t1. LISTS\n\t2. STACKS\n\t3. QUEUES\n"; << "Enter a choice below...\n\t0. Exit"
<< "\n\t1. Lists\n\t2. Stacks\n\t3. Queues\n";
std::cin >> choice; std::cin >> choice;
std::cin.clear(); std::cin.clear();
@ -27,6 +30,7 @@ int main ()
break; break;
case STACKS: // 2 case STACKS: // 2
Stack();
break; break;
case QUEUES: // 3 case QUEUES: // 3
@ -40,3 +44,56 @@ int main ()
return 0; 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> #include <iostream>
enum CONST { class StackArray {
MAX=10
};
template <typename T>
class ListNode {
public: public:
ListNode() : next(NULL) {}; StackArray() : top(EMPTY) {};
ListNode(T val) : data(val), next(NULL) {}; void Push(char val);
// Sneak more of Push() through node concstr? char Pop();
// ListNode(T val, LinkedList& l) data(val), next(l.Top()); char Top() const;
private:
T data;
ListNode* next;
};
template <typename T>
class LinkedList {
public:
LinkedList() {};
void Push(T val) {};
T Pop();
T Top();
void Display() const; void Display() const;
bool isEmpty() const;
private: private:
// ListNode data[MAX]; enum { EMPTY=-1, MAX=10 };
class ListNode {
public:
ListNode() : next(NULL) {};
ListNode(char val) : data(val), next(NULL) {};
char data;
ListNode* next;
};
ListNode list[MAX];
int top;
}; };
#endif #endif

View File

@ -1,3 +1,55 @@
#include <lib-datastruct.h> #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;
}