区块链技术博客
www.b2bchain.cn

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

这篇文章主要介绍了以太坊智能合约solidity队列数组,通过具体代码讲解7849并且分析了以太坊智能合约solidity队列数组的详细步骤与相关技巧,需要的朋友可以参考下

本文实例讲述了以太坊智能合约solidity队列数组。分享给大家供大家参考文章查询地址https://www.b2bchain.cn/?p=7849。具体如下:

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

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

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

  //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<len-1; i++){         strArr[i] = strArr[i+1];       }       delete strArr[len-1];       strArr.length--;     } }
  /*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<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);     }           } 

微信公众号

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

本文地址https://www.b2bchain.cn/?p=7849

赞(0) 打赏
部分文章转自网络,侵权联系删除b2bchain区块链学习技术社区 » 以太坊智能合约solidity队列数组
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

b2b链

联系我们联系我们