Add a simple class stack, and example

This commit is contained in:
danghai
2020-01-12 16:53:13 -08:00
parent 02951f1e00
commit ad23bbdf70
6 changed files with 288 additions and 0 deletions

View File

@@ -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 <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <assert.h>
#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<string> 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 <<endl;
cout << "Students the highest GPA are: " << endl;
while (!stk.isEmptyStack()) {
cout << stk.top() << endl;
stk.pop();
}
cout << endl;
return 0;
}

View File

@@ -0,0 +1,13 @@
CC= g++
CFLAGS = -c -Wall
all: clean 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

View File

@@ -0,0 +1,114 @@
#include <iostream>
#include <assert.h>
#include "stack.h"
using namespace std;
/* Default constructor*/
template <class Type>
stack<Type>::stack()
{
stackTop = NULL;
size = 0;
}
/* Destructor */
template <class Type>
stack<Type>::~stack()
{
}
/* Display for testing */
template <class Type>
void stack<Type>::display()
{
node<Type> *current = stackTop;
cout << "Top --> ";
while(current != NULL) {
cout<<current->data<< " ";
current = current -> next;
}
cout <<endl;
cout << "Size of stack: " << size << endl;
}
/* Determine whether the stack is empty */
template <class Type>
bool stack<Type>::isEmptyStack()
{
return (stackTop == NULL);
}
/* Clear stack */
template <class Type>
void stack<Type>::clear()
{
stackTop = NULL;
}
/* Add new item to the stack */
template <class Type>
void stack<Type>::push(Type item)
{
node<Type> *newNode;
newNode = new node<Type>;
newNode->data = item;
newNode->next = stackTop;
stackTop = newNode;
size++;
}
/* Return the top element of the stack */
template <class Type>
Type stack<Type>::top()
{
assert(stackTop != NULL);
return stackTop->data;
}
/* Remove the top element of the stack */
template <class Type>
void stack<Type>::pop()
{
node<Type> *temp;
if(!isEmptyStack()) {
temp = stackTop;
stackTop = stackTop->next;
delete temp;
size--;
} else {
cout << "Stack is empty !" << endl;
}
}
/* Operator "=" */
template <class Type>
stack<Type> stack<Type>::operator=(stack<Type> & otherStack)
{
node<Type> *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<Type>;
stackTop->data = current->data;
stackTop->next = NULL;
last = stackTop;
current = current ->next;
/* Copy the remaining stack */
while (current != NULL)
{
newNode = new node<Type>;
newNode->data = current->data;
newNode->next = NULL;
last->next = newNode;
last = newNode;
current = current->next;
}
}
size = otherStack.size;
return *this;
}

View File

@@ -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 <class Type>
struct node
{
Type data;
node<Type> *next;
};
/* Definition of the stack class */
template <class Type>
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<Type> operator=(stack<Type> & otherStack);
// Overload "=" the assignment operator.
private:
node<Type> *stackTop; /* Pointer to the stack */
int size;
};
#endif

View File

@@ -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

View File

@@ -0,0 +1,54 @@
#include <iostream>
#include "stack.h"
#include "stack.cpp"
using namespace std;
int main()
{
stack<int> stk;
cout << "---------------------- Test construct ----------------------" << endl;
stk.display();
cout << "---------------------- Test isEmptyStack ----------------------" << endl;
if(stk.isEmptyStack())
cout << "PASS" <<endl;
else
cout << "FAIL" <<endl;
cout << "---------------------- Test push ----------------------" << endl;
cout << "After pushing 10 20 30 40 into stack: "<<endl;
stk.push(10);
stk.push(20);
stk.push(30);
stk.push(40);
stk.display();
cout << "---------------------- Test top ----------------------" << endl;
int value = stk.top();
if (value == 40)
cout << "PASS" <<endl;
else
cout << "FAIL" <<endl;
cout << "---------------------- Test pop ----------------------" << endl;
stk.display();
stk.pop();
stk.pop();
cout << "After popping 2 times: "<< endl;
stk.display();
cout << "---------------------- Test overload = operator ----------------------" << endl;
stack<int> 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:" <<endl;
stk1.push(8);
stk1.push(9);
stk1.push(10);
stk1.display();
cout << endl << "stk current: " <<endl;
stk.display();
cout << "Assign back stk = stk1:" << endl;
stk = stk1;
stk.display();
return 0;
}