1 / 18

Chapter 4 (cont.)

Chapter 4 (cont.). Additional Lists Operations. 4.4 Circular Lists. The link field of the last node points to the first node in the list. first. BAT. CAT. FAT. WAT. Figure 4.13: A circular list. Insertion. Insertion in front must change the link field of last node

betty
Download Presentation

Chapter 4 (cont.)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4 (cont.) Additional Lists Operations

  2. 4.4 Circular Lists • The link field of the last node points to the first node in the list first . . . BAT CAT FAT WAT Figure 4.13: A circular list

  3. Insertion • Insertion in front • must change the link field of last node  must move down the entire length to last node  it is more convenient if list pointer points to last node rather than the first x1 x2 x3 last data link • Figure 4.15 : Pointing to the last node of a circular list

  4. Now, it can be done in a fixed amount of time template <class Type> void CircList ::InsertFront (ListNode <Type> *x) // Insert the node pointed at by x at the “front” of the circular // list this,where last points to the last node in the list { if (!last) { // empty list last = x; x → link = x; else { x → link = last → link; last → link = x; } } Program 4.14: Inserting at the front Insertion at the end • can be done by addinglast = xto theelseclause

  5. A Circular list with a dummy head first (a) first BAT CAT EAT ∙∙∙ WAT (b) Figure 4.16: A circular list with a head node

  6. 4.8 Sparse Matix • Representation • Each column or row is a circular list with a head node • Each node has a tag field (to distinguish between head and entry nodes) • Each node has three links (1) downlink points to a column list (2) rightlink points to a row list (3) nextlink points to the next head node • Tot # of head nodes = max{# of rows, # of columns} • Each entry node has six fields:tag, row, col, down, right, value • downandrightlink to next nonzero term in the same column and the same row.

  7. the list of head nodes (linked by next field) has a head node • tag = head • row, col = # of rows and cols (matrix dimension) • # of nodes needed to represent a (num_rows  num_cols) matrix with (num_terms) nonzero entries is max{num_row, num_cols} + num_terms + 1 nodes j down head right f i down head row col right aij next value (b) typical node (c) set up for aij (a) head node Figure 4.25 : Node structure for sparse matrices

  8. headnode H0 H1 H2 H3 H4 H5 H6 6 7 7 0 2 0 5 H0 11 13 1 0 1 6 H1 12 14 2 1 2 5 H2 -4 -8 H3 H4 5 1 H5 -9 points to H6 Figure 4.17: Linked representation of the sparse matrix A of Fugure 4.26

  9. enum Boolean {FALSE, TRUE}; struct Triple {int value, row, col ; }; class Matrix; // forward declaration class MatrixNode { friend class Matrix; friend istream& operator >> (istream&, Matrix&) ; // for reading in a matrix private : MatrixNode *down, *right; Boolean head; union { // anonymous union MatrixNode *next; Triple triple; }; MatrixNode(Boolean, Triple*); // constructor };

  10. MatrixNode::MatrixNode(Boolean b, Triple *t) // constructor { head = b; if (b) {right = next = down = this;} // row/column head node else triple = *t; // head node for list of headnodes OR element node } typedef MatrixNode * MatrixNodePtr; // to allow subsequent creation of array of pointers class Matrix { friend istream& operator >>(istream&, Matrix&); public: ~Matrix(); //destructor private: MatrixNode *headnode; } Program 4.30: Class definition for sparse matrices

  11. istream& operator>>(istream& is, Matrix& matrix) // Read in a matrix and set up its linked representation. // An auxiliary array head is used. { Triple s; int p; is >> s.row >> s.col >> s.value; // matrix dimensions if (s.row > s.col ) p = s.row; else p = s.col; // set up headnode for list of head nodes. matrix.headnode = new MatrixNode(FALSE, &s); if (p == 0) { matrix.headnode → right = matrix.headnode; return is; } // at least one row or column MatrixNodePtr *head = new MatrixNodePtr[p]; // initialize head nodes for (int i=0 ; i<p ; i++) head[i] = new MatrixNode(TRUE, 0); int CurrentRow = 0; MatrixNode *last = head[0]; // last node in current row

  12. for (i=0 ; i<s.value ; i++ ) // input triples { Triple t; is >> t.row >> t.col >> t.value; if (t.row > CurrentRow) { // close current row last → right = head[CurrentRow]; CurrentRow = t.row; last = head[CurrentRow]; } // end of if last = last → right = new MatrixNode(FALSE, &t); // link new node into row list head[t.col] → next = head[t.col] → next → down = last; // link into column list } // end of for

  13. last → right = head[CurrentRow]; // close last row for (i=0 ; i<s.col ; i++) head[i] → next → down = head[i]; // close all column lists // link the head nodes together for (i=0 ; i<p-1 ; i++) head[i] → next = head[i+1]; head[p-1] → next = matrix.headnode; matrix.headnode → right = head[0]; delete [] head; Rreturn is; } Program 4.31: Reading in a sparse matrix

  14. 4.9 Doubly Linked List • We can move in either direction along the nodes. • two links (for left and right) If ptr points to any node, then ptr = ptr->llink->rlink = ptr->rlink->llink • Better to have a head node  an empty list has the head node as its only node.

  15. leftlink data rightlink Head Node Figure 4.29 : Doubly linked circular list with head node first Figure 4.30 : Empty doubly linked circular list with head node

  16. class DblList; class DblListNode { friend class DblList; private: int data; DblListNode *llink, *rlink; }; class DblList { public: // List manipulation operations private: DblListNode *first; // points to head node }; Program 4.33: Class definition of a doubly linked list

  17. Insertion (Circular-doubly linked list) • xpoints to the node after which insertion will take place • ppoints to the node being inserted void DblList::Insert(DblListNode *p, DblListNode *x) // insert node p to the right of node x { p → llink = x; p → rlink = x → rlink; x → rlink → llink = p; x → rlink = p; } Program 4.35: Insertion into a doubly linked circlular list

  18. Deletion • firstpoints to the head node • xpoints to the node to be deleted void DblList::Delete(DblListNode *x) { if (x == first) cerr << ”Deletion of head node not permitted” << endl; else { x → llink → rlink = x → rlink; x → rlink → llink = x → llink; delete x; } } Program 4.34: Delete from a doubly linked circular list

More Related