By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,336 Members | 2,307 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,336 IT Pros & Developers. It's quick & easy.

Stack question

P: n/a
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?

Nov 19 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"SP" <da************@gmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
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.
>

Nov 19 '06 #2

P: n/a
PS

"SP" <da************@gmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
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
It is possbible with this Objet? Or I must make my struct?
Nov 19 '06 #3

P: n/a
SP
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" <da************@gmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
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
It is possbible with this Objet? Or I must make my struct?
Nov 23 '06 #4

P: n/a
"SP" <da************@gmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
[...]
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
Nov 23 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.