private (int len, MyLinkedListNode <int> tail) LengthAndTail(MyLinkedListNode <int> list) { var current = list; MyLinkedListNode <int> tail = list; var size = 0; while (current != null) { size++; tail = current; current = current.Next; } return(size, tail); }
public void TestInvalidInput() { MyLinkedListNode head = null; Assert.Throws <ArgumentNullException> ( () => DeleteNodeFromLinkedList.Delete(ref head, new MyLinkedListNode(30)) ); MyLinkedListNode head1 = new MyLinkedListNode(2); Assert.Throws <ArgumentNullException> ( () => DeleteNodeFromLinkedList.Delete(ref head1, null) ); }
private MyLinkedListNode GetMiddle(MyLinkedListNode head) { if (head == null) { return(head); } MyLinkedListNode slow = head; MyLinkedListNode fast = head; while (fast.nextNode != null && fast.nextNode.nextNode != null) { slow = slow.nextNode; fast = fast.nextNode.nextNode; } return(slow); }
public void TestAddBeforeDoesnotBelong() { bool result = Program.GetValues("list.txt", out MyLinkedList l); MyLinkedListNode n = new MyLinkedListNode(); try { l.AddBefore(n, 2, 4); } catch { result = false; } Assert.AreEqual(false, result); }
public MyLinkedListNode FindNodeAsPosition(MyLinkedListNode node, int pos) { MyLinkedListNode tNode = node; int tPos = 1; while (tNode.next != null) { if (tPos == pos) { return(tNode.next); } tNode = tNode.next; tPos++; } return(null); }
public override void Add(MyDataStruct nauj) { if (length > 0) { prevNode = currentNode; currentNode.nextNode = new MyLinkedListNode(nauj); currentNode = currentNode.nextNode; length++; } else { headNode = new MyLinkedListNode(nauj); currentNode = headNode; length++; } }
// 这种写法利用了while循环,程序栈没有溢出风险,问题在于写法比较繁琐,要多次判断两个链表头节点的大小 private static MyLinkedListNode MergeUsingWhile(MyLinkedListNode head1, MyLinkedListNode head2) { if (head1 == null) { return(head2); } if (head2 == null) { return(head1); } // head节点为null,而且要从两个链表中取一个来作为头节点,这次比对不能避免 MyLinkedListNode head = null; if (head1.Value > head2.Value) { head = head2; head2 = head2.Next; } else { head = head1; head1 = head1.Next; } MyLinkedListNode current = head; while (head1 != null && head2 != null) { if (head1.Value > head2.Value) { current.Next = head2; current = head2; head2 = head2.Next; } else { current.Next = head1; current = head1; head1 = head1.Next; } } current.Next = head1 == null ? head2 : head1; return(head); }
public MyLinkedListNode AddTwoNumbers(MyLinkedListNode l1, MyLinkedListNode l2) { int carry = 0; MyLinkedListNode root = new MyLinkedListNode(-1); MyLinkedListNode currentNode = root; while (l1 != null && l2 != null) { int sum = l1.Value + l2.Value + carry; carry = sum / 10; currentNode.Next = new MyLinkedListNode(sum % 10); currentNode = currentNode.Next; l1 = l1.Next; l2 = l2.Next; } if (l1 == null && l2 != null) { while (l2 != null) { int sum = l2.Value + carry; currentNode.Next = new MyLinkedListNode(sum % 10); carry = sum / 10; currentNode = currentNode.Next; l2 = l2.Next; } } else if (l1 != null && l2 == null) { while (l1 != null) { int sum = l1.Value + carry; currentNode.Next = new MyLinkedListNode(sum % 10); carry = sum / 10; currentNode = currentNode.Next; l1 = l1.Next; } } if (carry != 0) { currentNode.Next = new MyLinkedListNode(carry); } return(root.Next); }
private MyLinkedListNode GetNode(int index) { if (list == null || index < 0 || index >= Length) { return(null); } MyLinkedListNode ptr = list; int i = 0; while (i < index) { ptr = ptr.Next; i++; } return(ptr); }
/// <summary> /// 设定两个指针在链表上移动,一个一次移动一个节点,另一个一次移动两个节点 /// 当两个指针相遇时,相遇节点一定在环上;如果指针没有相遇就到达了链表尾部,说明环不存在 /// </summary> /// <param name="head">链表的头节点</param> /// <returns>两个指针相遇的节点</returns> private static MyLinkedListNode FindMeetingNode(ref MyLinkedListNode head) { MyLinkedListNode pFast = head.Next, pSlow = head; while (pFast != null && pSlow != null) { if (pFast == pSlow) { return(pFast); } pSlow = pSlow.Next; pFast = pFast.Next; pFast = pFast?.Next; } return(null); }
public void TestLinkedListWithRing() { MyLinkedListNode head = new MyLinkedListNode(1); MyLinkedListNode current = head; int[] source = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 25, 67, 98 }; for (int i = 1; i < source.Length; i++) { current.Next = new MyLinkedListNode(source[i]); current = current.Next; } current.Next = head.Next.Next.Next;// 4为入口节点 Assert.Equal(4, LinkedListWithRing.FindEntry(ref head).Value); }
public void TestDesigner3() { MyLinkedList <string> l = new MyLinkedList <string>(8); MyLinkedListNode <string> n = l.First; bool result = l.Count == 8; do { if (n.Value != default) { result = false; break; } n = n.Next; }while (n != l.First); Assert.AreEqual(true, result); }
public void TestWithAllNodesDeleted() { MyLinkedListNode head = new MyLinkedListNode(4); MyLinkedListNode current = head; int[] source = { 4, 4, 4, 4, 4, 4 }; for (int i = 1; i < source.Length; i++) { current.Next = new MyLinkedListNode(source[i]); current = current.Next; } DeleteDuplicatedLinkedNodes.Delete(ref head); Assert.Null(head); }
public void AddAfter(MyLinkedListNode <T> node, MyLinkedListNode <T> newNode) { ThrowArgumentNullExceptionIfParameterIsNull(node); ThrowArgumentNullExceptionIfParameterIsNull(newNode); ThrowInvalidOperationExceptionIfNodeIsAlreadyInList(newNode); if (node.Next is null) { Tail = newNode; } newNode.Previous = node; newNode.Next = node.Next; node.Next = newNode; Count++; }
public void TestAddNullToEmpty() { MyLinkedList l = new MyLinkedList(0); MyLinkedListNode n = null; bool result = true; try { l.AddLast(n); } catch { result = false; } Assert.AreEqual(false, result); }
public void TestWithOneNullList() { MyLinkedListNode list1 = GenerateRandomLinkedList(200, 500); MyLinkedListNode newList = MergeTwoSortedLinkedlist.Merge(list1, null); MyLinkedListNode currentNode = newList; MyLinkedListNode nextNode = newList.Next; while (nextNode != null) { Assert.True(currentNode.Value <= nextNode.Value); currentNode = nextNode; nextNode = nextNode.Next; } Assert.Null(nextNode); }
public void TestDesignerWithSize() { MyLinkedList l = new MyLinkedList(8); MyLinkedListNode n = l.First; bool result = l.Count == 8; while (n != null) { if (n.A != default && n.I != default) { result = false; break; } n = n.Next; } Assert.AreEqual(true, result); }
//This is solution answer public MyLinkedList <int> DeleteMiddleNode(MyLinkedList <int> input, MyLinkedListNode <int> node) { if (input == null) { throw new ArgumentNullException(nameof(input)); } if (node.Next == null) { throw new ArgumentException("This method only can delete middle and not last element"); } node.Value = node.Next.Value; node.Next = node.Next.Next; return(input); }
public void Clear() { var currentNode = Head; Head = null; for (int i = 0; i < Count; i++) { var oldNode = currentNode; currentNode = oldNode.Next; oldNode.Next = null; oldNode.Previous = null; } Tail = null; Count = 0; }
public void Contains_FindsItem(long index, bool isIn) { MyLinkedListNode <string> item = null; if (index >= 0 && index < _listCount) { item = _list[index]; } else if (index >= _listCount) { item = new MyLinkedListNode <string>("notThere"); } var result = _list.Contains(item); Assert.AreEqual(isIn, result); Assert.IsTrue(FilledListOkay); }
public void AddFirst(T t) { MyLinkedListNode n = new MyLinkedListNode(t); if (first != null) { first.previous = n; n.next = first; first = n; } else { first = n; last = n; } Count++; }
public void Remove(MyLinkedListNode <T> node) { var current = Head; for (int i = 0; i < Count; i++) { if (current.Equals(node)) { current.Previous.Next = current.Next; current.Next.Previous = current.Previous; Count--; break; } current = current.Next; } }
public void AddLast(T t) { MyLinkedListNode n = new MyLinkedListNode(t); if (last != null) { last.next = n; n.previous = last; last = n; } else { first = n; last = n; } Count++; }
public void TestRemoveNull() { bool result = true; string[] values = { "a", "b", "c", "d", "e" }; MyLinkedList <string> l = new MyLinkedList <string>(values); MyLinkedListNode <string> n = null; try { l.Remove(n); } catch { result = false; } Assert.AreEqual(false, result); }
private void ElementCount(double element, ref int position, ref int count) { position = -1; count = 0; int i = 0; for (MyLinkedListNode node = headNode; node != null; node = node.nextNode, i++) { if (Math.Abs(node.data - element) < 0.00001) { if (position == -1) { position = i; } count++; } } }
private void SortedInsert(ref MyLinkedListNode newNode, ref MyLinkedListNode sorted) { if (sorted == null || sorted.data >= newNode.data) { newNode.nextNode = sorted; sorted = newNode; } else { MyLinkedListNode current = sorted; while (current.nextNode != null && current.nextNode.data < newNode.data) { current = current.nextNode; } newNode.nextNode = current.nextNode; current.nextNode = newNode; } }
public void TestAddBeforeDoesnotBelong() { string[] values = { "a", "b", "c", "d", "e" }; MyLinkedList <string> l = new MyLinkedList <string>(values); MyLinkedListNode <string> n = new MyLinkedListNode <string>(); bool result = true; try { l.AddBefore(n, "a"); } catch { result = false; } Assert.AreEqual(false, result); }
public static MyLinkedListNode FindEntry(ref MyLinkedListNode head) { if (head == null) { throw new ArgumentNullException(nameof(head), "The linked list head can't be null"); } MyLinkedListNode meetingNode = FindMeetingNode(ref head); if (meetingNode == null) { return(null); } // 从上面返回的相遇节点出发,绕过一周回到相遇节点,经过的节点数即为环上的节点数 int nodeCountInLoop = 1; MyLinkedListNode countingNode = meetingNode; while (countingNode.Next != meetingNode) { nodeCountInLoop++; countingNode = countingNode.Next; } // 仿照查找链表中倒数第k个节点的代码 // 维护两个指针,其中一个先移动相当于环上总节点数的节点,然后两个指针同时移动 // 当第一个指针经过环上一周,并与第二个指针相遇时,两个个指针恰好位于环的入口节点上 MyLinkedListNode rightNode = head; for (int i = 0; i < nodeCountInLoop; i++) { rightNode = rightNode.Next; } MyLinkedListNode leftNode = head; while (rightNode != leftNode)// 两个指针相遇的时候,恰好位于环的入口节点上,此时结束循环 { leftNode = leftNode.Next; rightNode = rightNode.Next; } return(leftNode); }
public override void SwapWithHead(int index) { MyLinkedListNode nodeByIndex = headNode; for (int i = 0; i < index; i++) { nodeByIndex = nodeByIndex.nextNode; } long temporary = headNode.data; headNode.data = nodeByIndex.data; nodeByIndex.data = temporary; long temporary2 = headNode.data2; headNode.data2 = nodeByIndex.data2; nodeByIndex.data2 = temporary2; }
private void InitList(int c, MyLinkedListNode node) { MyLinkedListNode p = new MyLinkedListNode(); node.next = null; p = node; for (int i = 0; i < c; i++) { int a = int.Parse(Console.ReadLine().ToString()); MyLinkedListNode tNode = new MyLinkedListNode(); tNode.data = a; p.next = tNode; p = p.next;//使当前节点指向下一个节点 /*tNode.next = p.next; * p.next = tNode;*/ } p.next = null; }
private void VerifyJumpOrders(List<int> expectedOrders, MyLinkedListNode<string>[] nodes) { for (int i = 0; i < nodes.Length; i++) { Assert.AreEqual(nodes[i].JumpOrder, expectedOrders[i]); } }