Skip to content

Instantly share code, notes, and snippets.

@crazyrabbitLTC
Created November 28, 2018 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazyrabbitLTC/505cf266d2da0d516780ebfb28fdf8bb to your computer and use it in GitHub Desktop.
Save crazyrabbitLTC/505cf266d2da0d516780ebfb28fdf8bb to your computer and use it in GitHub Desktop.
Basic LinkedList for Solidity
pragma solidity ^0.4.24;
contract LinkedList {
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;
constructor() public {
length = 0;
}
function addNode(string _data) public returns (bool){
Node memory node = Node(head, _data);
bytes32 id = sha3(node.data, length, now);
nodes[id] = node;
head = id;
length = length+1;
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) public returns (bytes32, string){
return (nodes[_node].next, nodes[_node].data);
}
}
@cjxe
Copy link

cjxe commented Jan 30, 2022

Is this reversed linked list? The head is updating to the last node's id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment