diff --git a/data_structures/stk/main.cpp b/data_structures/stk/main.cpp index 44b7984e0..11215ac4b 100644 --- a/data_structures/stk/main.cpp +++ b/data_structures/stk/main.cpp @@ -8,29 +8,26 @@ * ./main student.txt ************************************************************ * */ -#include +#include #include #include #include #include -#include "stack.cpp" #include "stack.h" -using namespace std; - int main(int argc, char* argv[]) { double GPA; double highestGPA; - string name; + std::string name; assert(argc == 2); - ifstream infile; - stack stk; + std::ifstream infile; + stack stk; infile.open(argv[1]); - cout << fixed << showpoint; - cout << setprecision(2); + std::cout << std::fixed << std::showpoint; + std::cout << std::setprecision(2); infile >> GPA >> name; highestGPA = GPA; @@ -44,12 +41,12 @@ int main(int argc, char* argv[]) { } infile >> GPA >> name; } - cout << "Highest GPA: " << highestGPA << endl; - cout << "Students the highest GPA are: " << endl; + std::cout << "Highest GPA: " << highestGPA << std::endl; + std::cout << "Students the highest GPA are: " << std::endl; while (!stk.isEmptyStack()) { - cout << stk.top() << endl; + std::cout << stk.top() << std::endl; stk.pop(); } - cout << endl; + std::cout << std::endl; return 0; } diff --git a/data_structures/stk/makefile b/data_structures/stk/makefile deleted file mode 100644 index e965d2044..000000000 --- a/data_structures/stk/makefile +++ /dev/null @@ -1,13 +0,0 @@ -CC= g++ -CFLAGS = -c -Wall - -all: main test_stack -stack.o: stack.cpp - $(CC) $(CFLAGS) stack.cpp -test_stack: stack.o - $(CC) test_stack.cpp stack.o -o stk -main: stack.o - $(CC) main.cpp stack.o -o main - -clean: - rm *o stk main diff --git a/data_structures/stk/stack.cpp b/data_structures/stk/stack.cpp index bfa8a353e..23aa3c05a 100644 --- a/data_structures/stk/stack.cpp +++ b/data_structures/stk/stack.cpp @@ -1,8 +1,7 @@ -#include "stack.h" -#include -#include +#include "./stack.h" -using namespace std; +#include +#include /* Default constructor*/ template @@ -19,13 +18,13 @@ stack::~stack() {} template void stack::display() { node *current = stackTop; - cout << "Top --> "; + std::cout << "Top --> "; while (current != NULL) { - cout << current->data << " "; + std::cout << current->data << " "; current = current->next; } - cout << endl; - cout << "Size of stack: " << size << endl; + std::cout << std::endl; + std::cout << "Size of stack: " << size << std::endl; } /* Determine whether the stack is empty */ @@ -68,20 +67,22 @@ void stack::pop() { delete temp; size--; } else { - cout << "Stack is empty !" << endl; + std::cout << "Stack is empty !" << std::endl; } } /* Operator "=" */ template -stack stack::operator=(stack &otherStack) { +stack &stack::operator=(const stack &otherStack) { node *newNode, *current, *last; - if (stackTop != NULL) /* If stack is no empty, make it empty */ + /* If stack is no empty, make it empty */ + if (stackTop != NULL) { stackTop = NULL; - if (otherStack.stackTop == NULL) + } + if (otherStack.stackTop == NULL) { stackTop = NULL; - else { + } else { current = otherStack.stackTop; stackTop = new node; stackTop->data = current->data; @@ -101,3 +102,8 @@ stack stack::operator=(stack &otherStack) { size = otherStack.size; return *this; } + +#include +/** force instantiate to export the type class */ +template class stack; +template class stack; diff --git a/data_structures/stk/stack.h b/data_structures/stk/stack.h index da7788b73..e038440a1 100644 --- a/data_structures/stk/stack.h +++ b/data_structures/stk/stack.h @@ -1,6 +1,6 @@ /* This class specifies the basic operation on a stack as a linked list */ -#ifndef STACK_H -#define STACK_H +#ifndef DATA_STRUCTURES_STK_STACK_H_ +#define DATA_STRUCTURES_STK_STACK_H_ /* Definition of the node */ template @@ -22,11 +22,11 @@ class stack { void pop(); /* Remove the top element of the stack */ void clear(); - stack operator=(stack &otherStack); + stack &operator=(const stack &otherStack); // Overload "=" the assignment operator. private: node *stackTop; /* Pointer to the stack */ int size; }; -#endif +#endif // DATA_STRUCTURES_STK_STACK_H_ diff --git a/data_structures/stk/test_stack.cpp b/data_structures/stk/test_stack.cpp index 098027dfd..aa636916f 100644 --- a/data_structures/stk/test_stack.cpp +++ b/data_structures/stk/test_stack.cpp @@ -1,56 +1,58 @@ #include -#include "stack.cpp" -#include "stack.h" -using namespace std; +#include "./stack.h" int main() { stack stk; - cout << "---------------------- Test construct ----------------------" - << endl; + std::cout << "---------------------- Test construct ----------------------" + << std::endl; stk.display(); - cout << "---------------------- Test isEmptyStack ----------------------" - << endl; + std::cout + << "---------------------- Test isEmptyStack ----------------------" + << std::endl; if (stk.isEmptyStack()) - cout << "PASS" << endl; + std::cout << "PASS" << std::endl; else - cout << "FAIL" << endl; - cout << "---------------------- Test push ----------------------" << endl; - cout << "After pushing 10 20 30 40 into stack: " << endl; + std::cout << "FAIL" << std::endl; + std::cout << "---------------------- Test push ----------------------" + << std::endl; + std::cout << "After pushing 10 20 30 40 into stack: " << std::endl; stk.push(10); stk.push(20); stk.push(30); stk.push(40); stk.display(); - cout << "---------------------- Test top ----------------------" << endl; + std::cout << "---------------------- Test top ----------------------" + << std::endl; int value = stk.top(); if (value == 40) - cout << "PASS" << endl; + std::cout << "PASS" << std::endl; else - cout << "FAIL" << endl; - cout << "---------------------- Test pop ----------------------" << endl; + std::cout << "FAIL" << std::endl; + std::cout << "---------------------- Test pop ----------------------" + << std::endl; stk.display(); stk.pop(); stk.pop(); - cout << "After popping 2 times: " << endl; + std::cout << "After popping 2 times: " << std::endl; stk.display(); - cout << "---------------------- Test overload = operator " - "----------------------" - << endl; + std::cout << "---------------------- Test overload = operator " + "----------------------" + << std::endl; stack stk1; - cout << "stk current: " << endl; + std::cout << "stk current: " << std::endl; stk.display(); - cout << endl << "Assign stk1 = stk " << endl; + std::cout << std::endl << "Assign stk1 = stk " << std::endl; stk1 = stk; stk1.display(); - cout << endl << "After pushing 8 9 10 into stk1:" << endl; + std::cout << std::endl << "After pushing 8 9 10 into stk1:" << std::endl; stk1.push(8); stk1.push(9); stk1.push(10); stk1.display(); - cout << endl << "stk current: " << endl; + std::cout << std::endl << "stk current: " << std::endl; stk.display(); - cout << "Assign back stk = stk1:" << endl; + std::cout << "Assign back stk = stk1:" << std::endl; stk = stk1; stk.display(); return 0;