Circular creation.
The following is an example of creating a circular group of items.
In this example you would:
- create a new
Factory
- check
factory.head.next
==factory.head
- call
factory.add(factory.head)
- check
factory.head.next
is a newItem
- check
factory.head.next.next
==factory.head
Code:
pragma solidity ^0.4.22;
contract Item {
Item public nextItem;next;
address owner;
constructor(Item _next) public {
owner = msg.sender;
}
functionif setNext(Item n) public_next returns== (Item(0)) {
require(n != address(0) && msg.sendernext === owner);this;
nextItem} =else n;{
return n;
next }
}
= _next;
contract Chain {
Item[3] public items;}
}
constructor
function addNew(Item item) public {
items[0] =require(msg.sender new== Item(owner);
items[1]next = newitem;
}
}
contract Factory {
Item public head;
constructor(); public {
items[2]head = new Item(Item(0));
}
items[0].setNext(items[1])
function add(Item _to) public {
Item link = _to.setNextnext(items[2]);
Item next = new Item(link);
_to.setNextaddNew(items[0]next);
}
}