How do you return the value pushed before with only one function? I read some information about events, so I think it may be a solution, but it doesn't return the exact value pushed before. There is my new source below which added events:
pragma solidity ^0.4.0;
////////////////////////////////////////////////////////////
// This is an example contract hacked together at a meetup.
// It is by far not complete and only used to show some
// features of Solidity.
////////////////////////////////////////////////////////////
contract queue
{
struct Queue {
uint256[] data;
uint256 front;
uint256 back;
}
/// @dev the number of elements stored in the queue.
function length(Queue storage q) constant internal returns (uint256) {
return q.back - q.front;
}
/// @dev the number of elements this queue can hold
function capacity(Queue storage q) constant internal returns (uint256) {
return q.data.length - 1;
}
/// @dev push a new element to the back of the queue
function push(Queue storage q, uint256 data) internal
{
if ((q.back + 1) % q.data.length == q.front)
return; // throw;
q.data[q.back] = data;
q.back = (q.back + 1) % q.data.length;
}
/// @dev remove and return the element at the front of the queue
function pop(Queue storage q) internal returns (uint256 r)
{
if (q.back == q.front)
return; // throw;
r = q.data[q.front];
delete q.data[q.front];
q.front = (q.front + 1) % q.data.length;
return r;
}
Queue requests;
event PopEvent(bool ok); //my event designed by me here.
function queue() {
requests.data.length = 200;
}
function addRequest(uint256 d) {
push(requests, d);
}
function popRequest() returns (uint256) {
PopEvent(true);
return pop(requests);
}
function queueLength() constant returns (uint256) {
return length(requests);
}
function hello() constant returns(string s){
return 'hello world!';
}
}
I just want the popRequest function to return the exact value pushed before.