📄 doublelinkedlist.h
字号:
return true;}// method: writeData//// arguments:// Sof& sof: (input) sof file object// const String& pname: (input) parameter name//// return: a boolean value indicating status//// this method writes the object to the Sof file. it assumes that the// Sof file is already positioned correctly.//template<class TObject>boolean DoubleLinkedList<TObject>::writeData(Sof& sof_a, const String& pname_a) const { // we need an empty string for the sub-parameter // String empty_str; String start_str; start_str.assign(SofParser::DEF_BLOCKSTART_CHAR); // L"{" start_str.concat(SofParser::NEWLINE_CHAR); String end_str(SofParser::NEWLINE_CHAR); end_str.assign(SofParser::DEF_BLOCKSTOP_CHAR); // L"}" String delim_str(end_str); delim_str.concat(SofParser::DEF_DELIMITER_CHAR); delim_str.concat(SofParser::SPACE_CHAR); delim_str.concat(start_str); // L"}, {" String term; term.assign(SofParser::DEF_TERMINATOR_CHAR); term.concat(SofParser::NEWLINE_CHAR); // L";\n" // if text, write a parameter name // if (sof_a.isText()) { if (pname_a.length() > 0) { String output; output.assign(pname_a); output.concat(SofParser::SPACE_CHAR); output.concat(SofParser::DEF_ASSIGNMENT_CHAR); output.concat(SofParser::SPACE_CHAR); sof_a.puts(output); } if ((long)length_d > 0) { sof_a.puts(start_str); } } // for binary, write length // else { if (!length_d.writeData(sof_a)) { return Error::handle(name(), L"writeData", Error::IO,__FILE__,__LINE__); } } boolean is_first = true; // save the state of the list // NODE* tmp_node = first_d; // loop through the list // while (tmp_node != (NODE*)NULL) { if (sof_a.isText()) { if (!is_first) { sof_a.decreaseIndention(); sof_a.puts(delim_str); } sof_a.increaseIndention(); is_first = false; } // write this element // TObject* ptr = tmp_node->getItem(); if (ptr != (TObject*)NULL) { if (!ptr->writeData(sof_a, empty_str)) { return Error::handle(name(), L"writeData",Error::IO,__FILE__,__LINE__); } } else { return Error::handle(name(), L"writeData", Node<TObject>::ERR_NOITEM, __FILE__, __LINE__); } // move to the next node // tmp_node = tmp_node->getNext(); } if (sof_a.isText()) { // write the close brace // if ((long)length_d > 0) { sof_a.decreaseIndention(); sof_a.puts(end_str); } // possibly terminate the statement // if (pname_a.length() > 0) { sof_a.puts(term); } } // exit gracefully // return true;}// ----------------------------------------------------------------------//// required equality methods////------------------------------------------------------------------------// method: eq//// arguments:// const DoubleLinkedList<TObject>& compare_list: (input) the list to compare//// return: boolean value indicating test of equivalence//// this method compares two lists for equivalence. two lists are equivalent// if all corresponding nodes are equivalent//template<class TObject>boolean DoubleLinkedList<TObject>::eq(const DoubleLinkedList<TObject>& compare_list_a) const { // declare the output variable // boolean are_equal = true; // declare temporary variables to index into the lists // NODE* node_ptr_1 = first_d; NODE* node_ptr_2 = compare_list_a.first_d; // two lists can not be equivalent if they are of different lengths // if (length_d != compare_list_a.length_d) { // set the break flag // are_equal = false; } // loop over each node and see if each is equivalent // while ((are_equal) && (node_ptr_1 != (NODE*)NULL) && (node_ptr_2 != (NODE*)NULL)) { // see if the current nodes are equal // are_equal = node_ptr_1->eq(*node_ptr_2); if (are_equal) { // get the next nodes // node_ptr_1 = node_ptr_1->getNext(); node_ptr_2 = node_ptr_2->getNext(); } } // return a value representing if they are equivalent // return are_equal;}//-------------------------------------------------------------------------//// required memory management methods////-------------------------------------------------------------------------// method: clear//// arguments:// Integral::CMODE cmode_a: (input) clear mode// // return: a boolean value indicating status//// this method clears the contents of the list by the setting of cmode_a//// enum CMODE { RETAIN = 0, RESET, RELEASE, FREE, DEF_CMODE = RESET };//// RETAIN: call clear with RETAIN on each element in the list//// RESET: delete items from the front of the list until the list is empty//// RELEASE: delete items from the front of the list until the list is empty//// FREE: remove all nodes from the list and clear the corresponding nodes//// Programming hint://// use the clear() method to manage the memory of the objects going// into the list.// particular useful when list is in USER mode. Caution the object// you place into list must have a clear method that meets the// requirements of the IFC clear method.////template<class TObject>boolean DoubleLinkedList<TObject>::clear(Integral::CMODE cmode_a) { // temporary variables // TObject* tmp_item; // if cmode_a is called with RETAIN then call clear with RETAIN on // each element in the list // if (cmode_a == Integral::RETAIN) { for (boolean more = gotoFirst(); more; more = gotoNext()) { curr_d->getItem()->clear(Integral::RETAIN); } } // if cmode_a is called with RESET or RELEASE remove all nodes form // the list // if ((cmode_a == Integral::RESET) || (cmode_a == Integral::RELEASE)) { // delete items from the front of the list until the list is empty // while (removeFirst()); } // if cmode_a is called with FREE remove all nodes form the list and // clear the corresponding nodes // if (cmode_a == Integral::FREE) { if (alloc_d == SYSTEM) { for (gotoFirst(); !isEmpty(); remove()) { curr_d->clear(cmode_a); } } else { while (removeFirst(tmp_item)) { tmp_item->clear(cmode_a); delete tmp_item; } } } // exit gracefully // return true;}//---------------------------------------------------------------------------//// class-specific public methods:// overload operators and extensions to required methods////---------------------------------------------------------------------------// method: operator ()//// arguments:// long index: (input) the index with respect to the first_d//// return: TObject& of the given index//// this method gets the item of the given index in the list//template<class TObject>TObject& DoubleLinkedList<TObject>::operator() (long index_a) { // find the node // NODE* pp = getNode(index_a); if (pp == (NODE*)NULL) { Error::handle(name(), L"operator ()", ERR, __FILE__, __LINE__); static TObject tmp; return tmp; } // return the item inside the node // return *(pp->getItem()); }// method: operator ()//// arguments:// long index: (input) the index with respect to the first_d//// return: TObject& of the given index//// this method gets the item of the given index in the list//template<class TObject>const TObject& DoubleLinkedList<TObject>::operator() (long index_a) const { // find the node // NODE* pp = getNode(index_a); if (pp == (NODE*)NULL) { Error::handle(name(), L"operator ()", ERR, __FILE__, __LINE__); static TObject tmp; return tmp; } // return the item inside the node // return *(pp->getItem());}//---------------------------------------------------------------------------//// class-specific public methods:// positioning methods////---------------------------------------------------------------------------// method: gotoNext//// arguments: none//// return: a boolean value indicating status//// this method makes the current node pointer jump to the next node//template<class TObject>boolean DoubleLinkedList<TObject>::gotoNext() { // determine if the current and next nodes are valid // if ((curr_d == (NODE*)NULL) || (curr_d->getNext() == (NODE*)NULL)) { return false; } // make the current node jump to the next node // curr_d = curr_d->getNext(); // exit gracefully // return true;}// method: gotoPrev//// arguments: none//// return: a boolean value indicating status//// this method makes the current node pointer jump to the previous node//template<class TObject>boolean DoubleLinkedList<TObject>::gotoPrev() { // determine if the current node is valid // if ((curr_d == (NODE*)NULL) || (curr_d->getPrev() == (NODE*)NULL)) { return false; } // make the current node jump to the previous node // curr_d = curr_d->getPrev(); // exit gracefully // return true;}// method: gotoMark//// arguments: none//// return: a boolean value indicating status//// this method makes the current node pointer jump to the marked node//template<class TObject>boolean DoubleLinkedList<TObject>::gotoMark() { // make sure that the marked location is valid // if (mark_d == (NODE*)NULL) { return false; } // set the node back to mark // curr_d = mark_d; // exit gracefully // return true;}//---------------------------------------------------------------------------//// class-specific public methods:// marking methods////--------------------------------------------------------------------------- // method: setMark//// arguments: none// // return: a boolean value indicating status//// this method sets the current node to be the marked node//template<class TObject>boolean DoubleLinkedList<TObject>::setMark() { // check if the current node is NULL // if (curr_d == (NODE*)NULL) { return false; } // mark the current node // mark_d = curr_d; // exit gracefully // return true;}//---------------------------------------------------------------------------//// class-specific public methods:// insert and remove methods////---------------------------------------------------------------------------// method: insert//// arguments:// TObject* item: (input) the item to be added// // return: a boolean value indicating status//// this method adds a new node containing the item after the current position// of this list//template<class TObject>boolean DoubleLinkedList<TObject>::insert(TObject* item_a) { // make sure the item is not NULL // if (item_a == (TObject*)NULL) { return Error::handle(name(), L"insert", Error::NULL_ARG, __FILE__, __LINE__); } // the list was empty, add the input item to the beginning // if (isEmpty()) { return insertFirst(item_a); } // if curr_d is the last node in the old list then add the new item // at the end of the list // else if (curr_d == last_d) { return insertLast(item_a); } // if the list is not empty then add it as the next item in the list // // create a new node and put or copy the item into it // NODE* new_node; // SYSTEM mode will store a copy, USER mode stores just the reference // if (alloc_d == SYSTEM) { new_node = new NODE(new TObject(*item_a)); } else { new_node = new NODE(item_a); } // set the links for the new node // new_node->setNext(curr_d->getNext()); new_node->setPrev(curr_d); // set the links for the surrounding nodes // curr_d->setNext(new_node); if (new_node->getNext() != (NODE*)NULL) { new_node->getNext()->setPrev(new_node); } // move the current node pointer // curr_d = new_node; // increment the size of the list // length_d += 1; // exit gracefully // return true;}// method: insert//// arguments:// DoubleLinkedList<TObject>& ilist: (input) the list to be added into// this list// // return: a boolean value indicating status//// this method adds a list into the current position of this list//template<class TObject>boolean DoubleLinkedList<TObject>::insert(DoubleLinkedList<TObject>& ilist_a) { // if the input list is empty then there is nothing to do // if (ilist_a.isEmpty()) { // exit gracefully // return true; } // store the state of the input list // NODE* tmp_node = ilist_a.first_d; // loop over each element in the input list and add each element to the list // while (tmp_node != (NODE*)NULL) { // add the next element to the list // insert(tmp_node->getItem()); // move to the next node // tmp_node = tmp_node->getNext(); } // exit gracefully // return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -