Multi-Linked Lists
Multi-Linked Lists
• A multilinked list is a more general linked list with multiple links from nodes.
• In a general multi-linked list each node can have any number of pointers to other nodes, and there may or may not be inverses for each pointer.
• Multi-lists are essentially the technique of
embedding multiple lists into a single data structure.
• A multi-list has more than one next pointer, like a
doubly linked list, but the pointers create separate
lists
Multi-lists
head
Multi-lists
head
Linked Structures
• A doubly-linked list or multi-list is a data structure with multiple pointers in each node.
• In a doubly-linked list the two pointers create bi-directional links
• In a multi-list the pointers used to make
multiple link routes through the data.
Linked Structures
• What else can we do with multiple links?
• Make them point at different data: create
Trees (and Graphs).
Trees
node
leaf node
degree
root children
parent
Level 1
Level 2
Level 3
height = depth = 3
Example 2: List of List
• Suppose you want to make list of all customers in particular bank.
• Bank will have multiple branches.
• Each branch will have multiple customers.
Example 3: Sparse Matrices
• A second very common use of multi-linked lists is sparse matrices.
• A sparse matrix is a matrix of numbers, as in mathematics, in which almost all the
entries are zero
Sparse Matrix
Example of Multi-linked list
Matrix
• A matrix with 5 rows and 3 columns can be represented by
47 27
48
9 8
12
11 64
109
2 82
6
4 3
27
n = 3
m = 5
• We say this is a 5×3 matrix.
• A 5×3 matrix has 15 entries.
entry
Matrix
• Suppose a m×n matrix A. If m = n, we call the matrix square.
• We use two-tuple index to locate a matrix entry.
– Example:
• A(3, 2): entry locating at row 3, column 2.
2 0
2 1
3 1
3 2
9 22
0 0
17 5
3
4 • The matrix is square. It has
4 rows and 4 columns.
•A(3,2) = 0
Col 0 Col 1 Col 2 Col 3 Row 0
Row 1
Row 2 Row 3
Sparse Matrix
• A sparse matrix is a matrix that has many zero entries.
0 0
0 28
0 0
0 0
0 0
0 91
0 0
0 0
0 0
0 0
6 0
0 0
0 0
0 3
11 0
15 0
22 0
0
15
1 4
Sparse Matrix
• A matrix is sparse if many of its elements are zero
• A matrix that is not sparse is dense
• The boundary is not precisely defined
– Diagonal and tridiagonal matrices are sparse
• Two possible representations
– array
– linked list
Array Representation of Sparse Matrix
Single linear list in row-major order.
scan the nonzero elements of the sparse matrix in row-major order
each nonzero element is represented by a triple (row, column, value)
the list of triples may be an array list or a linked list
(chain)
Array Representation of Sparse Matrix
0 0 3 0 4 0 0 5 7 0
0 0 0 0 0 0 2 6 0 0
list =
row 1 1 2 2 4 4
column 3 5 3 4 2 3
value 3 4 5 7 2 6
Array Representation of Sparse Matrix
row 1 1 2 2 4 4 list = column 3 5 3 4 2 3 value 3 4 5 7 2 6
element 0 1 2 3 4 5
row 1 1 2 2 4 4
column 3 5 3 4 2 3
value 3 4 5 7 2 6
1 8
Linked Representation of Sparse Matrix
• A shortcoming of the 1-D array of a sparse matrix is that we need to know the number of nonzero
terms in each of the sparse matrices when the array is created
•A linked representation can overcome this shortcoming.
•Two types of linked representation
• Simple list linked representation/Chain representation
• Multi list linked representation
Simple list linked representation
Node structure.
row col
next value
Simple list linked representation
row 1 1 2 2 4 4 list = column 3 5 3 4 2 3 value 3 4 5 7 2 6
1 3 3
1 5 4
2 5
2 7
4 2
4 6
3 4 3
null
firstNode
2
Algorithm to represent sparse matrix using SimpleLinked List
• Define structure of node.
• Input the no of rows(m) and no of columns(n).
• Set for loop i< row as i=1 and another loop i < col as j=1
– Read value of element at position (i,j) – Next j and i
• If we choose non-zero element, then insert element by creating new node in the linked list.
• If we choose display than for i=1 to r then check p=p->next
• stop
PROGRAM CODE
#include<stdio.h>
struct node {
int row;
int col;
int data;
struct node *link;
}*head=NULL,*curr=NULL,*p=NULL;
void main() {
int i,j,m,n,a[50][50];
clrscr();
printf("\nSparse Matrix using Linked List\n");
printf("\nEnter the number of rows of the matrix:");
scanf("%d",&m);
printf("Enter the number of columns of the matrix:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<m;i++) {
for(j=0;j<n;j++) {
scanf("%d",&a[i][j]);
if(a[i][j]!=0) {
curr=(struct node*)
malloc(sizeof(struct node));
curr->row=i;
curr->col=j;
curr->data=a[i][j];
curr->link=NULL;
if(head==NULL) head=curr;
else {
p=head;
while(p->link!=NULL) p=p->link; p->link=curr;
} } } }
printf("\nSparse matrix using linked list:\nRow\tColumn\tElement\t\n");
p=head;
if(head==NULL)
printf("\nSparse matrix empty!\n");
else{
while(p->link!=NULL)
{ printf("%d\t%d\t%d\n",p->row,p->col,p->data);
p=p->link;
}printf("%d\t%d\t%d\n",p->row,p->col,p->data);
}getch();
}
Multi-list linked representation
Sparse Matrix:
The description of this representation is as
• It contains one header node that has four fields….
--#of rows --#of cols
--#of non zero elements
--head i.e. pointer to 1st row containing at least one non zero element.
• A linked list of rows containing at least one non zero term, in the ascending order of their row values. each node of this list has three fields
--row (row number for corresponding row list) --next (pointer to next node in the row list)
--first (a pointer to first column in a row having non zero item)
• A linked list of columns containing nonzero terms, in the ascending order of their column values. Each node of this list has three fields --col (column number for corresponding row list)
--term ( a non zero value in column col)
--link (a pointer to next column having non zero element)
Sparse Matrix
5 6 5
1 4 2 X
2 3 1
3 2 4 X
5 X 5 7 X
6 5 X
A linked representation of sparse matrix
0 0 0 2 0 0
0 0 1 0 0 5
0 4 0 0 0 0
0 0 0 0 0 0
0 0 0 0 7 0
One Linear List Per Row
0 0 3 0 4
0 0 5 7 0 0 0 0 0 0
0 2 6 0 0
row1 = [(3, 3), (5,4)]
row2 = [(3,5), (4,7)]
row3 = []
row4 = [(2,2), (3,6)]
Multi-list Representation Both row and column lists.
Node structure.
row col
next down
value
Row Lists
0 0 3 0 4
0 0 5 7 0 0 0 0 0 0 0 2 6 0 0
null
1 3 3 1 5 4
2 3 5 2 4 7
4 2 2 4 3 6
n
n
n
Column Lists
0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0
1 3 3 1 5 4
2 3 5 2 4 7
4 2 2 4 3 6
n
n n
Muti linked Lists
0 0 3 0 4
0 0 5 7 0 0 0 0 0 0 0 2 6 0 0
null
row[]
1 3 3 1 5 4
2 3 5 2 4 7
4 2 2 4 3 6
n n
n
n n n
// structure for col headnode struct cheadnode
{
int colno ;
struct node *down ; struct cheadnode *next;
} ;
// structure for row headnode struct rheadnode
{
int rowno ;
struct node * right ; struct rheadnode *next ; } ;
// structure for node to store element struct node
{
int row ; int col ; int val ;
struct node *right ; struct node *down ; } ;
/* structure for special headnode */
struct spmat {
struct rheadnode *firstrow ; struct cheadnode *firstcol ; int noofrows ;
int noofcols ; } ;