solidity队列和数组实现

solidity实现队列和数组。结构体存储 uint[] bytes[] 类型 ,输入参数为string类型。

实现传入string字符串到队列,只将队列中不存在的插入队尾,并且当队列满时丢弃队首。

实现数组增删查改。

tips:输入测试例string类型时 如 “sb12” 记得加引号

solidity队列和数组实现_第1张图片

1.1 队列的实现 结构体内存储为bytes[] 输入为string

 
//pragma solidity ^0.4.15;
 
 
contract queue
{
    struct Queue {
        bytes[] data;
        uint front;
        uint rear;
    }
 
    
    // push
    function push(Queue storage q, bytes data) internal
    {
        if ((q.rear + 1) % q.data.length == q.front)
           pop(q); // throw first;
        q.data[q.rear] = data;
        q.rear = (q.rear + 1) % q.data.length;
    }
    // pop
    function pop(Queue storage q) internal returns (string d)
    {
        if (q.rear == q.front)
            return; // throw;
        bytes r = q.data[q.front];
        d=string(r);
        delete q.data[q.front];
        q.front = (q.front + 1) % q.data.length;
    }
}
 
contract QueueMain is queue {
    Queue requests;
    function QueueMain() {
        requests.data.length = 5;//队列长度
    }
    function addRequest(string d) {
        push(requests, bytes(d));
    }
    function popRequest() returns (string) {
        return pop(requests);
    }
 
    
}

1.2 队列的实现 结构体内存储为uint[] 输入为uint

 
//pragma solidity ^0.4.15;
 
 
contract queue
{
    struct Queue {
        uint[] data;
        uint front;
        uint rear;
    }
    
    // Queue length
    function length(Queue storage q) constant internal returns (uint) {
        return q.rear - q.front;
    }
    
    // push
    function push(Queue storage q, uint data) internal
    {
        if ((q.rear + 1) % q.data.length == q.front)
           pop(q); // throw first;
        q.data[q.rear] = data;
        q.rear = (q.rear + 1) % q.data.length;
    }
    // pop
    function pop(Queue storage q) internal returns (uint r)
    {
        if (q.rear == q.front)
            return; // throw;
        r = q.data[q.front];
        delete q.data[q.front];
        q.front = (q.front + 1) % q.data.length;
    }
}
 
contract QueueMain is queue {
    Queue requests;
    function QueueMain() {
        requests.data.length = 5;
    }
    function addRequest(uint d) {
        push(requests, d);
    }
    function popRequest() returns (uint) {
        return pop(requests);
    }
    function queueLength() returns (uint) {
        return length(requests);
    }
    
}

1.3 队列的实现 结构体内存储为bytes[] 输入为string

 
/*pragma solidity ^0.4.15;
实现一个队列
传入string类型的参数:
通过keccak256()比较该string字符串是否在队列当中存在
  存在:丢弃
  不存在:加入队列尾,当队列满时将队首丢弃
*/
 
 
contract queue
{
    struct Queue {
        bytes[] data;
        uint front;
        uint rear;
    }
    
      // push
    function push(Queue storage q, bytes data) internal
    {
        if ((q.rear + 1) % q.data.length == q.front)
           pop(q); // throw first;
        q.data[q.rear] = data;
        q.rear = (q.rear + 1) % q.data.length;
    }
    
     // Queue length
    function length(Queue storage q) constant internal returns (uint) {
        return q.rear - q.front;
    }
  
    // pop
    function pop(Queue storage q) internal returns (string d)
    {
        if (q.rear == q.front)
            return; // throw;
        bytes r = q.data[q.front];
        d=string(r);
        delete q.data[q.front];
        q.front = (q.front + 1) % q.data.length;
    }
    
}
 
contract QueueMain is queue {
    Queue requests;
    function QueueMain() {
        requests.data.length = 5;//控制队列长度
    }
    
 
    //put in string(_tx_hash)
    function addRequest(string d) returns(string) {
     for( uint i= requests.front; i<requests.rear+1; i++){
            if(keccak256(string(requests.data[i]))==keccak256(d)) {
                 //  length(requests);
                  return "have exist";
            }
     }       
             if(keccak256(string(requests.data[i]))!=keccak256(d)){
                  push(requests, bytes(d));
                  return"ojbk";
             }
                 
   }
    
    function popRequest() returns (string) {
        return pop(requests);
    }
    
  function queueLength() returns (uint) {
        return length(requests);
    }
    
    
}

1.4 数组增删查改实现

//pragma solidity 0.4.20;
contract testArray {
 
  //全局数组变量
  string[] public strArr;
  
  //构造函数,数组中添加字符abc
   
    function testArray() public{
      strArr.push("abc");
    }
  
  //增
   function Add(string str) {
      strArr.push(str);
   }
  
   //删
    function delByIndex(uint _index){
      uint len = strArr.length;
      if (_index > len) return;
      for (uint i = _index; i<len-1; i++){
        strArr[i] = strArr[i+1];
      }
      delete strArr[len-1];
      strArr.length--;
    }
    
     //查 数组索引
   function valueByIndex(uint _index) returns (string _value){
      if (_index > strArr.length-1) throw;
      return strArr[_index];
  }
    
    
  //改 数组索引,修改后的值
   function update(uint _index, string _value){
      if (_index > strArr.length-1) throw;
      strArr[_index] = _value;
   }
    
}

你可能感兴趣的:(智能合约开发)