BCS304-Module 3 Notes (Sowmya)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

MODULE 3

Additional List Operations:

Search an Element on a Linked List

search an element on a linked list using a loop using the following steps. We are finding item on a linked
list.

• Make head as the current node.

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

searchNode(struct Node *head, int key)

struct node *temp= *head;

while (temp != NULL)

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.

void traversing (struct node *head)


{
struct node*head,*ptr;
ptr=head;
while(ptrnext!=null)
{
printf(“%d”,ptrdata);
ptr=ptrnext;
}
}
Concatenation of Linked List
Algorithm for concatenation

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.

struct node * concatenate (node *head1, node *head2)


{
node *p;
if (head1==NULL) //if the first linked list is empty
return (head2);
if (head2==NULL) //if second linked list is empty
return (head1);
p=head1; //place p on the first node of the first linked list

while (p->next!=NULL) //move p to the last node


p=p->next;
p->next=head2; //address of the first node of the
second linked list stored in the
last node of the first linked list

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.

Consider the sparse matrix, as shown in below figure (2).

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.

#define MAX-SIZE 50 /*size of largest matrix*/typedef enum {head, entry}


tagfield;
typedef struct matrixNode *matrixPointer;

typedef struct {

int row; int eol; int value;


} entryNode;

typedef struct {

matrixPointer down;
matrixPointer right;
tagfield tag;

union {
matrixPointer next;entryNode entry;
} u;
} matrixNode; matrixPointer hdnode[MAX- SIZE];

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy