Stacks: Data Structures Using C++ 1
Stacks: Data Structures Using C++ 1
Stacks
template<class Type>
void stackType<Type>::destroyStack()
{
stackTop = 0;
}//end destroyStack
Data Structures Using C++ 13
emptyStack and fullStack
template<class Type>
bool stackType<Type>::isEmptyStack()
{
return(stackTop == 0);
}//end isEmptyStack
template<class Type>
bool stackType<Type>::isFullStack()
{
return(stackTop == maxStackSize);
}//end isFullStack
Data Structures Using C++ 14
Push
template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
list = NULL;
copyStack(otherStack);
}//end copy constructor
#ifndef H_StackType
#define H_StackType
#include <iostream>
#include <cassert>
3.8 Lisa
3.6 John
3.9 Susan
3.7 Kathy
3.4 Jason
3.9 David
3.4 Jack
Data Structures Using C++ 25
Programming Example: Highest
GPA (Algorithm)
1. Declare the variables.
2. Open the input file.
3. If the input file does not exist, exit the program.
4. Set the output of the floating-point numbers to a
fixed decimal format with a decimal point and
trailing zeroes. Also, set the precision to two
decimal places.
5. Read the GPA and student name.
6. highestGPA = GPA;
7. Initialize the stack.
Data Structures Using C++ 26
Programming Example: Highest
GPA (Algorithm)
8. while (not end of file)
{
8.1 if (GPA > highestGPA)
{
8.1.1 destroyStack(stack);
8.1.2 push(stack, student name);
8.1.3 highestGPA = GPA;
}
8.2 else
if(GPA is equal to highestGPA)
push(stack, student name);
8.3 Read the GPA and student name;
}
Stack after the statements temp = stackTop; Stack after the statement delete temp;
and stackTop = stackTop->link; execute executes
Stack after pushing 6 Stack after retrieving the top two elements
and popping twice
Stack after retrieving the top two elements Stack after popping the element
and popping twice
Data Structures Using C++ 43
Postfix Expression Calculator (Main Algorithm)