ghostantanieh
ghostantanieh

Reputation: 59

help me with priority stack in C#

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

Answers (4)

Steve
Steve

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

Incognito
Incognito

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

tanascius
tanascius

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

Oded
Oded

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

Related Questions