📄 stack.h
字号:
// method: isLast // boolean isLast() { return stack_d.isLast(); } // method: isEmpty // boolean isEmpty() { return stack_d.isEmpty(); } //--------------------------------------------------------------------------- // // private methods // //---------------------------------------------------------------------------private:};//-----------------------------------------------------------------------------//// we define non-integral constants at the end of class definition for// templates (for non-templates these are defined in the default constructor)// //-----------------------------------------------------------------------------// constants: required constants such as the class name//template <class TObject>const String Stack<TObject>::CLASS_NAME(L"Stack");template <class TObject>const String Stack<TObject>::DEF_PARAM(L"values");// static instantiations: debug level//template <class TObject>Integral::DEBUG Stack<TObject>::debug_level_d = Integral::NONE;// static instantiations: the memory manager//template <class TObject>MemoryManager Stack<TObject>::mgr_d(sizeof(Stack<TObject>), CLASS_NAME);// below are all the methods for the Stack template class// //----------------------------------------------------------------------//// required static methods////----------------------------------------------------------------------// method: name//// arguments: none//// return: a static String& containing the class name//// this method returns the class name//template<class TObject>const String& Stack<TObject>::name() { // create the static name string for this class and return it // static String cname(CLASS_NAME); cname.clear(); cname.concat(CLASS_NAME); cname.concat(L"<"); cname.concat(TObject::name()); cname.concat(L">"); // return the name // return cname;}//----------------------------------------------------------------------//// required debug methods////----------------------------------------------------------------------// method: debug//// arguments:// const unichar* message: (input) information message//// return: a boolean value indicating status//// this method dumps the contents of an object to the console// template<class TObject>boolean Stack<TObject>::debug(const unichar* message_a) const { // declare local variables // String output; String value; // print out the memory allocation mode // output.debugStr(name(), message_a, L"alloc_d", NameMap::ALLOCATION_MAP((long)alloc_d)); Console::put(output); // output the debug level // value.assign(debug_level_d); output.debugStr(name(), message_a, L"debug_level_d", value); Console::put(output); // print out the top of stack message // value.assign(L"top of stack"); Console::put(value); // increment the indention level // Console::increaseIndention(); // call the list debug method // stack_d.debug(message_a); // decrement the indention level // Console::decreaseIndention(); // exit gracefully // return true;}//------------------------------------------------------------------------//// required destructor/constructor(s)////-----------------------------------------------------------------------// method: default constructor//// arguments:// ALLOCATION alloc: (input) the flag to specify whether or not the item// memory is allocated by the node itself//// return: none//template<class TObject>Stack<TObject>::Stack(ALLOCATION alloc_a) { // set the allocation flag // alloc_d = alloc_a; // the list object must be in USER mode // stack_d.setAllocationMode(USER);}//----------------------------------------------------------------------//// required assign methods////----------------------------------------------------------------------// method: assign//// arguments:// const Stack<TObject>& copy_stack: (input) stack to be copied//// return: a boolean value indicating status//// this method copies the elements in stack passed in to the current stack// template<class TObject>boolean Stack<TObject>::assign(const Stack<TObject>& copy_stack_a) { // error when the current stack is in USER mode and the stack to be copied // is in SYSTEM mode // if ((alloc_d == USER) && (copy_stack_a.getAllocationMode() == SYSTEM)) { return Error::handle(name(), L"assign", Error::ARG, __FILE__, __LINE__); } // if this stack is in system mode then temporarily set the list // to system mode and assign. this has the side-effect of copying the // data to this list // alloc_d = copy_stack_a.alloc_d; stack_d.setAllocationMode(alloc_d); stack_d.assign(copy_stack_a.stack_d); stack_d.setAllocationMode(USER); // reposition the stack // stack_d.gotoFirst(); // exit gracefully // return true;}//---------------------------------------------------------------------------//// class-specific public methods:// stack push and pop methods////---------------------------------------------------------------------------// method: push//// arguments:// TObject* item: (input) the item pushed// // return: a boolean value indicating status//// this method pushes a item to the top of the stack//template<class TObject>boolean Stack<TObject>::push(TObject* item_a) { // declare local variables // TObject* new_obj = (TObject*)NULL; // mode: SYSTEM // make a copy of the element // if (alloc_d == SYSTEM) { new_obj = new TObject(*item_a); } // mode: USER // make a copy of the pointer to the item // else { new_obj = item_a; } // push the object to the top of the stack // return stack_d.insertFirst(new_obj);}// method: push//// arguments:// Stack<TObject>& item_stack: (input) stack to be pushed// // return: a boolean value indicating status//// this method pushes a stack to the top of another stack//template<class TObject>boolean Stack<TObject>::push(Stack<TObject>& item_stack_a) { // if the stack is empty exit // if (item_stack_a.isEmpty()) { return false; } // loop over the stack and add elements // else { // move to the last element in the stack // item_stack_a.stack_d.gotoLast(); // add the last item to the stack // push(item_stack_a.stack_d.getCurr()); // loop over all elements in the input stack // while (item_stack_a.stack_d.gotoPrev()) { // push the element on the stack // push(item_stack_a.stack_d.getCurr()); } } // exit gracefully // return true;}// method: push//// arguments:// TObject** items: (input) list of items to be pushed// long num_items: (input) number of items in the list// // return: a boolean value indicating status//// this method pushes num_items from the items list to the top of the stack//template<class TObject>boolean Stack<TObject>::push(TObject** items_a, long num_items_a) { // loop over all elements in the list and push each one // for (long i = num_items_a - 1; i >= 0; i--) { if (!push(items_a[i])) { return false; } } // exit gracefully // return true;}// method: peek//// arguments: none// // return: TObject* of the item on top of the stack//// this method lets the user manipulate the item on top of the stack without// removing the item from the stack//template<class TObject>const TObject* Stack<TObject>::peek() const { // if the stack is empty then return an error // if (isEmpty()) { Error::handle(name(), L"peek", Stack::ERR_EMPTY, __FILE__, __LINE__); return (TObject*)NULL; } // return the pointer // return stack_d.getCurr();}// method: peek//// arguments: none// // return: const TObject* of the item on top of the stack//// this method lets the user manipulate the item on top of the stack without// removing the item from the stack//template<class TObject>TObject* Stack<TObject>::peek() { // if the stack is empty then return an error // if (isEmpty()) { Error::handle(name(), L"peek", Stack::ERR_EMPTY, __FILE__, __LINE__); return (TObject*)NULL; } // return the pointer // return stack_d.getCurr();}// method: pop//// arguments:// TObject* item: (input) memory space for the object// // return: a TObject* pointer to the object pop'd//// this method pops the top item off of the list and returns it to the user//template<class TObject>TObject* Stack<TObject>::pop(TObject* item_a) { // declare local variables // boolean result = false; TObject* ptr = (TObject*)NULL; // when in SYSTEM mode the item passed in should not be // null, if we are in reference mode the item should be null // if (((alloc_d == SYSTEM) && (item_a == (TObject*)NULL)) || ((alloc_d == USER) && (item_a != (TObject*)NULL))) { Error::handle(name(), L"pop", ERR, __FILE__, __LINE__); return (TObject*)NULL; } // when in USER mode the item should be null // if ((alloc_d == USER) && (item_a != (TObject*)NULL)) { Error::handle(name(), L"pop", ERR, __FILE__, __LINE__); return (TObject*)NULL; } // if the stack is empty exit // if (!isEmpty()) { // the returned item is valid so remove the item and make sure we are // positioned at the top of the stack // result = stack_d.removeFirst(ptr); stack_d.gotoFirst(); // mode: SYSTEM // if (alloc_d == SYSTEM) { // make a copy of the object // item_a->assign(*ptr); // delete the reference to the object // delete ptr; // return a pointer to the object (so that the return value can // be compared). // return item_a; } // mode: USER // else { return ptr; } } // nothing in stack, return null // return (TObject*)NULL;}// method: pop//// arguments:// Stack& item_a: (output) the items pop'd// long num_items_a: (input) multi-pop mode// // return: a boolean value indicating status//// this method pops the top items off of the stack and places them in the// input stack passed as an argument//template<class TObject>long Stack<TObject>::pop(Stack<TObject>& items_a, long num_items_a) { // check that the modes are compatible // if (items_a.alloc_d != alloc_d) { return (Error::handle(name(), L"pop", Stack::ERR, __FILE__, __LINE__)); } // declare local variables // long count = 0; long stack_len = 0; TObject* ptr = NULL; // get the initial stack length // stack_len = stack_d.length(); // the pop method was called with POP_ALL // if (num_items_a == POP_ALL) { while ((count < stack_len) && stack_d.removeFirst(ptr)) { items_a.stack_d.insertLast(ptr); count++; } } // the pop method was called with POP_TO_MARK // else if (num_items_a == POP_TO_MARK) { while (!stack_d.isMarkedElement() && stack_d.removeFirst(ptr)) { items_a.stack_d.insertLast(ptr); count++; } } // the pop method was called with num_items_a // else if (num_items_a >= 0) { while ((count < num_items_a) && stack_d.removeFirst(ptr)) { items_a.stack_d.insertLast(ptr); count++; } } // none of the valid options to pop were found // else { return (Error::handle(name(), L"pop", Stack::ERR, __FILE__, __LINE__)); } // return the number of items popped // return count;}// end of include file//#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -