hello,
any ideas about how to (first in last out) in an fixed length array?
thanks.
Hello and welcome,
Explain what you want to do exactly.
ok...
lets suppose we have an array with some integers inside....
int a[]={20,21,22,23,24,25};
and now we want to .....put a new number at position 0,
a[0]=19;
without replace the old value.
the old values i want to move them 1 step to the right and because the array is "fixed" the old value will delete.
so.... since i insert the value 19 ta position 0
the array want to be like this
{19,20,21,22,23,24}
in other words
Fisrt In - Last Out
the old values i want to move them 1 step to the right and because the array is "fixed" the old value will delete.
A four line for loop will do this.
byte arrayLen = sizeof(a) / sizeof(a[0]);
for(byte i=arrayLen - 1; i>0; i--)
{
a[i] = a[i-1];
}
If you want to have an array like a stack, don't try and put things in position 0 and move everything else around.
Instead, add and remove items from the other end of the array. Make sure the array is big enough. If you do this,
you only need to keep track of which array element is the current top of the stack ( or the first vacant location ),
and you never have to move things around.
Use a circular buffer like the Serial code does and keep track of the first and last elements.
Something like:
int first, last;
const int maxSize = 100;
int buffer[maxSize];
int addElement(int element)
{
if ((last + 1) == maxSize)
{
//set last to 0 and keep going...Check for overruns!
}
last++;
buffer[last] = element;
}
int getElement()
{
int ret = buffer[last];
last--;
if (last < 0)
{
// wrap around, if there are no elements, return an out-of-band error like -1
}
return ret;
}
First of alll,
i want to thank all of you, for your replies....and your time you spend for my issue.
i know that some tricks can give me the same result......with or without a lot of lines.
the point is that the real array i want to handle is big enought to think about a "loop".
so i am looking for this solution by using the follow methods
int a[10]={20,21,22,23,24,25,26,27,28,29}
int LastElement=a{10];
a.pop(LastElement); //Delete the last element
a.unshift(19); //Add new element (number 19 at 0 position)
i know about L.I.F.O (Last Input First Out) with the method ...pop() which delete the last element from the array.
KeithRB:
Something like:int first, last;
const int maxSize = 100;
int buffer[maxSize];
int addElement(int element)
{
if ((last + 1) == maxSize)
{
//set last to 0 and keep going...Check for overruns!
}
last++;
buffer[last] = element;
}
int getElement()
{
int ret = buffer[last];
last--;
if (last < 0)
{
// wrap around, if there are no elements, return an out-of-band error like -1
}
return ret;
}
You don't wrap around with a stack, you seem to have confused a queue or dequeue
with a stack.
Stack code looks like this:
const int stackSize = 100;
int stack[stackSize];
int sp = 0 ;
boolean push (int element)
{
if (sp < stackSize)
{
stack [sp++] = element ;
return true ;
}
else
return false ;
}
const int FAIL = -1 ; // or whatever value you want to represent empty stack
int pop()
{
if (sp > 0)
return stack [--sp] ;
else
return FAIL ;
}
BTW its always call "LIFO", last-in-first-out, not "FILO"