/// <inheritdoc/> public bool TryChangeValue(K key, V value, bool moveToTop, bool replaceIfExists, out V oldValue) { LinkedListNode <Pair <K, V> > valueContainer; if (map.TryGetValue(key, out valueContainer)) { oldValue = valueContainer.Value.Second; if (moveToTop) { list.Remove(valueContainer); list.AddFirst(valueContainer); } if (replaceIfExists) { valueContainer.Value = new Pair <K, V>(key, value); } return(true); } else { oldValue = default(V); valueContainer = list.AddFirst(new Pair <K, V>(key, value)); try { map.Add(key, valueContainer); } catch (Exception e) { list.RemoveFirst(); throw; } return(false); } }
/// <summary> /// Uses the Peek method to return and remove the first item in the Linked list /// </summary> /// <returns></returns> public T Pop() { var value = Peek(); _list.RemoveFirst(); return(value); }
public Dog DequeueDog() { if (_dogs.Count == 0) { throw new Exception("empty"); } Dog dog = _dogs.First.Value; _dogs.RemoveFirst(); return(dog); }
public Cat DequeueCat() { if (_cats.Count == 0) { throw new Exception("empty"); } Cat cat = _cats.First.Value; _cats.RemoveFirst(); return(cat); }
// Removes and returns the top item from the stack public T Pop() { if (_list.Count == 0) { throw new InvalidOperationException("The stack is empty"); } T value = _list.First.Value; _list.RemoveFirst(); return(value); }
public T Pop() { var node = list.First; if (node == null) { return(default(T)); } list.RemoveFirst(); map.Remove(node.Value); return(node.Value); }
public Animal DequeueAny() { if (_animals.Count == 0) { throw new Exception("empty"); } LinkedListNode <Animal> last = _animals.First; _animals.RemoveFirst(); return(last.Value); }
public T Dequeue() { if (_items.Count == 0) { throw new InvalidOperationException("The queue is empty."); } T value = _items.First.Value; _items.RemoveFirst(); return(value); }
/// <summary> /// This methods removes the first element from the queue. /// </summary> /// <returns>Returns the first element removed from the queue.</returns> public T Dequeue() { if (_list.Count == 0) { throw new InvalidOperationException("Queue is empty."); } T item = _list.First.Value; _list.RemoveFirst(); return(item); }
/// <summary> /// Remove first element in the queue and return it. /// </summary> /// <returns>Element of type T.</returns> public T Dequeue() { if (LinkedList.Count > 0) { T firstItem = LinkedList.First.Value; LinkedList.RemoveFirst(); return(firstItem); } else { throw new System.Exception("Unable to dequeue empty queue."); } }
/// <summary> /// Removes and returns the highest priority item from the queue /// </summary> /// <returns>The front item from the queue</returns> public T Dequeue() { if (_items.Count == 0) { throw new InvalidOperationException("The queue is empty."); } // store the last value in a temporary variable T value = _items.First.Value; // remove the last item _items.RemoveFirst(); // return the stored last value return(value); }
public T Pop() { if (_list.Count == 0) { throw new InvalidOperationException("The statck is empty"); } // this is first out so we take the firstelement // this is first out T value = _list.First.Value; _list.RemoveFirst(); // now return the first return(value); }
/// <summary> /// Does a search for a path from start to finish on /// graph using given search type /// </summary> /// <param name="start">start value</param> /// <param name="finish">finish value</param> /// <param name="graph">graph to search</param> /// <returns>string for path or empty string if there is no path</returns> string Search(int start, int finish, Graph <int> graph, SearchType searchType) { System.Collections.Generic.LinkedList <GraphNode <int> > searchList = new System.Collections.Generic.LinkedList <GraphNode <int> >(); //Special case for start and finish the same if (start == finish) { return(start.ToString()); } else if (graph.Find(start) == null || graph.Find(finish) == null) { //Start or finish not in graph return(""); } else { //Add start node to dictionary and search list GraphNode <int> startNode = graph.Find(start); Dictionary <GraphNode <int>, PathNodeInfo <int> > pathNodes = new Dictionary <GraphNode <int>, PathNodeInfo <int> >(); pathNodes.Add(startNode, new PathNodeInfo <int>(null)); searchList.AddFirst(startNode); //Loop until we exhaust all possible paths while (searchList.Count > 0) { //Extract front of search list GraphNode <int> currentNode = searchList.First.Value; print("Current Node: " + currentNode.Value); searchList.RemoveFirst(); //Explore each neighbor of this node foreach (GraphNode <int> neighbor in currentNode.Neighbors) { //Check for found finish if (neighbor.Value == finish) { pathNodes.Add(neighbor, new PathNodeInfo <int>(currentNode)); return(ConvertPathToString(neighbor, pathNodes)); } else if (pathNodes.ContainsKey(neighbor)) { //Found a cycle, so skip this neighbor continue; } else { //Link neighbor to current node in path pathNodes.Add(neighbor, new PathNodeInfo <int>(currentNode)); //Add neighbor to front or back of search list if (searchType == SearchType.DepthFirst) { searchList.AddFirst(neighbor); } else { searchList.AddLast(neighbor); } print("Just added " + neighbor.Value + " to search list"); } } } //Didn't find a path from start to finish return(""); } }
public void RemoveFirst() => linkedList.RemoveFirst();
// On error or failure, returns null. private static string locate_fxc_on_disk() { try { long EndTime = DateTime.Now.Ticks + MAX_LOCATE_TIME; // Paths to process System.Collections.Generic.LinkedList<string> PathQueue = new System.Collections.Generic.LinkedList<string>(); // Path already processed in form Key=Path, Value="1" System.Collections.Specialized.StringDictionary Processed = new System.Collections.Specialized.StringDictionary(); // Add default paths // - Program files PathQueue.AddLast(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)); // - Hard disks foreach (DriveInfo Drive in DriveInfo.GetDrives()) if (Drive.DriveType == DriveType.Fixed) PathQueue.AddLast(Drive.RootDirectory.Name); // Processing loop - berform breadth first search (BFS) algorithm while (PathQueue.Count > 0) { // Get directory to process string Dir = PathQueue.First.Value; PathQueue.RemoveFirst(); // Already processed if (Processed.ContainsKey(Dir)) continue; // Add to processed Processed.Add(Dir, "1"); try { // Look for fxc.exe file string[] FxcFiles = Directory.GetFiles(Dir, "fxc.exe"); if (FxcFiles.Length > 0) return FxcFiles[0]; // Look for subdirectories foreach (string SubDir in Directory.GetDirectories(Dir)) { // Interesting directory - add at the beginning of the queue if (DirectoryIsInteresting(SubDir)) PathQueue.AddFirst(SubDir); // Not interesting - add at the end of the queue else PathQueue.AddLast(SubDir); } } catch (Exception ) { } // Time out if (DateTime.Now.Ticks >= EndTime) return null; } // Not found return null; } catch (Exception ) { return null; } }