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

How to do?: Stack of limited size containing unique items.

P: n/a
Stack of limited size containing unique items?

Hi guys,

How would I implement a stack of limited size containing unique items?

For example. Suppose my stack has [3,5]. I add 2 to it and it is now
[3,5,2]. Now I want to add 5 to the unique item stack so it should
now be: [3,2,5]. The item added is pulled from the stack, then pushed.
The stack should also have a maximum size of 4 items. I add 4 and 7
respectively. It becomes: [3,2,5,4] then, after adding 7: [2,5,4,7].
The first item is dropped when the stack is at maximum capacity and a
new item added. Should this be a queue rather than a stack? I said
'stack' because, apart from the limited size behavior of the oldest
item being dropped in favour of the new item, I want it to have the
FILO behavior of a conventional stack.

Sep 28 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 28 Sep, 15:29, mark4asp <mark4...@gmail.comwrote:
Stack of limited size containing unique items?

Hi guys,

How would I implement a stack of limited size containing unique items?

For example. Suppose my stack has [3,5]. I add 2 to it and it is now
[3,5,2]. Now I want to add 5 to the unique item stack so it should
now be: [3,2,5]. The item added is pulled from the stack, then pushed.
The stack should also have a maximum size of 4 items. I add 4 and 7
respectively. It becomes: [3,2,5,4] then, after adding 7: [2,5,4,7].
The first item is dropped when the stack is at maximum capacity and a
new item added. Should this be a queue rather than a stack? I said
'stack' because, apart from the limited size behavior of the oldest
item being dropped in favour of the new item, I want it to have the
FILO behavior of a conventional stack.
OK, figured it.

using System;
using System.Collections.Generic;

public class StackLimited
{
int _maxSize;
List<int_stack;
public StackLimited(int maxSize)
{
_maxSize = maxSize;
_stack = new List<int>(maxSize);
}
public void Push(int item)
{
if(_stack.Contains(item))
_stack.Remove(item);
else
if (_stack.Count == maxSize)
{
Pop();
}
_stack.Insert(0, item);
}
public int Pop()
{
if (_stack.Count 0)
{
data = _stack[0];
_stack.RemoveAt(0);
}
}
}

Sep 28 '07 #2

P: n/a
mark4asp wrote:
Stack of limited size containing unique items?

Hi guys,

How would I implement a stack of limited size containing unique items?

For example. Suppose my stack has [3,5]. I add 2 to it and it is now
[3,5,2]. Now I want to add 5 to the unique item stack so it should
now be: [3,2,5]. The item added is pulled from the stack, then pushed.
The stack should also have a maximum size of 4 items. I add 4 and 7
respectively. It becomes: [3,2,5,4] then, after adding 7: [2,5,4,7].
The first item is dropped when the stack is at maximum capacity and a
new item added. Should this be a queue rather than a stack? I said
'stack' because, apart from the limited size behavior of the oldest
item being dropped in favour of the new item, I want it to have the
FILO behavior of a conventional stack.
Neither a Stack<or a Queue<has any method for removing an item in
the middle of the collection, so they aren't really useful in this case.

I would suggest that you use a List<or array as internal storage in a
class, and just implement the methods you need. The List<class has the
methods Contains and RemoveAt that might be useful, but as your
collection has a fixed maximum size, using an array might feel like a
more straight forward solution.

--
Göran Andersson
_____
http://www.guffa.com
Sep 28 '07 #3

P: n/a
mark4asp wrote:
On 28 Sep, 15:29, mark4asp <mark4...@gmail.comwrote:
>Stack of limited size containing unique items?

Hi guys,

How would I implement a stack of limited size containing unique items?

For example. Suppose my stack has [3,5]. I add 2 to it and it is now
[3,5,2]. Now I want to add 5 to the unique item stack so it should
now be: [3,2,5]. The item added is pulled from the stack, then pushed.
The stack should also have a maximum size of 4 items. I add 4 and 7
respectively. It becomes: [3,2,5,4] then, after adding 7: [2,5,4,7].
The first item is dropped when the stack is at maximum capacity and a
new item added. Should this be a queue rather than a stack? I said
'stack' because, apart from the limited size behavior of the oldest
item being dropped in favour of the new item, I want it to have the
FILO behavior of a conventional stack.

OK, figured it.

using System;
using System.Collections.Generic;

public class StackLimited
{
int _maxSize;
List<int_stack;
public StackLimited(int maxSize)
{
_maxSize = maxSize;
_stack = new List<int>(maxSize);
}
public void Push(int item)
{
if(_stack.Contains(item))
_stack.Remove(item);
else
if (_stack.Count == maxSize)
{
Pop();
But that will remove the most recent item, not the oldest one.
}
_stack.Insert(0, item);
}
public int Pop()
{
if (_stack.Count 0)
{
data = _stack[0];
_stack.RemoveAt(0);
}
}
}

--
Göran Andersson
_____
http://www.guffa.com
Sep 28 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.