⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stackp.cpp

📁 Data Abstraction & Problem Solving with C++源码
💻 CPP
字号:
// *********************************************************// Implementation file StackP.cpp for the ADT stack.// Pointer-based implementation.// *********************************************************#include "StackP.h"  // header file#include <cstddef>   // for NULL#include <cassert>   // for assertStack::Stack() : topPtr(NULL){}  // end default constructorStack::Stack(const Stack& aStack){   if (aStack.topPtr == NULL)      topPtr = NULL;  // original list is empty   else   {  // copy first node      topPtr = new StackNode;      topPtr->item = aStack.topPtr->item;      // copy rest of list      StackNode *newPtr = topPtr;    // new list pointer      for (StackNode *origPtr = aStack.topPtr->next;                      origPtr != NULL;                      origPtr = origPtr->next)      {  newPtr->next = new StackNode;         newPtr = newPtr->next;         newPtr->item = origPtr->item;      }  // end for      newPtr->next = NULL;   }  // end if}  // end copy constructorStack::~Stack(){   // pop until stack is empty   while (!isEmpty())      pop();   // Assertion: topPtr == NULL}  // end destructorbool Stack::isEmpty() const{   return topPtr == NULL;}  // end isEmptyvoid Stack::push(StackItemType newItem){   // create a new node   StackNode *newPtr = new StackNode;   // set data portion  of new node   newPtr->item = newItem;    // insert the new node   newPtr->next = topPtr;   topPtr = newPtr;}  // end pushvoid Stack::pop() throw(StackException){   if (isEmpty())      throw StackException("StackException: stack empty on pop");   else   {  // stack is not empty; delete top      StackNode *temp = topPtr;      topPtr = topPtr->next;      // return deleted node to system      temp->next = NULL;  // safeguard      delete temp;   }  // end if}  // end popvoid Stack::pop(StackItemType& stackTop) throw(StackException){   if (isEmpty())      throw StackException("StackException: stack empty on pop");   else   {  // stack is not empty; retrieve and delete top      stackTop = topPtr->item;      StackNode *temp = topPtr;      topPtr = topPtr->next;      // return deleted node to system      temp->next = NULL;  // safeguard      delete temp;   }  // end if}  // end popvoid Stack::getTop(StackItemType& stackTop) const throw(StackException){   if (isEmpty())      throw StackException("StackException: stack empty on getTop");   else      // stack is not empty; retrieve top      stackTop = topPtr->item;}  // end getTop// End of implementation file.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -