490 likes | 607 Views
Linked Lists. Chapter 4. Why “linked lists”. Insert “FAT” ? Or delete something. Usual way to draw a linked list. Inserting into a linked list. Delete GAT. Defining a node in C++. class ThreeLetterNode { private: char data[3]; ThreeLetterNode * link; }.
E N D
Linked Lists Chapter 4
Why “linked lists” Insert “FAT” ? Or delete something
Defining a node in C++ • class ThreeLetterNode { private: char data[3]; ThreeLetterNode* link; }
Designing a chain class in C++: Design attempt 1 • Use a global variable first which is a pointer of ThreeLetterNode. • Unable to access to private data members: data and link. • ThreeLetterNode *first; firstdata, firstlink firstdata[0], firstdata[1], firstdata[2]
Designing a chain class in C++: Design attempt 2 • Make data members public or define public member functions GetLink(), SetLink() and GetData() • Defeat the purpose of data encapsulation • The ideal solution would only permit those functions that perform list manipulation operations (like inserting a node into or deleting a node from a chain) access to the data members of ThreeLetterNode
Designing a chain class in C++: Design attempt 3 • Use of two classes. • Create a class that represents the linked list. • The class contains the items of another objects of another class. • HAS-A • A data object of type A HAS-A data object of type B if A conceptually contains B or B is a part of A. • Eg. Computer HAS-A Processor, Book HAS-A Page
Conceptual relationship between ThreeLetterChain and ThreeLetterNode Actual relationship between ThreeLetterChain and ThreeLetterNode
Pointer manipulation in C++ • Two pointer variables of the same type can be compared. • Eg. x == y, x != y, x == 0 x = y *x = *y
Program 4.5: Deleting a node Example: Delete the second node Delete the first node Reference: J.L. Huang@NCTU
Iterator • An iterator is an object that is used to access the elements of a container class one by one
Program 4.13: Reversing a list template <class T> void Chain<T>::Reverse() {// A chain is reversed so that (a1, …, an)becomes(an, …, a1) ChainNode<T> *current = first, *previous = 0; // previoustrails current while (current) { ChainNode<T> *r = previous; previous = current; // rtrailsprevious current = current link; // currentmoves to next node previouslink = r; // link previousto preceding node } first = previous; }
Circular lists How to insert at the front of a circular list? Read Program 4.14
Linked stacks and queues Read Program 4.19, 4.20, 4.21 and 4.22
Program 4.23: Polynomial class definition structTerm { intcoef; intexp; Term Set(intc,inte) {coef = c;exp = e;return *this;}; }; classPolynomial { public: // public functions defined here private: Chain<Term> poly; };
Polynomial representation (a) 3x14+2x8+1 (b) 8x14-3x10+10x6
Equivalence classes • For an arbitrary relation by the symbol • Reflexive • xx • Symmetric • If xy, then yx • Transitive • If xyand yz, then xz • A relation over a set, S, is said to be an equivalence relation over S if and only if it is symmetric, reflexive, and transitive over S.
Example • 04, 31, 610, 89, 74, 68, 35, 211, 110 • Three equivalent classes: {0,2,4,7,11}; {1,3,5}; {6,8,9,10}
Program 4.26: First version of equivalence algorithm void Equivalence() { initialize; while more pairs { input the next pair (i,j); process this pair; } initialize for output; for(each object not yet output) output the equivalence class that contains his object; }
Program 4.27: A more detail version of equivalence algorithm voidEquivalence() { readn; initializefirst[0:n-1] to0and out[0:n-1] tofalse; whilemore pairs { read the next pair(i, j); put jon the chainfirst[i]; put i on the chainfirst[j]; } for (i = 0;i < n;i++) if (!out[i]) { out[i] = true; output the equivalence class that contains objecti; } }
Lists after pairs have been input 04, 31, 610, 89, 74, 68, 35, 211, 110
Program 4.28: C++ function to find equivalence classes classENode{ friendvoidEquivalence( ); public: ENode(intd = 0) {data = d; link = 0;} private: intdata; ENode *link; };
voidEquivalence( ) {// input and output ifstreaminFile( "equiv.in", ios::in); if (!inFile) throw “Cannot open input file.”; inti, j, n; inFile >> n; // read number of objects // initializefirstandout ENode **first = newENode* [n]; bool *out = newbool[n]; // use STL functionfillto initialize fill (first, first + n, 0); fill (out, out + n, false);
// Phase 1: input equivalence pairs inFile >>i >> j; while (inFile.good()) { //check end of file first[i] = newENode(j, first[i]) first[j] = newENode(i, first[j]) inFile >>i >> j; }
// Phase 2: output equivalence classes for (i= 0;i < n;i++) if (!out[i]) { // needs to be output cout << endl << "A new class: " << i; out[i] = true; ENode *x = first[i];ENode *top = 0; // initialize stack while (1) {//find rest of class while (x) { // process the list j = xdata; if (!out[j]) { cout << ", " << j; out[j] = true; ENode *y = xlink; xlink = top; top = x; x = y; } elsex = xlink; } if (!top) break; x = first [topdata]; top = toplink; // unstack } // end of while(1)
} for (i = 0; i < n;i++) while (first[i]) { ENode *delnode = first[i]; first[i] = delnodelink; deletedelnode; } delete [] first;delete [] out; }
Sparse matrices 1 0 4 Next nonzero column Next nonzero row
Example Thus for an nxmsparse matrix with r nonzero terms, the number of nodes needed is max{n, m} + r + 1.
Program 4.29: Class definition for sparse matrices structTriple{introw, col, value;}; classMatrix; classMatrixNode{ friendclassMatrix; friendistream&operator>>(istream&, Matrix&); private: MatrixNode *down , *right; boolhead; union { MatrixNode *next; Tripletriple; }; MatrixNode(bool, Triple*); // constructor }
MatrixNode::MatrixNode(boolb, Triple *t) // constructor { head = b; if (b) {right = down = this;} // row/column header node elsetriple = *t; // element node or header node for list of header nodes } classMatrix{ friendistream&operator>>(istream&, Matrix&); public: ~Matrix(); // destructor private: MatrixNode *headnode; };
Doubly linked lists • Move in forward and backward direction. • How to get the preceding node during deletion or insertion? • Using 2 pointers • Node in doubly linked list • left link field (left link) • data field (item) • right link field (right link)
Doubly linked circular list with header node Empty doubly linked circular list with header node
Program 4.32: Class definition of a doubly linked list class DblList; class DblListNode { friend class DblList; private: intdata; DblListNode*left,*right; }; class DblList { public: // List manipulation operations . . private: DblListNode*first; // points to header node };
Program 4.33: Deletion from a doubly linked circular list void DblList :: Delete(DblListNode *x) { if (x = = first) throw "Deletion of header node not permited"; else { x→left→right = x→right; //(1) x→right→left = x→left; //(2) delete x; } Reference: J.L. Huang@NCTU
Program 4.34: Insertion into a doubly linked circular list void DblList:: Insert(DblListNode *p, DblListNode *x) { // insert node pto the right of nodex p→left = x; //(1) p→right = x→right; //(2) x→right→left = p; //(3) x→right= p; //(4) } Reference: J.L. Huang@NCTU