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. 3 8314
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.Collecti ons.Generic;
public class StackLimited
{
int _maxSize;
List<int_stack;
public StackLimited(in t maxSize)
{
_maxSize = maxSize;
_stack = new List<int>(maxSi ze);
}
public void Push(int item)
{
if(_stack.Conta ins(item))
_stack.Remove(i tem);
else
if (_stack.Count == maxSize)
{
Pop();
}
_stack.Insert(0 , item);
}
public int Pop()
{
if (_stack.Count 0)
{
data = _stack[0];
_stack.RemoveAt (0);
}
}
}
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
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.Collecti ons.Generic;
public class StackLimited
{
int _maxSize;
List<int_stack;
public StackLimited(in t maxSize)
{
_maxSize = maxSize;
_stack = new List<int>(maxSi ze);
}
public void Push(int item)
{
if(_stack.Conta ins(item))
_stack.Remove(i tem);
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: typingcat |
last post by:
Hello.
I'd like to know the SQL query for the following operation.
Table
AUTHOR BOOK
Bill A
Bill B
Charles A
Bill C
....
|
by: Adam Clauss |
last post by:
I have a page containing a list box. This list may contain duplicate
items - in which the ORDER is important.
ex:
a
b
b
a
is significant as compared to:
b
|
by: Geoff Jones |
last post by:
Hiya
I have a datatable with some columns. I would like to be able to retrieve a
list of the items in a column but only once e.g. if the column held
A,A,B,C,D,E,D then I'd like to be able to retrieve A,B,C,D,E.
Can anybody suggest how this can be done?
Thanks in advance
|
by: marcwentink |
last post by:
Could you please advise me what C++ STL container I could use that
would automaticly only store unique items. Hence prevents the insertion
of doubles.
It should behave like this, (I use 'vector' but I think this does not
behave this way)
vector<string> container;
container.push("1234");
|
by: Michel Racicot |
last post by:
In some language, it's possible to specify a "predefined" size for string
objects.
Exemple: string which is a string of 10 characters.
Is it possible to do this in C#?
Thank you
| |
by: sternr |
last post by:
Hey,
I'm using in my application a System.Collections.Stack object.
Is there a way to define a maximum size for the this stack object,
That when the max size is reached, the oldest value is deleted?
Thanks ahead
--sternr
|
by: Surinder Singh |
last post by:
Hi,
what would be max stack frame size (of single frame) on Linux and
Solaris/x86 of a running application stack?
I am compiling c program with gcc on Suse 10 Linux kernel
2.6.13-15.13-default i386 centrino processor.
gcc version -- 4.0.2
I have some 8KB structure (+ more data ) in a function and gcc with
|
by: =?Utf-8?B?YW1pcg==?= |
last post by:
Hi,
I have a Generic Object that has a private List field item.
I populate the List in a different function
I use a FOREACH LOOP with a FindAll function to get all items that have a
certain match for data in the list and do something to it.
The problem is that it repeats for all data items in the list and not
|
by: akadeco |
last post by:
Hi
I need to write a program and part it's functionality is to delete unique items in an array. That is, delete items in the array that only occur once.
I also need to delete the first occurrence of repeated items, and I'm pretty sure the solution to these two will go hand in hand.
Thanks
akadeco
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |