📄 queue.h
字号:
//template <class TObject>MemoryManager Queue<TObject>::mgr_d(sizeof(Queue<TObject>), CLASS_NAME);// below are all the methods for the Queue 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& Queue<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:// 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 Queue<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); // print out 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 queue message // value.assign(L"front of queue"); Console::put(value); // increment the indention level // Console::increaseIndention(); // call the list debug method // queue_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>Queue<TObject>::Queue(ALLOCATION alloc_a) { // set the allocation flag // alloc_d = alloc_a; // the list object must be in USER mode // queue_d.setAllocationMode(USER);}//----------------------------------------------------------------------//// required assign methods////----------------------------------------------------------------------// method: assign//// arguments:// const Queue<TObject>& copy_queue: (input) queue to be copied//// return: a boolean value indicating status//// this method copies the elements in queue passed in to the current queue// template<class TObject>boolean Queue<TObject>::assign(const Queue<TObject>& arg_a) { // when the current queue is in USER mode and the queue to be copied // is in SYSTEM mode error // if ((alloc_d == USER) && (arg_a.getAllocationMode() == SYSTEM)) { return Error::handle(name(), L"assign", Error::ARG, __FILE__, __LINE__); } // if this queue 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 = arg_a.alloc_d; queue_d.setAllocationMode(alloc_d); queue_d.assign(arg_a.queue_d); queue_d.setAllocationMode(USER); // reposition the Queue // queue_d.gotoLast(); // exit gracefully // return true;}//---------------------------------------------------------------------------//// class-specific public methods:// queue add and remove methods////---------------------------------------------------------------------------// method: add//// arguments:// TObject* item: (input) the item added// // return: a boolean value indicating status//// this method adds a item to the front of the queue//template<class TObject>boolean Queue<TObject>::add(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; } // add the object to the top of the queue // return queue_d.insertLast(new_obj);}// method: add//// arguments:// Queue<TObject>& item_queue: (input) queue to be added// // return: a boolean value indicating status//// this method adds a queue to the front of another queue//template<class TObject>boolean Queue<TObject>::add(Queue<TObject>& item_queue_a) { // if the queue is empty exit // if (item_queue_a.isEmpty()) { return false; } // loop over the queue and add elements // else { for (boolean more = item_queue_a.queue_d.gotoFirst(); more; more = item_queue_a.queue_d.gotoNext()) { // add the element on the queue // if (!add(item_queue_a.queue_d.getCurr())) { return false; } } item_queue_a.queue_d.gotoLast(); } // exit gracefully // return true;}// method: add//// arguments:// TObject** items: (input) list of items to be added// long num_items: (input) number of items in the list// // return: a boolean value indicating status//// this method adds num_items from the items list to the front of the queue//template<class TObject>boolean Queue<TObject>::add(TObject** items_a, long num_items_a) { // loop over all elements in the queue and add each one // for (long i = 0; i < num_items_a; i++) { if (!add(items_a[i])) { return false; } } // exit gracefully // return true;}// method: peek//// arguments: none// // return: TObject* of the item on top of the queue//// this method lets the user manipulate the item on top of the queue without// removing the item from the queue//template<class TObject>const TObject* Queue<TObject>::peek() const { // if the queue is empty then return an error // if (isEmpty()) { Error::handle(name(), L"peek", Queue::ERR_EMPTY, __FILE__, __LINE__); return (TObject*)NULL; } // return the pointer // return queue_d.getFirst();}// method: peek//// arguments: none// // return: TObject* of the item on top of the queue//// this method lets the user manipulate the item on top of the queue without// removing the item from the queue//template<class TObject>TObject* Queue<TObject>::peek() { // if the queue is empty then return an error // if (isEmpty()) { Error::handle(name(), L"peek", Queue::ERR_EMPTY, __FILE__, __LINE__); return (TObject*)NULL; } // return the pointer // return queue_d.getFirst();}// method: remove//// arguments:// TObject* item: (input) memory space for the object// // return: a TObject* pointer to the object removed//// this method removes the front item in the queue and returns it to the user//template<class TObject>TObject* Queue<TObject>::remove(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"remove", 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"remove", ERR, __FILE__, __LINE__); return (TObject*)NULL; } // if the queue is empty exit // if (!isEmpty()) { // the returned item is valid so remove the item and make sure we are // positioned at the front of the queue // result = queue_d.removeFirst(ptr); queue_d.gotoLast(); // 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 queue, return null // return (TObject*)NULL;}// method: remove//// arguments:// Queue& item_a: (output) the items to be removed// long num_items_a: (input) the number of items// // return: a boolean value indicating status//// this method removes the first num_items off of the queue and places them// in the input queue passed as an argument.//template<class TObject>long Queue<TObject>::remove(Queue<TObject>& items_a, long num_items_a) { // check that the modes are compatible // if (items_a.getAllocationMode() != alloc_d) { return Error::handle(name(), L"remove", Queue::ERR, __FILE__, __LINE__); } // declare local variables // long count = 0; long queue_len = 0; TObject* ptr = NULL; // get the initial queue length // queue_len = queue_d.length(); // the remove method was called with REMOVE_ALL // if (num_items_a == REMOVE_ALL) { while ((count < queue_len) && queue_d.removeFirst(ptr)) { items_a.queue_d.insertLast(ptr); count++; } } // the remove method was called with REMOVE_TO_MARK // else if (num_items_a == REMOVE_TO_MARK) { // position the queue // queue_d.gotoFirst(); // remove element by element // while (!queue_d.isMarkedElement() && queue_d.removeFirst(ptr)) { items_a.queue_d.insertLast(ptr); count++; } } // the remove method was called with num_items_a // else if (num_items_a >= 0) { while ((count < num_items_a) && queue_d.removeFirst(ptr)) { items_a.queue_d.insertLast(ptr); count++; } } // none of the valid options to remove were found // else { return Error::handle(name(), L"remove", Queue::ERR, __FILE__, __LINE__); } // return the current pointer to the end of the list // queue_d.gotoLast(); // return the number of items removed // return count;}// end of include file//#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -