Reputation: 59
I want to create a stack which will have the following methods:
getValueOf ("key")
getKeyOf ("value")
addKeyAndValue ("key" "value")
delete ("key")
can anyone please help me as I am very new to C sharp
Upvotes: 0
Views: 691
Reputation: 50573
There is a stack class in C#, Here is a quick guide to using it:
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=65
Hope that helps.
Upvotes: 0
Reputation: 16597
You need to check Dictionary
class or if you want as you said "priority" (sorted) you need to check SortedList
Class.
EDIT : If you need to implement the stack by your own. You need to check ICollection
, IEnumerable
and ICloneable
interfaces.
However better to use already exiting classes in Framework if they fit your needs. And based on your description seems you need Dictionary
or SortedList
.
Upvotes: 3
Reputation: 53944
Did you look at the Dictionary class?
That's no stack, but the methods you describe are not typical for a stack, either.
getValueOf( "key" ):
var value = dictionary[key];
addKeyAndValue( "key" "value" ):
dictionary.Add( key, value );
delete( "key" )
dictionary.Remove( key );
getKeyOf ("value") with linq:
var key = dictionary.Where( pair => pair.Value == 10 )
.Select( pair => pair.Key ).FirstOrDefault();
Upvotes: 2
Reputation: 499132
Are you sure you need a stack? What you are describing sounds very much like a Dictionary
collection.
A stack is a very specific data structure and would not normally allow you to search by keys of values - it is a "first in last out" data structure that would not have the methods you want.
A dictionary on the other hand has these search capabilities and has methods on it that are pretty close to what you are describing.
Upvotes: 1