Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Priority List

I'm having trouble finding the right C# data structure. I'm looking for a PriorityList. It needs to have the following:

  • Only one item a given priority
  • Must remain sorted at all times
  • Ability to add an item to the end of the list -- prorityList.Add(item)
  • Ability to insert an item at a given priority -- priorityList.Add(3, item)
  • Ability to access any element using the priority -- priorityList[3]
  • Ability to remove an item at a given priority -- priorityList.RemoveAt(3)
  • When an item is added or removed, the rest of the list must shift up or down appropriately -- for example, if the third item is removed, the fourth item becomes the third item, the fifth item becomes the fourth item, etc.

C#'s SortedList looked promising, but it can't handle inserting at a priority that already exists or removing an element at a given priority (having the list shift appropriately in both cases).

like image 553
Jake Avatar asked Feb 19 '23 22:02

Jake


1 Answers

If you just use a standard List<T> that should give you everything you have asked for if you use priority == index.

like image 155
mikeagun Avatar answered Feb 28 '23 13:02

mikeagun