473,326 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Stack question

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
4 1906
"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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
19
by: Jim | last post by:
I have spent the past few weeks designing a database for my company. The problem is I have started running into what I believe are stack overflow problems. There are two tab controls on the form...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
13
by: gmccallum | last post by:
General Info: A struct is stored on the stack and a class on the heap. A struct is a value type while a class is a reference type. Question: What if a struct contains a string...
8
by: LedZep | last post by:
What up everyone, I have to write a program that uses a stack to determine whether a string is a palindrome (a string that is spelled identically backward and forward). The program has to...
5
by: srikanth | last post by:
is it possible not to decrement the top variable after we remove(or retreive) a file from a stack >MY IDEA is:> if top is not decremented the stack is like it has some files existing in it with...
24
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as...
24
by: John | last post by:
I know this is a very fundamental question. I am still quite confused if the program call stack stack should always grows upwards from the bottom, or the opposite, or doesn't matter?? That means...
148
by: onkar | last post by:
Given the following code & variable i . int main(int argc,char **argv){ int i; printf("%d\n",i); return 0; } here i is allocated from bss or stack ?
87
by: CJ | last post by:
Hello: We know that C programs are often vulnerable to buffer overflows which overwrite the stack. But my question is: Why does C insist on storing local variables on the stack in the first...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.