Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * [LinkedStack.cpp]
- * The LinkedStack class definition/implementation.
- */
- #include "LinkedStack.h"
- /**
- * LinkedStack CLASS CONSTRUCTOR(s):
- */
- LinkedStack::LinkedStack()
- {
- top = 0;
- }
- /**
- * LinkedStack CLASS ACCESSORS:
- */
- void LinkedStack::Pop(float &item)
- {
- Node *pTemp; // creates a new node to point to the element being popped off the stack
- item = top->element; // stores the element being popped off the stack
- pTemp = top; // moves the current top of the stack to the temporary pointer
- top = top->next; // points the current top of the stack to the new top
- delete pTemp; // deletes the temporary pointer (destroying the element being popped off the stack)
- /**
- * NOTE:
- * The code implemented here facilitates the popping of elements from the beginning of the stack
- * ALWAYS.
- */
- }
- /**
- * LinkedStack CLASS MUTATORS:
- */
- void LinkedStack::Push(float item)
- {
- Node *newNode = new Node(); // creates a new node to assign the item to be pushed onto the stack to
- newNode->element = item; // assigns the item to be pushed onto the top of the stack to the element part of the new node
- newNode->next = top; // points the next of the new node to the (new) top of the stack (because the item will be added before the top)
- top = newNode; // points the current top of the stack to the new node
- }
- /**
- * LinkedStack CLASS GENERAL USE FUNCTIONS:
- */
- void LinkedStack::MakeEmpty(void)
- {
- Node *tempPtr; // a temporary pointer to point to nodes (elements) to be removed from the stack
- while(top != 0) // while the stack still contains elements
- {
- tempPtr = top; // moves the (current) top of the stack to a temporary pointer
- top = top->next; // moves the top to the next element on the stack (creating the new top)
- delete tempPtr; // deletes the previous top element of the stack
- }
- }
- bool LinkedStack::IsEmpty(void)
- {
- return (top == 0); // if the top is null, true will be returned, indicating an empty stack, otherwise, false
- }
- bool LinkedStack::IsFull(void)
- {
- Node *pTemp = new Node(); // try to allocate memory for a temporary node
- if(pTemp == 0) // if the attempted memory allocation failed...
- return true; // ...true is returned, indicating a full stack
- else // ...
- {
- delete pTemp; // removes the temporary pointer
- return false; // ...false is returned
- }
- }
- /**
- * LinkedStack CLASS DESTRUCTOR
- */
- LinkedStack::~LinkedStack()
- {
- MakeEmpty();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement