Presentation is loading. Please wait.

Presentation is loading. Please wait.

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... BATCATFATWAT.

Similar presentations


Presentation on theme: "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... BATCATFATWAT."— Presentation transcript:

1 Chapter 4 (cont.) Additional Lists Operations

2 2 4.4 Circular Lists The link field of the last node points to the first node in the list... BATCATFATWAT first Figure 4.13: A circular list

3 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 x1x1 x2x2 x3x3 Figure 4.15 : Pointing to the last node of a circular list last datalink

4 4 –Now, it can be done in a fixed amount of time template void CircList ::InsertFront (ListNode *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 adding last = x to the else clause

5 5 A Circular list with a dummy head first BATCATEATWAT (b) (a) ∙∙∙ Figure 4.16: A circular list with a head node

6 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) down link points to a column list (2) right link points to a row list (3) next link 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 –down and right link to next nonzero term in the same column and the same row.

7 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 downheadright downheadrowcolright f i j nextvalue a ij (a) head node (b) typical node (c) set up for a ij Figure 4.25 : Node structure for sparse matrices

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

9 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 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 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 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 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 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 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 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 17 Insertion (Circular-doubly linked list) –x points to the node after which insertion will take place –p points 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 18 Deletion –first points to the head node –x points 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


Download ppt "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... BATCATFATWAT."

Similar presentations


Ads by Google