Advertisement
Phr0zen_Penguin

LinkedStack.cpp - VTC C++ Linked Stack

Apr 29th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. /**
  2.  * [LinkedStack.cpp]
  3.  * The LinkedStack class definition/implementation.
  4.  */
  5.  
  6. #include "LinkedStack.h"
  7.  
  8. /**
  9.  * LinkedStack CLASS CONSTRUCTOR(s):
  10.  */
  11. LinkedStack::LinkedStack()
  12. {
  13.     top = 0;
  14. }
  15.  
  16.  
  17. /**
  18.  * LinkedStack CLASS ACCESSORS:
  19.  */
  20. void LinkedStack::Pop(float &item)
  21. {
  22.     Node    *pTemp;         // creates a new node to point to the element being popped off the stack
  23.  
  24.     item = top->element;    // stores the element being popped off the stack
  25.     pTemp = top;            // moves the current top of the stack to the temporary pointer
  26.     top = top->next;        // points the current top of the stack to the new top
  27.  
  28.     delete pTemp;           // deletes the temporary pointer (destroying the element being popped off the stack)
  29.  
  30.     /**
  31.      * NOTE:
  32.      * The code implemented here facilitates the popping of elements from the beginning of the stack
  33.      * ALWAYS.
  34.      */
  35. }
  36.  
  37.  
  38. /**
  39.  * LinkedStack CLASS MUTATORS:
  40.  */
  41. void LinkedStack::Push(float item)
  42. {
  43.     Node    *newNode = new Node();  // creates a new node to assign the item to be pushed onto the stack to
  44.  
  45.     newNode->element = item;        // assigns the item to be pushed onto the top of the stack to the element part of the new node
  46.     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)
  47.     top = newNode;                  // points the current top of the stack to the new node
  48. }
  49.  
  50.  
  51. /**
  52.  * LinkedStack CLASS GENERAL USE FUNCTIONS:
  53.  */
  54. void LinkedStack::MakeEmpty(void)
  55. {
  56.     Node    *tempPtr;           // a temporary pointer to point to nodes (elements) to be removed from the stack
  57.  
  58.         while(top != 0)         // while the stack still contains elements
  59.         {
  60.             tempPtr = top;      // moves the (current) top of the stack to a temporary pointer
  61.             top = top->next;    // moves the top to the next element on the stack (creating the new top)
  62.  
  63.             delete tempPtr;     // deletes the previous top element of the stack
  64.         }
  65. }
  66.  
  67. bool LinkedStack::IsEmpty(void)
  68. {
  69.     return (top == 0);          // if the top is null, true will be returned, indicating an empty stack, otherwise, false
  70. }
  71.  
  72. bool LinkedStack::IsFull(void)
  73. {
  74.     Node    *pTemp = new Node();    // try to allocate memory for a temporary node
  75.  
  76.         if(pTemp == 0)  // if the attempted memory allocation failed...
  77.             return true;            // ...true is returned, indicating a full stack
  78.         else            // ...
  79.         {
  80.             delete pTemp;           // removes the temporary pointer
  81.  
  82.             return false;           // ...false is returned
  83.         }
  84. }
  85.  
  86. /**
  87.  * LinkedStack CLASS DESTRUCTOR
  88.  */
  89. LinkedStack::~LinkedStack()
  90. {
  91.     MakeEmpty();
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement