MP4
Stacks and Queues
Stack< T > Class Template Reference

Stack class: represents a standard stack. More...

#include "stack.h"

+ Inheritance diagram for Stack< T >:
+ Collaboration diagram for Stack< T >:

Public Member Functions

void push (const T &newItem)
 Adds the parameter object to the top of the Stack. More...
 
pop ()
 Removes the object on top of the Stack, and returns it. More...
 
void add (const T &theItem)
 Adds an element to the ordering structure. More...
 
remove ()
 Removes an element from the ordering structure. More...
 
peek ()
 Finds the object on top of the Stack, and returns it to the caller. More...
 
bool isEmpty () const
 Determines if the Stack is empty. More...
 
- Public Member Functions inherited from OrderingStructure< T >
virtual ~OrderingStructure ()
 Destructor for the OrderingStructure. More...
 

Private Attributes

list< T > myStack
 The list representing our Stack: the front of the list corresponds to the top of the Stack. More...
 

Detailed Description

template<class T>
class Stack< T >

Stack class: represents a standard stack.

Templated to hold elements of any type.

You should not modify this file for the MP!

Author
CS 225 course staff
Date
Spring 2007
Author
Daniel Hoodin
Date
Spring 2008
Author
Chase Geigle
Date
Fall 2012

Member Function Documentation

template<class T >
void Stack< T >::push ( const T &  newItem)

Adds the parameter object to the top of the Stack.

That is, the element should go at the beginning of the list.

Note
This function must be O(1)!
Parameters
newItemThe object to be added to the Stack.
Todo:
Your code here!
template<class T >
T Stack< T >::pop ( )

Removes the object on top of the Stack, and returns it.

That is, remove the element at the beginning of the list. You may assume this function is only called when the Stack is not empty.

Note
This function must be O(1)!
Returns
The element that used to be at the top of the Stack.
Todo:
Your code here! You will have to replace the following line.
template<class T >
void Stack< T >::add ( const T &  theItem)
virtual

Adds an element to the ordering structure.

See also
OrderingStructure::add()
Todo:
Your code here! Hint: this should call another Stack function to add the element to the Stack.

Implements OrderingStructure< T >.

template<class T >
T Stack< T >::remove ( )
virtual

Removes an element from the ordering structure.

See also
OrderingStructure::remove()
Todo:
Your code here! Hint: this should call another Stack function to remove an element from the Stack and return it. You will need to replace the following line.

Implements OrderingStructure< T >.

template<class T >
T Stack< T >::peek ( )
virtual

Finds the object on top of the Stack, and returns it to the caller.

Unlike pop(), this operation does not alter the Stack itself. It should look at the beginning of the list. You may assume this function is only called when the Stack is not empty.

Note
This function must be O(1)!
Returns
The element at the top of the Stack.
Todo:
Your code here! You will need to replace the following line.

Implements OrderingStructure< T >.

template<class T >
bool Stack< T >::isEmpty ( ) const
virtual

Determines if the Stack is empty.

Note
This function must be O(1)! Note that the std::list's size() function is O(n), so you should not attempt to use it here.
Returns
Whether or not the stack is empty (bool).
Todo:
Your code here! You will need to replace the following line.

Implements OrderingStructure< T >.

Member Data Documentation

template<class T >
list<T> Stack< T >::myStack
private

The list representing our Stack: the front of the list corresponds to the top of the Stack.


The documentation for this class was generated from the following files: