1

In any language, I would write following:

struct Node {
   void* data;
   Node* next;
}

...

head.next = new Node(data, null)

But it seems that solidity doesn't have a concept of pointers.

How could it be done here?

EDIT The answer at the end of Are there well-solved and simple storage patterns for Solidity? is not enough for me as I also need to insert nodes to the middle.

Lauri Peltonen
  • 29,391
  • 3
  • 20
  • 57
Alex Zhukovskiy
  • 491
  • 1
  • 5
  • 14
  • (Linked-lists are mentioned at the end of the accepted answer.) – Richard Horrocks Jun 13 '18 at 17:37
  • I have seen this example, but it doesn't seem to fit to me, because I need to insert in the middle while this code appends only. – Alex Zhukovskiy Jun 13 '18 at 17:42
  • 1
    My blog has an implementation of a doubly linked list: https://programtheblockchain.com/posts/2018/03/30/storage-patterns-doubly-linked-list/. – user19510 Jun 13 '18 at 17:42
  • @smarx thank you for the link. It's seems to be a proper approach. Brb when done. – Alex Zhukovskiy Jun 13 '18 at 18:07
  • @smarx I tried your approach and I get 1>Dynamic exception type: class boost::exception_detail::clone_impl<struct dev::solidity::UnimplementedFeatureError> 1>std::exception::what: Copying of type struct Season.StatusUpdate memory[] memory to storage not yet supported – Alex Zhukovskiy Jun 13 '18 at 18:31
  • You'd have to share your code. – user19510 Jun 13 '18 at 18:33
  • @smarx https://gist.github.com/Pzixel/995969e188df1b6c0054957e947f6073 – Alex Zhukovskiy Jun 13 '18 at 18:40
  • The error means what it says: you can't create that type in memory and then copy it into storage. I'd suggest opening a new question with a minimal repro so people can suggest alternatives. – user19510 Jun 13 '18 at 18:51
  • @smarx I currently workarounded it with nodes.length++ and then writing directly into new storage. It seems that it's a common way to do such a thing in Solidity. Thank you for your post, it was really priceless. – Alex Zhukovskiy Jun 13 '18 at 19:12
  • 1
    That's the alternative I would have suggested. I'm glad you got it working! – user19510 Jun 13 '18 at 19:13

1 Answers1

0

Here is a library on GitHub which does the trick for circular doubly linked lists. Although I haven't actually used the library, I referenced it heavily when writing my own similar implementation. Even if you don't use it, there's easily enough to work with to make a simple linked list.

AnAllergyToAnalogy
  • 3,553
  • 11
  • 23