public void ConstructorPUT(int value) { DoublyLinkedListNode<int> n = new DoublyLinkedListNode<int>(value); PexAssert.AreEqual(value, n.Value); PexAssert.IsNull(n.Previous); PexAssert.IsNull(n.Next); }
public void ConstructorTest() { DoublyLinkedListNode<int> n = new DoublyLinkedListNode<int>(10); Assert.AreEqual(10, n.Value); Assert.IsNull(n.Previous); Assert.IsNull(n.Next); }
public static void ClassInitializer(TestContext context) { head = new DoublyLinkedListNode<int>(-1); doublyLinkedList= new DoublyLinkedList<int>(head); doublyLinkedList.Add(4); doublyLinkedList.Add(41); doublyLinkedList.Add(54); doublyLinkedList.Add(9); }
//注意:在后退和前进的逻辑中,不创建新的路径节点,而是基于已有的路径节点(引用) //后退 private void tsbtnBack_Click(object sender, EventArgs e) { if (curPathNode != firstPathNode) { curPathNode = curPathNode.PreNode; string prePath = curPathNode.Path; ShowFilesList(prePath, false); //前进按钮可用 tsbtnAdvance.Enabled = true; } else { //后退按钮不可用 tsbtnBack.Enabled = false; } }
private DoublyLinkedListNode FindNode(NetworkedUnit unit) { DoublyLinkedListNode current = head; if (current == null) { return(null); } do { if (current._item == unit) { break; } current = current.forward; } while (current.forward != head); return(current); }
public void RotateRight(NetworkedGridElement selectedGE) { DoublyLinkedListNode curNode = head; if (selectedGE.piece && selectedGE.piece.GetComponent <NetworkedGamePiece>() is NetworkedUnit) { NetworkedUnit selectedUnit = selectedGE.piece.GetComponent <NetworkedUnit>(); curNode = FindNode(selectedUnit); } if (curNode == null) { return; } if (activeMenu is NetworkedGridMenu) { ((NetworkedGridMenu)activeMenu).ChangeElementSelected(curNode.prev._item.GetComponent <NetworkedUnit>().gridElement.gameObject); } }
static DoublyLinkedListNode reverseDoublyLinkedList(DoublyLinkedListNode head) { DoublyLinkedListNode next = null; DoublyLinkedListNode prev = null; var curr = head; while (curr != null) { next = curr.next; curr.next = prev; prev = curr; curr.prev = next; curr = next; } head = prev; return(head); }
/// <summary> /// The count. /// </summary> /// <returns> /// The <see cref="int"/>. /// </returns> public int Count() { if (this.IsEmpty()) { return(0); } int index = 1; DoublyLinkedListNode <T> currentNode = this.start; while (currentNode.NextNode != null) { index++; currentNode = currentNode.NextNode; } return(index); }
/// <summary> /// The add last. /// </summary> /// <param name="newNode"> /// The new node. /// </param> public void AddLast(DoublyLinkedListNode <T> newNode) { // check for null this.CheckNull(newNode); // find the last node and execute the action var node = this.FindLastNode( (t) => { t.node.NextNode = newNode; newNode.PrevNode = t.node; }); if (node == null) { this.start = newNode; } }
//前进 private void tsbtnAdvance_Click(object sender, EventArgs e) { if (curPathNode.NextNode != null) { curPathNode = curPathNode.NextNode; string nextPath = curPathNode.Path; ShowFilesList(nextPath, false); //后退按钮可用 tsbtnBack.Enabled = true; } else { //前进按钮不可用 tsbtnAdvance.Enabled = false; } }
public CompositeMetricReader(IEnumerable <MetricReader> readers) { Guard.Null(readers, nameof(readers)); using var iter = readers.GetEnumerator(); if (!iter.MoveNext()) { throw new ArgumentException($"'{iter}' is null or empty", nameof(iter)); } this.head = new DoublyLinkedListNode(iter.Current); this.tail = this.head; while (iter.MoveNext()) { this.AddReader(iter.Current); } }
static DoublyLinkedListNode reverseUtil(DoublyLinkedListNode head, DoublyLinkedListNode prevNode, DoublyLinkedListNode currNode, DoublyLinkedListNode nextNode) { if (currNode.next == null) { currNode.next = prevNode; currNode.prev = null; head = currNode; return(head); } currNode.next = prevNode; currNode.prev = nextNode; prevNode = currNode; currNode = nextNode; nextNode = currNode.next; return(reverseUtil(head, prevNode, currNode, nextNode)); }
public CompositeProcessor(IEnumerable <BaseProcessor <T> > processors) { Guard.ThrowIfNull(processors, nameof(processors)); using var iter = processors.GetEnumerator(); if (!iter.MoveNext()) { throw new ArgumentException($"'{iter}' is null or empty", nameof(iter)); } this.head = new DoublyLinkedListNode(iter.Current); this.tail = this.head; while (iter.MoveNext()) { this.AddProcessor(iter.Current); } }
public void InsertBefore(DoublyLinkedListNode <T> node, T value) { Size++; var newNode = new DoublyLinkedListNode <T>(value); newNode.Next = node; newNode.Prev = node.Prev; newNode.Next.Prev = newNode; if (newNode.Prev != null) { newNode.Prev.Next = newNode; } else { First = newNode; } }
public void AddFirst(T item) { DoublyLinkedListNode <T> node = new DoublyLinkedListNode <T>(item); if (IsEmpty) { Last = node; } else { node.Next = First; First.Previous = node; } First = node; Count++; }
Public AddHead(int value) { DoublyLinkedListNode <int> adding = new DoublyLinkedListNode <int>(value, null, head); if (head != null) { head.Previous = adding; } head = adding; if (tail == null) { tail = head; } count += 1; }
public void InsertAfter(DoublyLinkedListNode <T> node, T value) { Size++; var newNode = new DoublyLinkedListNode <T>(value); newNode.Prev = node; newNode.Next = node.Next; newNode.Prev.Next = newNode; if (newNode.Next != null) { newNode.Next.Prev = newNode; } else { Last = newNode; } }
/// <summary> /// Put a page number(key) and the page content/or reference to content as value in the cache /// </summary> /// <param name="key"></param> /// <param name="value"></param> public void Put(int key, int value) { // Algo: if the key is present in dictionary, do nothing // if the key is not present in the dictionary, add to dictionary and also enqueue in queue // while enqueue, make sure that the capacity is not reached, else dequeue the LRU element if (!CacheDictionary.ContainsKey(key)) { DoublyLinkedListNode <KeyValuePair <int, int> > nodeToAdd = new DoublyLinkedListNode <KeyValuePair <int, int> >(new KeyValuePair <int, int>(key, value)); // Add to cacheDictionary CacheDictionary[key] = nodeToAdd; // Put the node in the Last of Queue QueueForLRU.Enqueue(nodeToAdd); LimitQueueToCacheCapacity(); } }
public static DoublyLinkedListNode Reverse(DoublyLinkedListNode head) { // empty list if (head == null) { return(null); } // one element long list if (head.next == null && head.prev == null) { return(head); } DoublyLinkedListNode current = head, next; bool headCheck = true; while (true) { next = current.next; // the head if (headCheck) { current.prev = current.next; current.next = null; headCheck = false; } else if (current.next == null) // last element { current.next = current.prev; current.prev = null; return(current); } else // all the middle elements { DoublyLinkedListNode temp = current.prev; current.prev = current.next; current.next = temp; } current = next; } }
public void AddFirst(DoublyLinkedListNode <T> node) { DoublyLinkedListNode <T> temp = Head; Head = node; Head.Next = temp; Count++; if (Count == 1) { Tail = Head; } else { temp.Previous = Head; } }
/// <summary> /// The get height. /// </summary> /// <param name="node"> /// The node. /// </param> /// <returns> /// The <see cref="int"/>. /// </returns> private int GetHeight(DoublyLinkedListNode <int> node) { if (node == null) { return(0); } var hL = this.GetHeight(node.PrevNode); var hR = this.GetHeight(node.NextNode); if (hL > hR) { return(1 + hL); } else { return(1 + hR); } }
/// <summary> /// The display. /// </summary> /// <param name="node"> /// The node. /// </param> /// <param name="level"> /// The level. /// </param> public void Display(DoublyLinkedListNode <int> node, int level) { if (node == null) { return; } this.Display(node.NextNode, level + 1); Debug.WriteLine(""); for (int i = 0; i < level; i++) { Debug.Write(" "); } Debug.Write(node.Value); this.Display(node.PrevNode, level + 1); }
public static DoublyLinkedListNode ReverseDoublyLinkedList(DoublyLinkedListNode head) { if (head == null || head.next == null) { return(head); } while (head != null) { var temp = head.next; head.next = head.previous; head.previous = head.next; head = temp; } return(head); }
public void ReverseDoublyLinkedList() { var current = head; DoublyLinkedListNode temp = null; while (current != null) { temp = current.previous; current.previous = current.next; current.next = temp; current = current.previous; } if (head != null) { head = temp; } head = temp.previous; }
// Complete the sortedInsert function below. /* * For your reference: * * DoublyLinkedListNode { * int data; * DoublyLinkedListNode next; * DoublyLinkedListNode prev; * } * */ static DoublyLinkedListNode sortedInsert(DoublyLinkedListNode head, int data) { DoublyLinkedListNode temp = head; while (temp != null) { // Node to be inserted at the head if (temp.data > data) { DoublyLinkedListNode node = new DoublyLinkedListNode(data); DoublyLinkedListNode tempNext = temp; temp.prev = node; node.next = tempNext; head = node; return(head); } // Node to be inserted at the middle if (temp.next != null && temp.prev != null && temp.next.data > data && temp.prev.data < data) { DoublyLinkedListNode node = new DoublyLinkedListNode(data); DoublyLinkedListNode tempNext = temp.next; node.prev = temp; node.next = tempNext; temp.next = node; return(head); } // Node to be inserted at the tail if (temp.next == null && temp.data < data) { DoublyLinkedListNode node = new DoublyLinkedListNode(data); DoublyLinkedListNode tempNext = temp; temp.next = node; node.prev = tempNext; } temp = temp.next; } return(head); }
public int Get(int key) { if (!_hashedNodes.ContainsKey(key)) { return(-1); } // Grab the selected Node var selectedNode = _hashedNodes[key]; // Let's re-link the surrounding nodes if necessary var lessRecentNode = selectedNode.LessRecent; var moreRecentNode = selectedNode.MoreRecent; // If this is the most recent node then return the value if (moreRecentNode == null) { return(selectedNode.Value); } // If this is the least recent node (and not also the most recent) then re-link _leastRecent if (lessRecentNode == null) { _leastRecent = moreRecentNode; } // Link neighboring nodes moreRecentNode.LessRecent = lessRecentNode; if (lessRecentNode != null) { lessRecentNode.MoreRecent = moreRecentNode; } // Link the selected Node to the most recent selectedNode.LessRecent = _mostRecent; selectedNode.MoreRecent = null; _mostRecent.MoreRecent = selectedNode; // Make the most recent node point to the selected Node _mostRecent = selectedNode; return(selectedNode.Value); }
// Add one element at the end public void PushBack(T value) { var newNode = new DoublyLinkedListNode <T>(value); if (Last == null) { Last = newNode; First = newNode; Size++; return; } newNode.Prev = Last; Last.Next = newNode; Last = newNode; Size++; }
// Add one element at the beginning public void PushFront(T value) { var newNode = new DoublyLinkedListNode <T>(value); // If list is empty if (First == null) { First = newNode; Last = newNode; Size++; return; } newNode.Next = First; First.Prev = newNode; First = newNode; Size++; }
private static DoublyLinkedListNode sortedInsert(DoublyLinkedListNode head, int data) { var current = head; var newNode = new DoublyLinkedListNode(data); if (current == null) { return(newNode); } if (data <= current.data) { newNode.next = current; current.prev = newNode; newNode.prev = null; return(head = newNode); } else { while (current.next != null) { if (data <= current.next.data) { //insert in the midle var storeNext = current.next; current.next = newNode; newNode.next = storeNext; storeNext.prev = newNode; return(head); } current = current.next; } //insert at the end current.next = newNode; newNode.prev = current; newNode.next = null; return(head); } }
static DoublyLinkedListNode reverse(DoublyLinkedListNode currentNode) { while (true) { var next = currentNode.next; // Invert the links currentNode.next = currentNode.prev; currentNode.prev = next; if (next == null) { // Return the tail as the new head return(currentNode); } currentNode = next; } }
// Complete the reverse function below. /* * For your reference: * * DoublyLinkedListNode { * int data; * DoublyLinkedListNode next; * DoublyLinkedListNode prev; * } * */ static DoublyLinkedListNode reverse(DoublyLinkedListNode head) { if (head == null) { return(null); } while (head != null) { DoublyLinkedListNode i = head.prev; head.prev = head.next; head.next = i; if (head.prev == null) { return(head); } head = head.prev; } return(head); }
static DoublyLinkedListNode sortedInsert(DoublyLinkedListNode head, int data) { if (head == null) { return(new DoublyLinkedListNode(data)); } var currentNode = head; // Find the position to insert the node while (data > currentNode.data) { if (currentNode.next == null) { // Insert into tail var tailNode = new DoublyLinkedListNode(data); currentNode.next = tailNode; tailNode.prev = currentNode; return(head); } currentNode = currentNode.next; } // Insert and update links var prevNode = currentNode.prev; var newNode = new DoublyLinkedListNode(data); newNode.next = currentNode; newNode.prev = prevNode; currentNode.prev = newNode; if (prevNode == null) { // New node is the head return(newNode); } prevNode.next = newNode; return(head); }
/// <summary> /// The pop. /// </summary> /// <returns> /// The <see cref="T"/>. /// </returns> public T Pop() { if (this.lastNode == null) { return(default(T)); } var currentLastNode = this.lastNode; this.lastNode = currentLastNode.PrevNode; if (this.lastNode != null) { this.lastNode.NextNode = null; currentLastNode.PrevNode = null; } return(currentLastNode.Value); }