From ad23bbdf702fb3de2d0466551e2fc5e3ae0ebeb1 Mon Sep 17 00:00:00 2001 From: danghai Date: Sun, 12 Jan 2020 16:53:13 -0800 Subject: [PATCH] Add a simple class stack, and example --- data_structure/stk/main.cpp | 55 ++++++++++++++ data_structure/stk/makefile | 13 ++++ data_structure/stk/stack.cpp | 114 ++++++++++++++++++++++++++++++ data_structure/stk/stack.h | 35 +++++++++ data_structure/stk/student.txt | 17 +++++ data_structure/stk/test_stack.cpp | 54 ++++++++++++++ 6 files changed, 288 insertions(+) create mode 100644 data_structure/stk/main.cpp create mode 100644 data_structure/stk/makefile create mode 100644 data_structure/stk/stack.cpp create mode 100644 data_structure/stk/stack.h create mode 100644 data_structure/stk/student.txt create mode 100644 data_structure/stk/test_stack.cpp diff --git a/data_structure/stk/main.cpp b/data_structure/stk/main.cpp new file mode 100644 index 000000000..2d6bbec56 --- /dev/null +++ b/data_structure/stk/main.cpp @@ -0,0 +1,55 @@ +/* + * This program reads a data file consisting of students' GPAs + * followed by their names. The program then prints the highest + * GPA and the names of the students with the highest GPA. + * It uses stack to store the names of the students + * Run: + * make all + * ./main student.txt + ************************************************************ + * */ +#include +#include +#include +#include +#include + +#include "stack.h" +#include "stack.cpp" + +using namespace std; + +int main(int argc, char * argv[]) { + double GPA; + double highestGPA; + string name; + + assert(argc == 2); + ifstream infile; + stack stk; + + infile.open(argv[1]); + cout << fixed << showpoint; + cout << setprecision(2); + infile >> GPA >> name; + highestGPA = GPA; + + while (infile) { + if (GPA > highestGPA) { + stk.clear(); + stk.push(name); + highestGPA = GPA; + } else if (GPA == highestGPA) { + stk.push(name); + } + infile >> GPA >> name; + } + cout << "Highest GPA: " << highestGPA < +#include +#include "stack.h" + +using namespace std; + +/* Default constructor*/ +template +stack::stack() +{ + stackTop = NULL; + size = 0; +} + +/* Destructor */ +template +stack::~stack() +{ +} + +/* Display for testing */ +template +void stack::display() +{ + node *current = stackTop; + cout << "Top --> "; + while(current != NULL) { + cout<data<< " "; + current = current -> next; + } + cout < +bool stack::isEmptyStack() +{ + return (stackTop == NULL); +} + +/* Clear stack */ +template +void stack::clear() +{ + stackTop = NULL; +} + +/* Add new item to the stack */ +template +void stack::push(Type item) +{ + node *newNode; + newNode = new node; + newNode->data = item; + newNode->next = stackTop; + stackTop = newNode; + size++; +} + +/* Return the top element of the stack */ +template +Type stack::top() +{ + assert(stackTop != NULL); + return stackTop->data; +} + +/* Remove the top element of the stack */ +template +void stack::pop() +{ + node *temp; + if(!isEmptyStack()) { + temp = stackTop; + stackTop = stackTop->next; + delete temp; + size--; + } else { + cout << "Stack is empty !" << endl; + } +} + +/* Operator "=" */ +template +stack stack::operator=(stack & otherStack) +{ + node *newNode, *current, *last; + + if (stackTop != NULL) /* If stack is no empty, make it empty */ + stackTop = NULL; + if (otherStack.stackTop == NULL) + stackTop = NULL; + else { + current = otherStack.stackTop; + stackTop = new node; + stackTop->data = current->data; + stackTop->next = NULL; + last = stackTop; + current = current ->next; + /* Copy the remaining stack */ + while (current != NULL) + { + newNode = new node; + newNode->data = current->data; + newNode->next = NULL; + last->next = newNode; + last = newNode; + current = current->next; + } + } + size = otherStack.size; + return *this; +} diff --git a/data_structure/stk/stack.h b/data_structure/stk/stack.h new file mode 100644 index 000000000..a93669482 --- /dev/null +++ b/data_structure/stk/stack.h @@ -0,0 +1,35 @@ +/* This class specifies the basic operation on a stack as a linked list */ +#ifndef STACK_H +#define STACK_H + +/* Definition of the node */ +template +struct node +{ + Type data; + node *next; +}; + +/* Definition of the stack class */ +template +class stack +{ + public: + void display(); /* Show stack */ + stack(); /* Default constructor*/ + ~stack(); /* Destructor */ + bool isEmptyStack(); /* Determine whether the stack is empty */ + void push (Type item); /* Add new item to the stack */ + Type top(); /* Return the top element of the stack */ + void pop(); /* Remove the top element of the stack */ + void clear(); + + stack operator=(stack & otherStack); + // Overload "=" the assignment operator. + private: + node *stackTop; /* Pointer to the stack */ + int size; +}; + +#endif + diff --git a/data_structure/stk/student.txt b/data_structure/stk/student.txt new file mode 100644 index 000000000..b7e3b9e79 --- /dev/null +++ b/data_structure/stk/student.txt @@ -0,0 +1,17 @@ +3.4 Tom +3.2 Kathy +2.5 Hoang +3.4 Tom +3.8 Randy +3.9 Kingston +3.8 Mickey +3.6 Peter +3.5 Donald +3.8 Cindy +3.7 Dome +3.9 Andy +3.8 Hai +3.9 Minnie +2.7 Gilda +3.9 Vinay +3.4 Hiral diff --git a/data_structure/stk/test_stack.cpp b/data_structure/stk/test_stack.cpp new file mode 100644 index 000000000..4703fc906 --- /dev/null +++ b/data_structure/stk/test_stack.cpp @@ -0,0 +1,54 @@ +#include +#include "stack.h" +#include "stack.cpp" + +using namespace std; + +int main() +{ + stack stk; + cout << "---------------------- Test construct ----------------------" << endl; + stk.display(); + cout << "---------------------- Test isEmptyStack ----------------------" << endl; + if(stk.isEmptyStack()) + cout << "PASS" < stk1; + cout << "stk current: "<< endl; + stk.display(); + cout << endl << "Assign stk1 = stk "<< endl; + stk1 = stk; + stk1.display(); + cout << endl<< "After pushing 8 9 10 into stk1:" <