Rockson
1
I need To find and remove objects in the middle of a queue of objects. I suppose that list, arraylist, stack and queue implemented in .net 2.0 are useless. Should I implement an other data structure by myself? And which should I use? Aren’t there any third party libraries?
If you have programming experience you can write it yourself according to your needs. Here is an idea. Write a double linked list which will hold your objects(Which has extra node class to hold objects). When you add to the queue, add to the linked list and also hold a dictionary for fast search of node to delete. So the code will be like:
// Actually a double linked list
public class LinkedList<T>{
LinkedListNode<T> head;
LinkedListNode<T> tail;
Dictionary<T,LinkedListNode<T>> searchDic;
void Remove(T item){
var nodeToRemove = searchDic[item];
searchDic.Remove(item);
nodeToRemove.Remove();
}
void Add(T item){
var newNode = new LinkedListNode(item);
tail.next = newNode;
tail = newNode;
searchDic.Add(item, newNode);
}
}
public class LinkedListNode<T>{
void Remove(); // This basically binds the next and previous
#region Constructors
// Insert Constructors here
#endregion
}
Note that it doesn’t have to be generic.
I don’t guarantee it will work or you have what it takes to make it work. Also code is not completed and you should complete it as an exercise.
If this works it means you can add, search and delete in constant time. One point I did oversee is you should first check if element exists in dictionary or you will get exceptions. You can use TryGetValue().
fafase
3
With an unordered collection there is no faster way than iterating through the whole thing and compare with the token.
Now, I said unordered which means if you use an ordered queue/list/array/else then you have some algorithm getting to the point faster using a binary search, then the point moves to the choice of sorting algorithm. BUT, Bill already did that for you so you can use list.Sort and that is it.
Once ordered, you can use a binary search that will increase your performance. Using an integer for comparison as opposed to string will also improve.