【正文】
return p。 p = CursorSpace[0].next。 }Node,CursorSpace[Spacesize]。 } Doubly Linked Lists:Changing Data Structure Software College Northeastern University DLL pared to SLL ?Advantages: ?Can be traversed in either direction (may be essential for some programs) ?Some operations, such as deletion and inserting before a node, bee easier ?Disadvantages: ?Requires more space ?List manipulations are slower (because more links must be changed) ?Greater chance of having bugs (because more links must be manipulated) Data Structure Software College Northeastern University Problem: ?Inserting or Deleting use new and delete to obtain and release a piece of space ?dynamic management ?If operations of getting or returning are frequently, it may indicate more time required Two resolving way: ?Cursor Implementation of linked lists using static memory ?Initially We allocate many nodes to linked into spare lists Linked Lists: Cursor Implementation Data Structure Software College Northeastern University Tow important features of linked list: ?The data are stored in a collection of structure, each structure contains data and a pointer to the next structure ?A new structure can be obtained from the system?s global memory by a call to new and released by a call to delete Implementation: ?Have a global array of structure ?Array index can be used in place of an address Linked Lists: Cursor Implementation Data Structure Software College Northeastern University Global array of structures Address: Array index 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 0 define SpaceSize 1000 typedef struct Node{ ElementType data。 } current = current→ lLink。 } Doubly Linked Lists:Changing Data Structure Software College Northeastern University template class Type int DblListType::Prior ( ) { if ( current→ lLink == first ) { current = NULL。 } current = current→ rLink。 } template class Type int DblListType::Next ( ) { if ( current→ rLink == first ) { current = NULL。 } current = NULL。 } Doubly Linked Lists:counting Data Structure Software College Northeastern University template class Type int DblListType::First ( ) { if ( !IsEmpty ( ) ) //跳過(guò)表頭結(jié)點(diǎn)的第一個(gè) { current = first→ rLink。 count++。 int count = 0。 else current = current→ rLink。 delete temp。 //下一結(jié)點(diǎn) current→ lLink = temp→ lLink。 1 2 Doubly Linked Lists:Deleting(1) Data Structure Software College Northeastern University template class Type void DblListType::Remove ( ) { if ( current != NULL ) { DblNode *temp = current。 } Doubly Linked Lists:Inserting(2) Data Structure Software College Northeastern University 1. current→ rLink→ lLink = current→ lLink。 current = current→ rLink。 value ) { if ( current == NULL ) //空表情形 current = first→ rLink = new DblNode ( value, first, first )。 5. current→ rLink→ lLink = current。 3. current→ rLink = p。 } Doubly Linked Lists:searching Another method? Data Structure Software College Northeastern University 1. p→ lLink = current。 return 1。 p→ data != target ) p = p→ rLink。 while ( p != first amp。 target ) { //在雙向循環(huán)鏈表中搜索含 target的結(jié)點(diǎn), //搜索成功返回 1,否則返回 0。 current = NULL。 Implementation of Doubly linked lists(3) Data Structure Software College Northeastern University Doubly linked lists:Searching Data Structure Software College Northeastern University Doubly Linked Lists:constructing template class Type DblListType::DblLIst ( Type uniqueVal ) { //雙向循環(huán)鏈表的構(gòu)造函數(shù) , 創(chuàng)建表頭結(jié)點(diǎn) first = new DblNodeType ( uniqueVal )。 private: DblNodeType *first, *current。 value )。 int operator ! ( ) { return current != NULL。 int Next ( )。 Implementation of Doubly linked lists(2) Data Structure Software College Northeastern University void Firster ( ) { current = first。 target )。 int IsEmpty ( ) { return first→ rlink == first。 ~DblList ( )。 //指針 DblNode ( Type value, //構(gòu)造函數(shù) DblNodeType *left, DblNodeType *right ) : data (value), lLink (left), rLink (right) { } Implementation of Doubly linked lists(1) Data Structure Software College Northeastern University DblNode ( Type value ) : data (value), lLink (NULL), rLink (NULL) { } }。 private: Type data。 // p 在搜索成功時(shí)返回找到的結(jié)點(diǎn)地址 // p 在搜索不成功時(shí)返回空值 } Linked Lists: Searching in a CLL Data Structure Software College Northeastern University Variations of Linked Lists ?Doubly linked lists ?Each node points to not only successor but the predecessor ?There are two NULL: at the first and last nodes in the list ?Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists backwards Head A ? B ? Data Structure Software College Northeastern University p == p→ lLink→ rLink == p→ rLink→ lLink Non_empty list empty list Doubly linked lists Data Structure Software College Northeastern University template class Type class DblList。 p→ data != value ) p = p→ link。 //檢測(cè)指針 p 指示第一個(gè)結(jié)點(diǎn) while ( p != first amp。 value ) { last =first = new ListNodeType( value )。 firstlink = first。 插入 Implementation of Circular linked lists(3) Data Structure Software College Northeastern University Variations of Circular Linked Lists ?Circular linked lists ?construct an empty Circular linked list CircList ( const Type amp。 private: CircListNodeType *first, *current, *last。 value)。 Implementation of Circular linked lists(2) Data St