• No results found

Multi-Linked Lists

N/A
N/A
Protected

Academic year: 2020

Share "Multi-Linked Lists"

Copied!
35
0
0

Loading.... (view fulltext now)

Full text

(1)

Multi-Linked Lists

(2)

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

(3)

Multi-lists

head

(4)

Multi-lists

head

(5)

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.

(6)

Linked Structures

• What else can we do with multiple links?

• Make them point at different data: create

Trees (and Graphs).

(7)

Trees

node

leaf node

degree

root children

parent

Level 1

Level 2

Level 3

height = depth = 3

(8)

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.

(9)

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

(10)

Sparse Matrix

Example of Multi-linked list

(11)

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

(12)

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

(13)

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

(14)

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

(15)

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)

(16)

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

(17)

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

(18)

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

(19)

Simple list linked representation

Node structure.

row col

next value

(20)

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

(21)

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

(22)

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]);

(23)

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;

} } } }

(24)

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();

}

(25)

Multi-list linked representation

(26)

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)

(27)

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

(28)

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)]

(29)

Multi-list Representation Both row and column lists.

Node structure.

row col

next down

value

(30)

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

(31)

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

(32)

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

(33)

// 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 ; } ;

(34)

/* structure for special headnode */

struct spmat {

struct rheadnode *firstrow ; struct cheadnode *firstcol ; int noofrows ;

int noofcols ; } ;

(35)

Creation of multi-linked list of sparse matrix

Step 1: Create a special node.

Step 2: Create all the column heads.

Step 3: Create all the row heads.

Step 4: Insert non-zero elements by

traversing till specific position(use row heads to reach till that row and use

column head to reach till column an then

update links).

References

Related documents

The last node points to NULL Declaring a Linked list In C language a linked list would be implemented using structure and pointers struct LinkedList int data.. Creating nodes

Specialization: Digital design(System design,RTL and Verification), Verilog Main business: Design, Research, Offshore projects. Description: ARF-Design works in two domains of

A doubly linear linked list contains two pointers and a data part where one of the pointer points to the preceding node and the other pointer will point to the subsequent node..

A doubly linked list is like a linked list, but each node also has a previous field, which points to the nodes predecessor..

A doubly linked list node contains, minimally a data field and two pointers, one to next node and other to previous node.. The following struct can be used to define a

In a Doubly Linked List, each node has a data item &amp; two pointers:..  A pointer to the

Including tokyo city as osaka guide looks great side trip to find the local and from.. Clearly demonstrates the time and transfer to set up

Finally, this theory of other hemisphere recruitment also sheds light on the frequent neuroimaging finding after TBI showing greater involvement of the right hemisphere