Guest User

Untitled

a guest
Jan 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. pragma solidity ^0.5.0;
  2.  
  3. import "zos-lib/contracts/Initializable.sol";
  4.  
  5. contract LinkedList is Initializable{
  6.  
  7. event AddEntry(bytes32 head, string data, bytes32 next);
  8.  
  9. //Struct will be our Node
  10. struct Node {
  11. bytes32 next;
  12. string data;
  13. }
  14.  
  15.  
  16. //Mappping will hold nodes
  17. mapping (bytes32 => Node) public nodes;
  18.  
  19. //Length of LinkedList (initialize with constructor/initalizer)
  20. uint public length;
  21.  
  22. //Head of list;
  23. bytes32 public head;
  24.  
  25.  
  26. function initialize() initializer public {
  27. length = 0;
  28. }
  29.  
  30. function addNode(string memory _data) public returns (bool){
  31. Node memory node = Node(head, _data);
  32. bytes32 id = keccak256(abi.encodePacked(node.data, length, now));
  33. nodes[id] = node;
  34. head = id;
  35. length = length+1;
  36.  
  37. emit AddEntry(head, node.data, node.next);
  38. }
  39.  
  40. //popNode
  41. function popHead() public returns (bool) {
  42.  
  43. //hold this to delete it
  44. bytes32 newHead = nodes[head].next;
  45. //delete it
  46. delete nodes[head];
  47. head = newHead;
  48. length = length-1;
  49. }
  50.  
  51. //Contract interface
  52. function getNode(bytes32 _node) external view returns (bytes32, string memory){
  53. return (nodes[_node].next, nodes[_node].data);
  54. }
  55.  
  56.  
  57. }
Add Comment
Please, Sign In to add comment