private void Button2__Click(object sender, EventArgs e) { // 1. using Button1__Click() create a LinkedList which contains the digits 1 through 10 LinkedList <object> linkedList = new LinkedList <object>(); // 2. Your code here for (int i = 1; i <= 10; i++) { //add element to end of list linkedList.AddLast(i); } // 3. using example #18, copy the linkedList to reverseLinkedList in reverse order // so that reverseLinkedList goes from 10 to 1 LinkedList <object> reverseLinkedList = new LinkedList <object>(); LinkedListNode <object> linkedListNode; // 4. Your code here linkedListNode = linkedList.Last; while (linkedListNode != null) { reverseLinkedList.AddLast(linkedListNode.Value); linkedListNode = linkedListNode.Previous; } // 5. then call the visualizer VisualizeLinkedList visualizeLinkedList = new VisualizeLinkedList(reverseLinkedList); }
private void Button4__Click(object sender, EventArgs e) { // 1. create a LinkedList which contains the words // Because I'm sad Clap along if you feel like a room without a roof // Because I'm sad Clap along if you feel like sadness is the truth sad LinkedList <object> linkedList = null; LinkedListNode <object> linkedListNode; string[] s = null; // 2. Your code here // 3. replace "sad" with "happy" // and "sadness with "happiness" // note that because Value is an object // you will have to cast Value as a string as follows: // if( (string)linkedListNode.Value == "sad" // 4. Your code here // 5. then call the visualizer VisualizeLinkedList visualizeLinkedList = new VisualizeLinkedList(linkedList); }
private void Button6__Click(object sender, EventArgs e) { LinkedListNode <object> linkedListNode1; LinkedListNode <object> linkedListNode2; string[] letters = { "D", "O", "R", "M", "I", "T", "O", "R", "Y" }; LinkedList <object> anagram = new LinkedList <object>(letters); // rearrange the Nodes to spell "DIRTYROOM" // your Add methods must use 2 LinkedListNode arguments like examples #13 and #14 // you may not use string arguments in your Add method calls // Your code here linkedListNode1 = anagram.Find("I"); linkedListNode2 = anagram.Find("D"); anagram.Remove(linkedListNode1); anagram.AddAfter(linkedListNode2, linkedListNode1); //currently have D I O R M I T O R Y linkedListNode1 = anagram.Find("I"); linkedListNode2 = anagram.Find("R"); anagram.Remove(linkedListNode2); anagram.AddAfter(linkedListNode1, linkedListNode2); //diromtory linkedListNode1 = anagram.Find("T"); linkedListNode2 = anagram.Find("O"); anagram.Remove(linkedListNode1); anagram.AddBefore(linkedListNode2, linkedListNode1); //dirtomory linkedListNode1 = anagram.Find("Y"); linkedListNode2 = anagram.Find("T"); anagram.Remove(linkedListNode1); anagram.AddAfter(linkedListNode2, linkedListNode1); //dirtyomor linkedListNode1 = anagram.FindLast("R"); linkedListNode2 = anagram.Find("Y"); anagram.Remove(linkedListNode1); anagram.AddAfter(linkedListNode2, linkedListNode1); //dirtyromo linkedListNode1 = anagram.FindLast("O"); linkedListNode2 = anagram.Find("M"); anagram.Remove(linkedListNode2); anagram.AddAfter(linkedListNode1, linkedListNode2); //dirtyroom // then call the visualizer VisualizeLinkedList visualizeLinkedList = new VisualizeLinkedList(anagram); }
/* Linked List Examples * * 1. Create a Linked List of strings * LinkedList<string> sentence = new LinkedList<string>(); * * 2. Create a Linked List from an array of strings * string[] words = * { "the", "red", "car", "speeds", "away" }; * LinkedList<string> sentence = new LinkedList<string>(words); * * 3. Add new strings to the end of the Linked List * sentence.AddLast("word"); * * 4. Add the word 'today' to the beginning of the linked list. * sentence.AddFirst("today"); * * 5. Move the first node to be the last node. * LinkedListNode<string> firstNode = sentence.First; * sentence.RemoveFirst(); * sentence.AddLast(firstNode); * * 6. Change the last node to 'yesterday' * sentence.RemoveLast(); * sentence.AddLast("yesterday"); * * 7. Move the last node to be the first node. * LinkedListNode<string> lastNode = sentence.Last; * sentence.RemoveLast(); * sentence.AddFirst(lastNode); * * 8. Find the last occurence of 'the'. * LinkedListNode<string> target = sentence.FindLast("the"); * if (target == null) * { * // "the" is not found * } * else * { * // Add 'bright' and 'red' after 'the' (the LinkedListNode named target). * sentence.AddAfter(target, "bright"); * sentence.AddAfter(target, "red"); * } * * 9. Find the 'car' node * LinkedListNode<string> target = sentence.Find("car"); * * 10. Add 'sporty' and 'red' before 'car': * sentence.AddBefore(target, "sporty"); * sentence.AddBefore(target, "red"); * * 11. Keep a reference to the 'car' node * and to the previous node in the list * carNode = sentence.Find("car"); * LinkedListNode<string> current = carNode.Previous; * * 12. The AddBefore method throws an InvalidOperationException * if you try to add a node that already belongs to a list. * try * { * // try to add carNode before current * sentence.AddBefore(current, carNode); * } * catch (InvalidOperationException ex) * { * Console.WriteLine("Exception message: {0}", ex.Message); * } * * * 13. Remove the node referred to by carNode, and then add it * before the node referred to by current. * sentence.Remove(carNode); * sentence.AddBefore(current, carNode); * * * 14. Add the 'current' node after the node referred to by mark2 * sentence.AddAfter(mark2, current); * * 15. The Remove method finds and removes the * first node that that has the specified value. * sentence.Remove("red"); * * 16. Create an array with the same number of * elements as the linked list. * string[] sArray = new string[sentence.Count]; * sentence.CopyTo(sArray, 0); * * 17. Walk through a Linked List in forward order * LinkedListNode<object> linkedListNode = linkedList.First; * * while( linkedListNode != null ) * { * linkedListNode = linkedListNode.Next; * } * * 18. Walk through a Linked List in reverse order * LinkedListNode<object> linkedListNode = linkedList.Last; * * while( linkedListNode != null ) * { * linkedListNode = linkedListNode.Previous; * } * * 19. Change the Value of a node * linkedListNode.Value = "new value"; * * 20. Release all the nodes. * sentence.Clear(); * */ private void Button1__Click(object sender, EventArgs e) { // 1. create a LinkedList which contains the digits 1 through 10 LinkedList <object> linkedList = new LinkedList <object>(); // 2. Your code here // 3. then call the visualizer VisualizeLinkedList visualizeLinkedList = new VisualizeLinkedList(linkedList); }
/* Linked List Examples * * 1. Create a Linked List of strings * LinkedList<string> sentence = new LinkedList<string>(); * * 2. Create a Linked List from an array of strings * string[] words = * { "the", "red", "car", "speeds", "away" }; * LinkedList<string> sentence = new LinkedList<string>(words); * * 3. Add new strings to the end of the Linked List * sentence.AddLast("word"); * * 4. Add the word 'today' to the beginning of the linked list. * sentence.AddFirst("today"); * * 5. Move the first node to be the last node. * LinkedListNode<string> firstNode = sentence.First; * sentence.RemoveFirst(); * sentence.AddLast(firstNode); * * 6. Change the last node to 'yesterday' * sentence.RemoveLast(); * sentence.AddLast("yesterday"); * * 7. Move the last node to be the first node. * LinkedListNode<string> lastNode = sentence.Last; * sentence.RemoveLast(); * sentence.AddFirst(lastNode); * * 8. Find the last occurence of 'the'. * LinkedListNode<string> target = sentence.FindLast("the"); * if (target == null) * { * // "the" is not found * } * else * { * // Add 'bright' and 'red' after 'the' (the LinkedListNode named target). * sentence.AddAfter(target, "bright"); * sentence.AddAfter(target, "red"); * } * * 9. Find the 'car' node * LinkedListNode<string> target = sentence.Find("car"); * * 10. Add 'sporty' and 'red' before 'car': * sentence.AddBefore(target, "sporty"); * sentence.AddBefore(target, "red"); * * 11. Keep a reference to the 'car' node * and to the previous node in the list * carNode = sentence.Find("car"); * LinkedListNode<string> current = carNode.Previous; * * 12. The AddBefore method throws an InvalidOperationException * if you try to add a node that already belongs to a list. * try * { * // try to add carNode before current * sentence.AddBefore(current, carNode); * } * catch (InvalidOperationException ex) * { * Console.WriteLine("Exception message: {0}", ex.Message); * } * * * 13. Remove the node referred to by carNode, and then add it * before the node referred to by current. * sentence.Remove(carNode); * sentence.AddBefore(current, carNode); * * * 14. Add the 'current' node after the node referred to by mark2 * sentence.AddAfter(mark2, current); * * 15. The Remove method finds and removes the * first node that that has the specified value. * sentence.Remove("red"); * * 16. Create an array with the same number of * elements as the linked list. * string[] sArray = new string[sentence.Count]; * sentence.CopyTo(sArray, 0); * * 17. Walk through a Linked List in forward order * LinkedListNode<object> linkedListNode = linkedList.First; * * while( linkedListNode != null ) * { * linkedListNode = linkedListNode.Next; * } * * 18. Walk through a Linked List in reverse order * LinkedListNode<object> linkedListNode = linkedList.Last; * * while( linkedListNode != null ) * { * linkedListNode = linkedListNode.Previous; * } * * 19. Change the Value of a node * linkedListNode.Value = "new value"; * * 20. Release all the nodes. * sentence.Clear(); * */ private void Button1__Click(object sender, EventArgs e) { // 1. create a LinkedList which contains the digits 1 through 10 LinkedList <object> linkedList = new LinkedList <object>(); // 2. Your code here for (int i = 1; i <= 10; i++) { //add element to end of list linkedList.AddLast(i); } string[] words = { "the", "car", "speeds", "away" }; LinkedList <object> sentence = new LinkedList <object>(words); //sentence.AddLast("today"); sentence.AddFirst("today"); LinkedListNode <object> firstNode = sentence.First; sentence.RemoveFirst(); sentence.AddLast(firstNode); //after the, we want it to say bright red LinkedListNode <object> target = sentence.FindLast("the"); if (target == null) { // "the" is not found } else { // Add 'bright' and 'red' after 'the' (the LinkedListNode named target). sentence.AddAfter(target, "red"); sentence.AddAfter(target, "bright"); } target = sentence.Find("car"); //10.Add 'sporty' and 'red' before 'car': sentence.AddBefore(target, "sporty"); sentence.AddBefore(target, "red"); // 3. then call the visualizer VisualizeLinkedList visualizeLinkedList = new VisualizeLinkedList(sentence); }
private void Button5__Click(object sender, EventArgs e) { // 1. create a LinkedList which contains the following words // The Spain in rain falls plain on the mainly LinkedList <object> linkedList = null; LinkedListNode <object> linkedListNode1; LinkedListNode <object> linkedListNode2; string[] s = null; // 2. Your code here string[] words = { "The", "Spain", "in", "rain", "falls", "plain", "on", "the", "mainly" }; linkedList = new LinkedList <object>(words); // 3. manipulate the list using Find(), Remove(), AddBefore() and/or AddAfter() to result in // "The rain in Spain falls mainly on the plain" // your Add methods must use 2 LinkedListNode arguments like examples #13 and #14 // you may not use string arguments in your Add method calls //add rain and in before spain and remove rain linkedListNode1 = linkedList.Find("Spain"); linkedListNode2 = linkedList.Find("rain"); linkedList.Remove(linkedListNode2); linkedList.AddBefore(linkedListNode1, linkedListNode2); linkedListNode2 = linkedList.Find("in"); linkedList.Remove(linkedListNode2); linkedList.AddBefore(linkedListNode1, linkedListNode2); //swap mainly and plain linkedListNode1 = linkedList.Find("the"); linkedListNode2 = linkedList.Find("plain"); linkedList.Remove(linkedListNode2); linkedList.AddAfter(linkedListNode1, linkedListNode2); linkedListNode1 = linkedList.Find("on"); linkedListNode2 = linkedList.Find("mainly"); linkedList.Remove(linkedListNode2); linkedList.AddBefore(linkedListNode1, linkedListNode2); // 4. Your code here // 5. then call the visualizer VisualizeLinkedList visualizeLinkedList = new VisualizeLinkedList(linkedList); }
private void Button4__Click(object sender, EventArgs e) { // 1. create a LinkedList which contains the words // Because I'm sad Clap along if you feel like a room without a roof // Because I'm sad Clap along if you feel like sadness is the truth sad LinkedList <object> linkedList = null; LinkedListNode <object> linkedListNode; string[] s = null; // 2. Your code here // 3. replace "sad" with "happy" // and "sadness with "happiness" // note that because Value is an object // you will have to cast Value as a string as follows: // if( (string)linkedListNode.Value == "sad" string[] words = { "Because", "I'm", "sad", "Clap", "along", "if", "you", "feel", "like", "a", "room", "without", "a", "roof","Because","I'm", "sad", "Clap", "along", "if", "you", "feel", "like", "sadness", "is", "the", "truth", "sad" }; // 4. Your code here linkedList = new LinkedList <object>(words); //linkedListNode = linkedList.Find("sad"); LinkedListNode <object> sadness = linkedList.Find("sadness"); for (int i = 0; i < 3; i++) { linkedListNode = linkedList.Find("sad"); if ((string)linkedListNode.Value == "sad") { linkedListNode.Value = "happy"; } else { } } if ((string)sadness.Value == "sadness") { sadness.Value = "hapiness"; } // 5. then call the visualizer VisualizeLinkedList visualizeLinkedList = new VisualizeLinkedList(linkedList); }
private void Button3__Click(object sender, EventArgs e) { // 1. create a LinkedList which contains the words // "the", "fox", "jumped", "over", "the", "dog" LinkedList <object> linkedList = null; LinkedListNode <object> linkedListNode; string[] sentence = null; // 2. Your code here // 3. add "quick" and "brown" before "fox" string[] words = { "the", "fox", "jumped", "over", "the", "dog" }; linkedList = new LinkedList <object>(words); linkedListNode = linkedList.Find("fox"); if (linkedListNode == null) { // "fox" is not found } else { // Add add "quick" and "brown" before "fox" (the LinkedListNode named target). linkedList.AddBefore(linkedListNode, "quick"); linkedList.AddBefore(linkedListNode, "brown"); } // 4. Your code here // 5. using example #8, add "lazy" after the last "the" linkedListNode = linkedList.FindLast("the"); if (linkedListNode == null) { // "the" is not found } else { // Add lazy after 'the' (the LinkedListNode named target). linkedList.AddAfter(linkedListNode, "lazy"); } // 6. Your code here // 7. then call the visualizer VisualizeLinkedList visualizeLinkedList = new VisualizeLinkedList(linkedList); }
private void Button6__Click(object sender, EventArgs e) { LinkedListNode <object> linkedListNode1; LinkedListNode <object> linkedListNode2; string[] letters = { "D", "O", "R", "M", "I", "T", "O", "R", "Y" }; LinkedList <object> anagram = new LinkedList <object>(letters); // rearrange the Nodes to spell "DIRTYROOM" // your Add methods must use 2 LinkedListNode arguments like examples #13 and #14 // you may not use string arguments in your Add method calls // Your code here // then call the visualizer VisualizeLinkedList visualizeLinkedList = new VisualizeLinkedList(anagram); }
private void Button2__Click(object sender, EventArgs e) { // 1. using Button1__Click() create a LinkedList which contains the digits 1 through 10 LinkedList <object> linkedList = new LinkedList <object>(); // 2. Your code here // 3. using example #18, copy the linkedList to reverseLinkedList in reverse order // so that reverseLinkedList goes from 10 to 1 LinkedList <object> reverseLinkedList = new LinkedList <object>(); LinkedListNode <object> linkedListNode; // 4. Your code here // 5. then call the visualizer VisualizeLinkedList visualizeLinkedList = new VisualizeLinkedList(reverseLinkedList); }
private void Button3__Click(object sender, EventArgs e) { // 1. create a LinkedList which contains the words // "the", "fox", "jumped", "over", "the", "dog" LinkedList <object> linkedList = null; LinkedListNode <object> linkedListNode; string[] sentence = null; // 2. Your code here // 3. add "quick" and "brown" before "fox" // 4. Your code here // 5. using example #8, add "lazy" after the last "the" // 6. Your code here // 7. then call the visualizer VisualizeLinkedList visualizeLinkedList = new VisualizeLinkedList(linkedList); }
private void Button5__Click(object sender, EventArgs e) { // 1. create a LinkedList which contains the following words // The Spain in rain falls plain on the mainly LinkedList <object> linkedList = null; LinkedListNode <object> linkedListNode1; LinkedListNode <object> linkedListNode2; string[] s = null; // 2. Your code here // 3. manipulate the list using Find(), Remove(), AddBefore() and/or AddAfter() to result in // "The rain in Spain falls mainly on the plain" // your Add methods must use 2 LinkedListNode arguments like examples #13 and #14 // you may not use string arguments in your Add method calls // 4. Your code here // 5. then call the visualizer VisualizeLinkedList visualizeLinkedList = new VisualizeLinkedList(linkedList); }