Created
January 17, 2019 16:01
-
-
Save crazyrabbitLTC/14f3baf0099a5681e049819e1013b8f4 to your computer and use it in GitHub Desktop.
LinkedList upgraded to Solidity v5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pragma solidity ^0.5.0; | |
| import "zos-lib/contracts/Initializable.sol"; | |
| contract LinkedList is Initializable{ | |
| event AddEntry(bytes32 head, string data, bytes32 next); | |
| //Struct will be our Node | |
| struct Node { | |
| bytes32 next; | |
| string data; | |
| } | |
| //Mappping will hold nodes | |
| mapping (bytes32 => Node) public nodes; | |
| //Length of LinkedList (initialize with constructor/initalizer) | |
| uint public length; | |
| //Head of list; | |
| bytes32 public head; | |
| function initialize() initializer public { | |
| length = 0; | |
| } | |
| function addNode(string memory _data) public returns (bool){ | |
| Node memory node = Node(head, _data); | |
| bytes32 id = keccak256(abi.encodePacked(node.data, length, now)); | |
| nodes[id] = node; | |
| head = id; | |
| length = length+1; | |
| emit AddEntry(head, node.data, node.next); | |
| } | |
| //popNode | |
| function popHead() public returns (bool) { | |
| //hold this to delete it | |
| bytes32 newHead = nodes[head].next; | |
| //delete it | |
| delete nodes[head]; | |
| head = newHead; | |
| length = length-1; | |
| } | |
| //Contract interface | |
| function getNode(bytes32 _node) external view returns (bytes32, string memory){ | |
| return (nodes[_node].next, nodes[_node].data); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment