简体   繁体   中英

Algorithm to insert a particular value into a singly linked list based on row and column

I'm new to data structures . I have been working for the past 72 hours to find an algorithm to insert a particular value into a singly linked list based on row and column index. I created the singly linked list based on the SPARSE MATRIX below.

基于稀疏矩阵的单链表图像

I have attached the image of the linked list above. For an example, if i wanted to insert a value at row 0 and column 4 with the value of 8 . What is the most suitable algorithm to make this happen? Thanks in advance guys

An interesting point to consider.

First if you flatten your matrix, then you can notice that

  • the cell at (0,1) (at row 0 and column 1) becomes the cell of index 1.
  • the cell at (1,0) becomes the cell at index 5.
  • More generally, the cell (i,j) becomes the cell at index i * row_size + j

Using this observation you can go through the list until the computed index of the cell you want to insert is smaller than the computed index of the current element.

If you know how to insert a node at a specific position in a linked list (which i recommend you try first if you dont) it should be easy to make the bridge between the two.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM