📄 stack.cc
字号:
//// *********************************************************************// *// * Author : Gerald Carter// * Filename : stack.cc// * Date Created : 960422// * Project : Compx Timing Tool// *// * Description // * ------------// * This is a stack implementation for the generation of the // * Maple Expression Tree.// *// * --------------------// * Modifications// * --------------------// *// **********************************************************************////g++ template pragma#pragma implementation// INCLUDE FILES#include <iostream.h> // I/O stream (cin, cout, cerr)#include "logic.h" // boolean typedef and associated macros#include "stack.h" // stack template declaration#include "maple.h" // mapleNode class for template instaniation// ####################################################################// ## class stack// ##// *******************************************************************// CONSTRUCTOR// Initialize stack<T> to another stack<T>template <class T> stack<T>::stack (const stack<T>& init_stack){ stackNode<T, 2> *iterate = init_stack.top; stackNode<T, 2> *tag = 0; node_count = init_stack.node_count; while (tag = (stackNode<T, 2>*)iterate->GetLink (PREVIOUS)) iterate = tag; if (init_stack.IsEmpty()) top = 0; else { top = new stackNode<T, 2>; top->SetData(iterate->GetData()); iterate = (stackNode<T, 2>*)iterate->GetLink (NEXT); while ((stackNode<T, 2>*) iterate->GetLink(NEXT)) { tag = top; top->SetLink(NEXT, new stackNode<T, 2>); top = (stackNode<T, 2>*)top->GetLink(NEXT); top->SetLink(PREVIOUS, tag); top->SetData(iterate->GetData()); iterate = (stackNode<T, 2>*)iterate->GetLink (NEXT); } }}// *******************************************************************// DESTRUCTOR// Delete all nodestemplate <class T> stack<T>::~stack (void){ // delete stack except one item if (!(IsEmpty())) { while (top->GetLink(PREVIOUS)) { top = (stackNode<T, 2>*)top->GetLink(PREVIOUS); delete (top->GetLink(NEXT)); } delete top; // delete last item }}// *******************************************************************// GENERAL METHODS// Pop top object from stacktemplate <class T> T stack<T>::Pop (void){ T result; if (IsEmpty()) { cerr << "Empty Stack.\n"; return result; } result = top->GetData(); if ((stackNode<T ,2>*)top->GetLink(PREVIOUS) != 0) { top = (stackNode<T ,2>*)top->GetLink(PREVIOUS); delete (top->GetLink(NEXT)); top->SetLink(NEXT, 0); } else { delete (top); top = 0; } --node_count; return result;}// *******************************************************************// Push object onto top of stacktemplate <class T>boolean stack<T>::Push (const T& item){ stackNode<T, 2> *temp = 0; if (IsEmpty()) { top = new stackNode<T, 2>; if (top == 0) { cerr << "Unable to allocate space for a new node in the stack.\n"; return FALSE; } top->SetData(item); top->SetLink(NEXT, 0); top->SetLink(PREVIOUS, 0); } else { // Try to allocate space for a new node temp = new stackNode<T, 2>; if (temp == 0) { cerr << "Unable to allocate space for a new node in the stack.\n"; return FALSE; } // now set the links top->SetLink(NEXT, temp); temp->SetLink(PREVIOUS, top); temp->SetData(item); top = temp; } ++node_count; return TRUE;}// *******************************************************************// Empty the stack of all contentstemplate <class T> void stack<T>::Dump (void){ // delete stack except one item while (top->GetLink(PREVIOUS)) { top = (stackNode<T, 2>*)top->GetLink(PREVIOUS); delete (top->GetLink(NEXT)); } delete top; // delete last item // reset initial values node_count = 0; top = 0;}// *******************************************************************// Print the contents of the stacktemplate <class T> void stack<T>::Print (ostream& S){ stackNode<T, 2> *temp = top; while (temp->GetLink(PREVIOUS)) temp = (stackNode<T, 2>*)temp->GetLink(PREVIOUS); while (temp) { S << temp->GetData() << "\n"; temp = (stackNode<T, 2>*)temp->GetLink(NEXT); }}// *******************************************************************// Instantiation for the templatetypedef stack<mapleNode*> dummmyMapleStack;typedef stack<char*> dummyCharStack;//********** end of stack.cc *******************************************//**********************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -