CS215stack(4o5)

CS215stack(4o5) - = topPtr->next delete tempPtr...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 #include <iostream> using namespace std #include "CS215stack.h" CS215stack::CS215stack( ) // constructor // Default constructor. // POST: Stack is created and empty. { topPtr = NULL } bool CS215stack::IsFull( ) const // Returns true if stack is full // always return false (linked list stack) { return ( false ) } bool CS215stack::IsEmpty( ) const // Returns true if there are no elements // on the stack { return ( topPtr == NULL ) } void CS215stack::Push ( int newItem ) // Adds newItem to the top of the stack. { NodeType* location location = new NodeType location->info = newItem location->next = topPtr topPtr = location } void CS215stack::Pop ( int& item ) // Removes element at the top of the stack and // returns it in item. { NodeType* tempPtr item = topPtr->info tempPtr = topPtr topPtr
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: = topPtr-&gt;next delete tempPtr } void CS215stack::MakeEmpty( ) // Post: Stack is empty { NodeType* tempPtr // Delete elements until NULL pointer reached while ( topPtr != NULL ) { tempPtr = topPtr topPtr = topPtr-&gt;next delete tempPtr // Empty stack false otherwise // Get new node storage // Put data into it // Point it to previous top // It is now new top // Return top of stack // Save its address for delete // Point to next element in stack // Delete top of stack node storage all elements deallocated. // Save node address for delete // Point to next element // Delete this element Page 1 Sheet1 } } CS215stack::~CS215stack( ) // destructor { MakeEmpty( ) } Page 2...
View Full Document

This note was uploaded on 10/13/2008 for the course CS 215 taught by Professor Piwowarski during the Spring '06 term at Kentucky.

Page1 / 3

CS215stack(4o5) - = topPtr-&amp;amp;gt;next delete tempPtr...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online