Stack question

  • Thread starter Thread starter SP
  • Start date Start date
S

SP

Hi,

I've a Stack. Now I would use this to mantain a LIFO objects chain.
My problem is that I want to limit the Stack dimension. If the Stack is
full and I want to add a object, I eant to remove the bottom element
and put the new element in the top.
It is possbible with this Objet? Or I must make my struct?
 
SP said:
Hi,

I've a Stack. Now I would use this to mantain a LIFO objects chain.
My problem is that I want to limit the Stack dimension. If the Stack is
full and I want to add a object, I eant to remove the bottom element
and put the new element in the top.
It is possbible with this Objet? Or I must make my struct?

That description is not a stack, you probably should just use an arraylist
and possibly wrap it in your own class.
 
SP said:
Hi,

I've a Stack. Now I would use this to mantain a LIFO objects chain.
My problem is that I want to limit the Stack dimension. If the Stack is
full and I want to add a object, I eant to remove the bottom element
and put the new element in the top.

First, you have your terminology wrong. Last In, First Out would mean that
once you have reached the limit you would just stop adding new items. That
is because you are saying that the last element added should be the first
element removed, so why bother adding it at all. You are wanting a First In
First Out collection which is a Queue. You dequeue when the count exceeds
your limit.

myQueue.Enqueue(myObject);
if(myQueue.Count > 10)
myQueue.Dequeue();

PS
 
Why I'm wrong?
LAST IN FIRST OUT is using the structure, that is, when I need to read
the datastructure I Pop the Stack. When I want to insert a element i
need to Push in the Stack.

I not need a Queue.

I want a Stack that "record" the last n element pushed. The oldest must
be overwrite when pushing element when the count exceeds limit.

I need to use this in a Undo scenario. I want to record the action put
them in a Stack. When I make a action I make push,when redo a Pop. If a
make many action without redo, I want to limit my Stack. I want also
that new entry have priority, and oldest can be overwrite.

So the use is LIFO.






PS ha scritto:
 
SP said:
[...]
I need to use this in a Undo scenario. I want to record the action put
them in a Stack. When I make a action I make push,when redo a Pop. If a
make many action without redo, I want to limit my Stack. I want also
that new entry have priority, and oldest can be overwrite.

I'm not sure why people are giving you a hard time about your use of the
term "stack". It seems appropriate enough, even if you do want your stack
to allow old elements to be able to fall off the other end.

Anyway, seems to me the simplest way to implement your "stack" is to use
some form of a linked list (like, maybe using the LinkedList class :) ).
Designate one end of the list as the top of the stack, and the other as the
bottom. A double-linked list (such as LinkedList) allows for fast changes
to the list, including retrieving the first and last elements quickly, as
well as adding or removing elements.

So, when you want to push onto the stack, you simply add the element at the
end you've chosen for the top of your stack. If the length of the stack
exceeds your limit, you remove the element at the end that you've chosen for
the bottom of your stack.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top