以太坊智能合约solidity队列数组

字符串string bytes 队列 输入输出string类型  

输入测试例如  "sb12" 记得加引号 


//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);
    }

    
}

 

uint queue队列 输入输出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;
    }
    //compare
    function compare(uint spv_out_num,Queue storage q) internal returns (uint r){
        
    }
    
    // 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);
    }
    
}

数组增删查改-转

//pragma solidity 0.4.20;
contract testArray {

  //声明一个全局数组变量
  string[] public strArr;
  
  /*
   * 构造函数
   * 默认向数组中添加一个字符
   */
    function testArray() public{
      strArr.push("Hi");
    }
  
  /*
   * @dev 添加一个值到数组
   * @param val string, 要传入的数值
   */
   function Add(string str) {
      strArr.push(str);
   }
  
  /*
   * @dev 更新数组的值
   * @param _index uint, 指定的索引
   * @param _value uint, 要修改的值
   */
   function update(uint _index, string _value){
      if (_index > strArr.length-1) throw;
      strArr[_index] = _value;
   }
   
   /*
    * @dev 获取指定数组索引的值
    * @param _index uint, 索引值
    * @return _value string, 返回结果
    */
   function valueByIndex(uint _index) returns (string _value){
      if (_index > strArr.length-1) throw;
      return strArr[_index];
  }
  
   /*
    * @dev 删除指定数组索引的值
    * @param _index uint, 索引值
    */
    function delByIndex(uint _index){
      uint len = strArr.length;
      if (_index > len) return;
      for (uint i = _index; i

/*pragma solidity ^0.4.15;
put in _tx_hash:
if exist then throw it 
else push into the queue
full then pop first
实现一个队列
输入传入的参数:该参数由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

微信公众号

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