//method2, keep 2 pointers, don't keep original order, O(N) time, O(1) space static LinkListNode partition2(LinkListNode node, int d) { LinkListNode head = node; LinkListNode tail = node; while (node != null) { LinkListNode next = node.next; if (node.data < d) { node.next = head; head = node; } else { tail.next = node; tail = node; } node = next; } tail.next = null; //otherwise stack overflow exception return head; }
public void a_single_node_can_point_to_itself() { var node = new LinkListNode<string>("node"); node.AddNext(node); node.Next.ShouldBe(node); }
public void node_can_point_to_the_next_node() { var first = new LinkListNode<string>("first"); var second = new LinkListNode<string>("second"); first.AddNext(second); first.Next.ShouldBe(second); }
public void a_node_that_points_to_itself_is_cyclic() { var node = new LinkListNode<string>("node"); node.AddNext(node); node.IsCyclic().ShouldBe(true); }
public void two_nodes_can_have_circular_pointers() { var first = new LinkListNode<string>("first"); var second = new LinkListNode<string>("second"); first.AddNext(second) .AddNext(first); first.Next.Next.ShouldBe(first); second.Next.Next.ShouldBe(second); }
public void chain_of_nodes_can_point_to_an_inner_node() { var first = new LinkListNode<string>("first"); var second = new LinkListNode<string>("second"); var third = new LinkListNode<string>("third"); first.AddNext(second) .AddNext(third) .AddNext(second); first.Next.Next.Next.ShouldBe(second); }
/// <summary> /// 出栈 /// </summary> /// <returns></returns> public T Pop() { if (_top == null) { return(default(T)); } var result = _top.Data; _top = _top.Next; Length--; return(result); }
/// <summary> /// 入栈 /// </summary> /// <param name="item"></param> public void Push(T item) { if (_top == null) { _top = new LinkListNode <T>(item); Length++; return; } var newNode = new LinkListNode <T>(item, _top); _top = newNode; Length++; }
//Hash table of all nodes //////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> Node hast table. </summary> /// /// <remarks> Galarist, 18/09/2018. </remarks> /// /// <param name="nodeList"> List of nodes. </param> /// /// <returns> A Hashtable. </returns> //////////////////////////////////////////////////////////////////////////////////////////////////// public static Hashtable NodeHastTable(LinkListNodeList nodeList) { //returns a hash table containing all the nodes Hashtable tempTable = new Hashtable(); int count = 1; //loop over the link list and add all the nodes to the hash table for (LinkListNode i = nodeList.CurrentNode; i.GetNext() != null; i = i.GetNext()) { tempTable.Add(count.ToString(), i); count++; } return(tempTable); }
//method 1, make a small list and large list, then merge them (small.next = large.head). O(N) time, O(1) space static LinkListNode partition(LinkListNode head, int d) { LinkListNode s = null; LinkListNode sHead = null; LinkListNode l = null; LinkListNode lhead = null; LinkListNode n = head; while (n != null) { LinkListNode next = n.next; n.next = null; //otherwise stack overflow exception if (n.data < d) { if (s == null) { sHead = n; s = sHead; } else { s.next = n; s = n; } } else { if (l == null) { lhead = n; l = lhead; } else { l.next = n; l = n; } } n = next; } if (s == null) return lhead; else { s.next = lhead; return sHead; } }
private int FindNthFromtheLast(LinkListNode <int> node) { if (node == null) { return(0); } int nthItem = FindNthFromtheLast(node.Next) + 1; if (nthItem == 5) { Console.WriteLine($"{node.Value} is the 5th."); } return(nthItem); }
//Partition: Write code to partition a linked list around value x, such that all nodes less than x come before all nodes greater than or equal to x. //If x is contained within the list, the values of x only need to be after the elements less than x. static void Main(string[] args) { int[] intArray = new int[]{ 3, 5, 8, 5, 10, 2, 1 }; LinkListNode first = new LinkListNode(intArray[0]); LinkListNode head = first; for (int i = 1; i < intArray.Length; i++) { LinkListNode second = new LinkListNode(intArray[i]); first.next = second; first = second; } Console.WriteLine(head.printForward()); LinkListNode newHead = partition2(head, 5); Console.WriteLine(newHead.printForward()); }
private void buildList() { for (int index = 1; index <= size; index++) { if (current == null) { head = new LinkListNode<string>(index.ToString()); current = head; } else { current = current.AddNext(index.ToString()); } if (index == targetIndex) target = current; } }
public void IsPalindromeTest() { var root = new LinkListNode(1) { Next = new LinkListNode(2) { Next = new LinkListNode(3) { Next = new LinkListNode(2) { Next = new LinkListNode(1) } } } }; Assert.That(IsPalindrome(root), Is.EqualTo(true)); }
public void SumListsTest() { var root1 = new LinkListNode(7) { Next = new LinkListNode(1) { Next = new LinkListNode(6) } }; var root2 = new LinkListNode(5) { Next = new LinkListNode(9) { Next = new LinkListNode(2) } }; Assert.That(SumLists(root1, root2), Is.EqualTo(912)); }
/// <summary> /// 入队 /// </summary> public void EnQueue(T item) { if (Length == 0) { // 队列为空 // 新创建的节点直接作为队首节点 // 并将队尾指向队首 _front = new LinkListNode <T>(item); _rear = _front; Length++; return; } // 将新节点添加当前队尾后面 // 并将新节点做为新的队尾 var newNode = new LinkListNode <T>(item); _rear.Next = newNode; _rear = newNode; Length++; }
public void ReturnLastTest() { var root = new LinkListNode(1) { Next = new LinkListNode(2) { Next = new LinkListNode(3) { Next = new LinkListNode(1) { Next = new LinkListNode(2) { Next = new LinkListNode(4) } } } } }; Assert.That(ReturnLast(root, 4), Is.EqualTo(3)); }
public void LoopDetectionTest() { var root1 = new LinkListNode(1) { Next = new LinkListNode(2) { Next = new LinkListNode(3) { Next = new LinkListNode(4) { Next = new LinkListNode(3) { Next = new LinkListNode(4) } } } } }; Assert.That(LoopDetection(root1), Is.EqualTo(3)); }
public void PartitionTest() { var root = new LinkListNode(4) { Next = new LinkListNode(6) { Next = new LinkListNode(1) { Next = new LinkListNode(3) { Next = new LinkListNode(2) { Next = new LinkListNode(5) } } } } }; var expected = new LinkListNode(1) { Next = new LinkListNode(2) { Next = new LinkListNode(4) { Next = new LinkListNode(6) { Next = new LinkListNode(3) { Next = new LinkListNode(5) } } } } }; var actual = Partition(root, 3); Assert.That(actual, Is.EqualTo(expected)); }
public void IntersectionNodeTest() { var root1 = new LinkListNode(7) { Next = new LinkListNode(1) { Next = new LinkListNode(6) } }; var root2 = new LinkListNode(5) { Next = new LinkListNode(9) { Next = new LinkListNode(2) { Next = root1 } } }; Assert.That(IntersectionNode(root1, root2), Is.EqualTo(root1)); }
/// <summary> /// 出队 /// </summary> /// <returns></returns> public T DeQueue() { if (Length == 0) { return(default(T)); } var result = _front.Data; if (Length == 1) { _front = null; _rear = null; } else { _front = _front.Next; } Length--; return(result); }
public void DeleteTheMiddleTest() { var root = new LinkListNode(1) { Next = new LinkListNode(2) { Next = new LinkListNode(3) { Next = new LinkListNode(4) { Next = new LinkListNode(5) { Next = new LinkListNode(6) } } } } }; var expected = new LinkListNode(1) { Next = new LinkListNode(2) { Next = new LinkListNode(3) { Next = new LinkListNode(5) { Next = new LinkListNode(6) } } } }; DeleteTheMiddle(root); Assert.That(root, Is.EqualTo(expected)); }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> Searches for the first node dictionary. </summary> /// /// <remarks> Galarist, 18/09/2018. </remarks> /// /// <param name="nodeHashtable"> The node hashtable. </param> /// <param name="searchResult"> The search result. </param> /// /// <returns> The found node dictionary. </returns> //////////////////////////////////////////////////////////////////////////////////////////////////// public static LinkListNode SearchNodeDict(Hashtable nodeHashtable, LinkListNode searchResult) { //uses linq to search through the hastable for a node looking at the result return((LinkListNode)nodeHashtable.Values.OfType <LinkListNode>().Where(x => x == searchResult)); }
public LinkListNode() { Data = default(T); Next = null; }
//数据域,当前Node Data /// <summary> /// 指针域:头结点 /// </summary> /// <param name="p"></param> public LinkListNode(LinkListNode <T> p) { this.Next = p; }
/// <summary> /// 数据域+指针域:普通Node /// </summary> /// <param name="item"></param> /// <param name="p"></param> public LinkListNode(T item, LinkListNode <T> p) { this.Data = item; this.Next = p; }
public void can_add_next_by_value() { var node = new LinkListNode<string>("first"); node.AddNext("second"); }
public void node_can_have_a_value() { var node = new LinkListNode<string>("node"); node.Value.ShouldBe("node"); }
private void ReceiveCallback(IAsyncResult AR) { try { // Socket exception will raise here when client closes int received = clientSocket.EndReceive(AR); if (received == 0) { return; } // The received data is deserialized in the EquationProperties. string message = Encoding.ASCII.GetString(buffer); // Any excess characters are stipped of the end int index = message.IndexOf("}"); message = message.Substring(0, index + 1); // Deserialize the json string into the equation object equation = JsonConvert.DeserializeObject <Equations>(message); // Create new node and add to nodelist Invoke((Action) delegate { LinkListNode node = new LinkListNode(equation); equationNodeList.AddEquationNode(node); }); //add the node to the binary tree if (tree.root == null) { //if tree is emtpy, set the node as the root tree.root = new BinaryTreeNode(equation); } else { //add the node to the bindary tree tree.Add(equation); } Invoke((Action) delegate { rtbAsked.Clear(); //the default print order for the binary tree is in-order //rtbAsked.Text = BinaryTree.PrintInOrder(tree); SendBtn.Enabled = true; }); // Check if answer is incorrect if (equation.IsCorrect == false) { Invoke((Action) delegate { if (equation.IsCorrect == false) { rtbWrong.Text = InstructorController.PrintLinkList(equationNodeList); } }); } // Start receiving data again. clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, null); } // Avoid catching all exceptions handling in cases like these. catch (SocketException ex) { ShowErrorDialog(ex.Message); Invoke((Action) delegate { //lbl_clientCount.Text = "No connected clients"; }); } catch (ObjectDisposedException ex) { ShowErrorDialog(ex.Message); } }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> Sets a next. </summary> /// /// <remarks> Galarist, 18/09/2018. </remarks> /// /// <param name="aNode"> The node. </param> //////////////////////////////////////////////////////////////////////////////////////////////////// public void SetNext(LinkListNode aNode) { this.next = aNode; }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> Sets the previous. </summary> /// /// <remarks> Galarist, 18/09/2018. </remarks> /// /// <param name="aNode"> The node. </param> //////////////////////////////////////////////////////////////////////////////////////////////////// public void SetPrevious(LinkListNode aNode) { this.previous = aNode; }
public LinkListNode(LinkListNode <T> next) { Next = next; }
public static void TaskList() { string[] words = { "the", "fox", "jumped", "over", "the", "dog" }; LinkList <string> list = new LinkList <string>(words); Console.WriteLine("The linked list values:\n" + list + "\n"); Console.WriteLine($"sentence.Contains(\"jumped\") = {list.Contains("jumped")}\n"); list.AddFirst("today"); Console.WriteLine("Test 1: Add 'today' to beginning of the list:\n" + list + "\n"); LinkListNode <string> mark1 = list.First; list.RemoveFirst(); list.AddLast(mark1); Console.WriteLine("Test 2: Move first node to be last node:\n" + list + "\n"); list.RemoveLast(); list.AddLast("yesterday"); Console.WriteLine("Test 3: Change the last node to 'yesterday':\n" + list + "\n"); mark1 = list.Last; list.RemoveLast(); list.AddFirst(mark1); Console.WriteLine("Test 4: Move last node to be first node:\n" + list + "\n"); list.RemoveFirst(); LinkListNode <string> current = list.FindLast("the"); list.AddAfter(current, "old"); list.AddAfter(current, "lazy"); current = list.Find("fox"); list.AddBefore(current, "quick"); list.AddBefore(current, "brown"); mark1 = current; LinkListNode <string> mark2 = current.Previous; current = list.Find("dog"); list.Remove(mark1); list.AddBefore(current, mark1); list.Remove(current); list.AddAfter(mark2, current); list.Remove("old"); Console.WriteLine("Test 13: Remove node that has the value 'old':\n" + list + "\n"); list.RemoveLast(); ICollection <string> icoll = list; icoll.Add("rhinoceros"); Console.WriteLine("Test 14: Remove last node, cast to ICollection, and add 'rhinoceros':\n" + list + "\n"); Console.WriteLine("Test 15: Copy the list to an array:"); string[] sArray = new string[list.Count]; list.CopyTo(sArray, 0); Console.WriteLine(sArray.GetString()); list.Clear(); Console.WriteLine($"\nTest 16: Clear linked list. Contains 'jumped' = {list.Contains("jumped")}"); Console.WriteLine("\n" + new string('-', Console.WindowWidth)); }
public static void Task4() { // Create the link list. string[] words = { "the", "fox", "jumped", "over", "the", "dog" }; LinkList <string> list = new LinkList <string>(words); Console.WriteLine("The linked list values:\n" + list.ToString()); Console.WriteLine($"sentence.Contains(\"jumped\") = {list.Contains("jumped")}\n"); // Add the word 'today' to the beginning of the linked list. list.AddFirst("today"); Console.WriteLine("Test 1: Add 'today' to beginning of the list:\n" + list.ToString() + "\n"); // Move the first node to be the last node. LinkListNode <string> mark1 = list.First; list.RemoveFirst(); list.AddLast(mark1); Console.WriteLine("Test 2: Move first node to be last node:\n" + list.ToString() + "\n"); // Change the last node be 'yesterday'. list.RemoveLast(); list.AddLast("yesterday"); Console.WriteLine("Test 3: Change the last node to 'yesterday':\n" + list.ToString() + "\n"); // Move the last node to be the first node. mark1 = list.Last; list.RemoveLast(); list.AddFirst(mark1); Console.WriteLine("Test 4: Move last node to be first node:\n" + list.ToString() + "\n"); // Indicate, by using parentheisis, the last occurence of 'the'. list.RemoveFirst(); LinkListNode <string> current = list.FindLast("the"); IndicateNode(current, "Test 5: Indicate last occurence of 'the':"); // Add 'lazy' and 'old' after 'the' (the LinkListNode named current). list.AddAfter(current, "old"); list.AddAfter(current, "lazy"); IndicateNode(current, "Test 6: Add 'lazy' and 'old' after 'the':"); // Indicate 'fox' node. current = list.Find("fox"); IndicateNode(current, "Test 7: Indicate the 'fox' node:"); // Add 'quick' and 'brown' before 'fox': list.AddBefore(current, "quick"); list.AddBefore(current, "brown"); IndicateNode(current, "Test 8: Add 'quick' and 'brown' before 'fox':"); // Keep a reference to the current node, 'fox', // and to the previous node in the list. Indicate the 'dog' node. mark1 = current; LinkListNode <string> mark2 = current.Previous; current = list.Find("dog"); IndicateNode(current, "Test 9: Indicate the 'dog' node:"); list.Remove(mark1); list.AddBefore(current, mark1); IndicateNode(current, "Test 10: Move a referenced node (fox) before the current node (dog):"); list.Remove(current); IndicateNode(current, "Test 11: Remove current node (dog) and attempt to indicate it:"); list.AddAfter(mark2, current); IndicateNode(current, "Test 12: Add node removed in test 11 after a referenced node (brown):"); list.Remove("old"); Console.WriteLine("Test 13: Remove node that has the value 'old':\n" + list.ToString() + "\n"); list.RemoveLast(); ICollection <string> icoll = list; icoll.Add("rhinoceros"); Console.WriteLine("Test 14: Remove last node, cast to ICollection, and add 'rhinoceros':\n" + list.ToString() + "\n"); Console.WriteLine("Test 15: Copy the list to an array:"); string[] sArray = new string[list.Count]; list.CopyTo(sArray, 0); Console.WriteLine(sArray.GetString()); list.Clear(); Console.WriteLine($"\nTest 16: Clear linked list. Contains 'jumped' = {list.Contains("jumped")}"); Console.ReadLine(); }
public void node_can_be_null_terminated() { var node = new LinkListNode<string>("node"); node.Next.ShouldBe(null); }
public void a_single_node_is_not_cyclic() { var node = new LinkListNode<string>("node"); node.IsCyclic().ShouldBe(false); }
public LinkListNode(T data, LinkListNode <T> next) { Data = data; Next = next; }