BCS304-Module 3 Notes (Sowmya)
BCS304-Module 3 Notes (Sowmya)
BCS304-Module 3 Notes (Sowmya)
search an element on a linked list using a loop using the following steps. We are finding item on a linked
list.
• Run a loop until the current node is NULL because the last element points to NULL.
• In each iteration, check if the key of the node is equal to item. If it the key matches the item,
return true otherwise return false.
if (temp->data == key)
printf(“success”);
ptr = ptr->next;
return false;
Traversing of a singly linked list:Traverse each element node of the list display values of list.
Let us assume that the two linked lists are referenced by head1 and head2 respectively.
1. If the first linked list is empty then return head2.
2. If the second linked list is empty then return head1.
3. Store the address of the starting node of the first linked
list in a pointer variable, say p.
4. Move the p to
the last node of the linked list through simple linked list traversal
technique.
5. Store the address of the first node of the second linked
list in the next field of the node pointed by p. Return head1.
return(head1);
}
SPARSE MATRIX REPRESENTATION
In data representation, each column of a sparse matrix is represented as a circularly linked list with
a header node. A similar representation is used for each row of a sparse matrix.
Each node has a tag field, which is used to distinguish between header nodes and entry nodes.
Header Node:
Each header node has three fields: down, right, and next as shown in figure (a).
The down field is used to link into a column list and the right field to link into a row list.
The next field links the header nodes together.
The header node for row i is also the header node for column i, and the total number of
header nodes is max {number of rows, number of columns}.
Element node:
Each element node has five fields in addition in addition to the tag field: row, col, down, right,
value as shown in figure (b).
The down field is used to link to the next nonzero term in the same column and the right field
to link to the next nonzero term in the same row. Thus, if aij ≠ 0, there is a node with tag
field = entry, value = aij, row = i, and col = j as shown in figure (c).
We link this node into the circular linked lists for row i and column j. Hence, it is
simultaneously linked into two different lists.
Figure (3) shows the linked representation of this matrix. Although we have not shown thevalue of
the tag fields, we can easily determine these values from the node structure.
For each nonzero term of a, have one entry node that is in exactly one row list and one column list.
The header nodes are marked HO-H3. As the figure shows, we use the right field of the header node
list header to link into the list of header nodes.
typedef struct {
typedef struct {
matrixPointer down;
matrixPointer right;
tagfield tag;
union {
matrixPointer next;entryNode entry;
} u;
} matrixNode; matrixPointer hdnode[MAX- SIZE];