public void PrintTest(int value) { LList list = new LList(new Node(7)); list.Add(new Node(value)); Assert.True(list.Print() == true); }
public static LList Merge(LList list1, LList list2) { list1.Current = list1.Head; list2.Current = list2.Head; Node pointer1 = list1.Current; Node pointer2 = list2.Current; while (list1.Current != null || list2.Current != null) { pointer1 = pointer1.Next; pointer2 = pointer2.Next; if (pointer1 == null) { list1.Current.Next = list2.Current; return(list1); } if (pointer2 == null) { list1.Current.Next = list2.Current; list2.Current.Next = pointer1; return(list1); } list1.Current.Next = list2.Current; list1.Current = pointer1; list2.Current.Next = pointer1; list2.Current = pointer2; } return(list1); }
/// <summary> /// Implementation of Eeney Meeney Miney Moe /// </summary> /// <param name="inputList">List of parties to play</param> /// <param name="number">Which number goes out</param> /// <returns>The surviour</returns> public static string EeneyMeeneyMineyMoe(LList inputList, int number) { if (number == 0) { return(string.Empty); } Node _curr = inputList.Head; Node _prev = inputList.Head; int i = 1; // there must be only one to stop iterating while (_curr != _curr.Next) { if (i == number) { // current goes out _prev.Next = _curr.Next; _curr = _curr.Next; i = 1; } else { _prev = _curr; _curr = _curr.Next; i += 1; } } return((string)_curr.Value); }
static void Main(string[] args) { LList llist = new LList(); llist.Append("Eeney"); llist.Append("Meeney"); llist.Append("Miney"); llist.Append("Moe"); Console.WriteLine("Initial list:"); llist.Print(); string result = EeneyMeeneyMineyMoe(llist, 3); Console.WriteLine($"The winner is {result}"); LList llist1 = new LList(); string[] array = Enumerable.Range(0, 40000).Select(i => "Num" + i).ToArray(); foreach (string item in array) { llist1.Append(item); } llist1.Print(); string result1 = EeneyMeeneyMineyMoe(llist1, 30000); Console.WriteLine($"The winner is {result1}"); Console.ReadLine(); }
public void CanInsertAfterInMiddle() { // Arrange int value = 0; LList <int> list = new LList <int>(); list.Insert(15); list.Insert(8); list.Insert(4); // Act list.InsertAfter(8, 1); while (list.Current.Next != null) { if (list.Current.Value == 8) { value = list.Current.Next.Value; } list.Current = list.Current.Next; } // Assert Assert.Equal(1, value); }
private void Parse() { if (this.children != null) { return; } Resolve(); LList list = this.parsed_rlp == null?RLP.DecodeLazyList(this.rlp) : this.parsed_rlp; if (list != null && list.Count == 2) { this.children = new object[2]; TrieKey key = TrieKey.FromPacked(list.GetBytes(0)); this.children[0] = key; if (key.IsTerminal) { this.children[1] = list.GetBytes(1); } else { this.children[1] = list.IsList(1) ? new TrieNode(this.reference, list.GetList(1)) : new TrieNode(this.reference, list.GetBytes(1)); } } else { this.children = new object[17]; this.parsed_rlp = list; } }
public static LList Merge(LList one, LList two) { Node dummListStart = new Node(1); Node tail = dummListStart; one.Current = one.Head; two.Current = two.Head; while (true) { if (one.Current == null) { tail.Next = two.Current; break; } else if (two.Current == null) { tail.Next = one.Current; break; } else { tail.Next = one.Current; tail = one.Current; one.Current = one.Current.Next; tail.Next = two.Current; tail = two.Current; two.Current = two.Current.Next; } } LList mergedList = new LList(dummListStart.Next); return(mergedList); }
public void AddTest(int val) { LList ll = new LList(new Node(1)); ll.Add(new Node(val)); Assert.True((int)ll.Head.Value == val); }
public void AppendToEmptyList() { LList lL = new LList(); lL.Append(1); Assert.Equal(1, lL.Head.Value); }
public void AddTest(int value) { LList list = new LList(new Node(7)); list.Add(new Node(value)); Assert.True((int)list.Head.Value == value); }
// Main - entry point static void Main(string[] args) { LinkedList <int> myLList = new LinkedList <int>(); LinkedListNode <int> current; myLList.AddLast(1); myLList.AddLast(4); myLList.AddLast(9); myLList.AddLast(16); myLList.AddLast(25); myLList.AddLast(36); myLList.AddLast(49); current = myLList.First; while (current != null) { Console.WriteLine(current.Value); current = current.Next; } Console.WriteLine("------------------"); LList.Insert(myLList, 50); current = myLList.First; while (current != null) { Console.WriteLine(current.Value); current = current.Next; } }
public void InsertOneValue() { LList lL = new LList(); lL.Insert(5); Assert.Equal(5, lL.Head.Value); }
public static void Execute() { LList<string> list = new LList<string>(); list.AddFirst(new linkedNode<string>("first to left")); list.AddFirst(new linkedNode<string>("second to left")); list.AddLast(new linkedNode<string>("first to right")); list.AddLast(new linkedNode<string>("second to right")); list.AddFirst(new linkedNode<string>("third to left")); list.AddLast(new linkedNode<string>("third to right")); linkedNode<string> newLastNode = new linkedNode<string>("last node"); list.AddLast(newLastNode); list.insertAfter(newLastNode, new linkedNode<string>("inserted first after the last")); list.insertAfter(newLastNode, new linkedNode<string>("inserted second after the last, but will appear right after the last")); list.Traverse(); Console.WriteLine("list2 items are being displayed"); LList<string> list2 = new LList<string>(); list2.AddLast(new linkedNode<string>("first item")); list2.AddLast(new linkedNode<string>("second item")); linkedNode<string> thirdItem = new linkedNode<string>("third item"); list2.AddLast(thirdItem); list2.AddLast(new linkedNode<string>("fourth item")); list2.AddLast(new linkedNode<string>("fifth item")); list2.removeLink(thirdItem); list2.Traverse(); Console.ReadLine(); }
public void TestMerge(int expected, int position) { // initializes two lists LList one = new LList(new Node(3)); LList two = new LList(new Node(6)); // add values to the lists one.Add(new Node(1)); two.Add(new Node(5)); two.Add(new Node(4)); two.Add(new Node(2)); // merges the two lists LList merged = merge(one, two); int iterator = 1; Node nthNode = merged.Head; // iterates to the nth node while (iterator < position) { nthNode = nthNode.Next; iterator++; } int check = (int)nthNode.Value; // compares the values Assert.True(check == expected); }
public void Insert(int value) { LList testList = new LList(); testList.Insert(value); Assert.Equal(testList.Head.Value, value); }
public void ListsOfEqualLength() { LList lL1 = new LList(); lL1.Insert(1); lL1.Insert(2); lL1.Insert(3); LList lL2 = new LList(); lL2.Insert(5); lL2.Insert(6); lL2.Insert(7); LList solution = new LList(); solution.Insert(5); solution.Insert(1); solution.Insert(6); solution.Insert(2); solution.Insert(7); solution.Insert(3); Assert.Equal(solution.Print(), Program.MergeLists(lL1, lL2).Print()); }
public void ListTwoLongerThanListOne() { // 5 -> 1 -> null LList lL1 = new LList(); lL1.Insert(1); lL1.Insert(5); // 3 -> 2 -> 6 -> 5 -> null LList lL2 = new LList(); lL2.Insert(5); lL2.Insert(6); lL2.Insert(2); lL2.Insert(3); // 5 -> 3 -> 1 -> 2 -> 6 -> 5 -> null LList solution = new LList(); solution.Insert(5); solution.Insert(6); solution.Insert(2); solution.Insert(1); solution.Insert(3); solution.Insert(5); Assert.Equal(solution.Print(), Program.MergeLists(lL1, lL2).Print()); }
static void Main(string[] args) { Console.WriteLine("Demonstration of zipper merge for two linked lists:\n\n"); //Create new linked list LList ll1 = new LList(new Node(1)); LList ll2 = new LList(new Node(5)); ll1.Append(new Node(3)); ll1.Append(new Node(2)); ll2.Append(new Node(9)); ll2.Append(new Node(4)); //Show values Console.WriteLine("The first linked list has the following values: "); ll1.Print(); Console.WriteLine("\n\nThe second linked list has the following values: "); ll2.Print(); LList llMerged = Merge(ll1, ll2); Console.WriteLine("\n\nThe merged linked list: "); llMerged.Print(); }
public static LList RemoveDupes(LList ll) { int length = 0; Node curr = ll.Head; // Get length while (curr.Next != null) { length += 1; curr = curr.Next; } curr = ll.Head; //nfor each node of the LL we are going to hold one Node and make a copy // then traverse the copy looking for dupes while (curr.Next != null) { Node temp = curr; while (temp.Next.Next != null) { if (temp.Next == curr) { temp.Next = temp.Next.Next; } temp = temp.Next; } curr = curr.Next; } return(ll); }
/// <summary> /// k-th value from the end of a linked list. /// </summary> /// <param name="k">value from the end to find</param> /// <param name="list">linked list to be traversed</param> /// <returns></returns> public static object FindNode(int k, LList list) { int counter = 0; list.Current = list.Head; while (list.Current != null) { counter++; list.Current = list.Current.Next; } list.Current = list.Current.Next; try { if (k > counter) { throw new Exception("Value not found in the List"); } } catch (Exception e) { return(e.Message); } for (int i = 0; i < counter - k; i++) { list.Current = list.Current.Next; } return(list.Current.Value); }
static void Main(string[] args) { Console.WriteLine("LinkedList!"); LList listing = new LList(); listing.Insert(2); listing.Insert(4); listing.Insert(6); listing.Print(); listing.Append(8); listing.Append(10); listing.Append(12); listing.Print(); listing.InsertBefore(10, 11); listing.Print(); listing.InsertAfter(2, 100); listing.Print(); int a = listing.GetValue(13); int b = listing.GetValue(2); int c = listing.GetValue(130); Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); }
public static void Main(string[] args) { Console.WriteLine("Hello Merging Lists!"); Console.WriteLine(""); LList carlosListOne = new LList(); carlosListOne.Insert(88); carlosListOne.Insert(77); carlosListOne.Insert(66); carlosListOne.Insert(55); carlosListOne.Insert(44); Console.WriteLine($"Linked List One:"); carlosListOne.Print(); LList carlosListTwo = new LList(); carlosListTwo.Insert(82); carlosListTwo.Insert(72); carlosListTwo.Insert(62); carlosListTwo.Insert(52); carlosListTwo.Insert(42); Console.WriteLine($"Linked List Two:"); carlosListTwo.Print(); Console.WriteLine("\n"); Console.WriteLine("Merging Linked List"); MergeLists(carlosListOne, carlosListTwo); carlosListOne.Print(); Console.ReadLine(); }
/// <summary> /// Merges two lists and returns one list of the merged list. The original lists are destroyed /// </summary> /// <param name="one">first list to merge</param> /// <param name="two">second list to merge</param> /// <returns></returns> public static LList merge(LList one, LList two) { // creates the two pointers Node first = one.Head; Node second = two.Head; // loops while neither is null while (first.Next != null && second.Next != null) { // stores the next of each current into temp variables Node nextFirst = first.Next; Node nextSecond = second.Next; // switches to the appropriate references first.Next = second; second.Next = nextFirst; // resets the points to the correct currents first = nextFirst; second = nextSecond; } // sets the rest of the list if (first.Next == null) { first.Next = second; } else { second.Next = first; } // reset two so you can't change the list using the old references two.Head = null; return(one); }
static void Main(string[] args) { // Creates new list one with values: [1] -> [3] LList one = new LList(new Node(3)); one.Add(new Node(1)); // Creates new list two with values: [2] -> [4] -> [5] -> [6] LList two = new LList(new Node(6)); two.Add(new Node(5)); two.Add(new Node(4)); two.Add(new Node(2)); // Displays the first list WriteLine("List one has the following values: "); DisplayList(one); // Displays the second list WriteLine("List two has the following values: "); DisplayList(two); // Merges the two lists and displays them LList merged = merge(one, two); WriteLine("After merging, list should be 1, 2, 3, 4, 5, 6"); WriteLine("Actual"); DisplayList(merged); }
public static void Main(string[] args) { Console.WriteLine("Hello World!"); Node node1 = new Node(1); Node node2 = new Node(12); LList one = new LList(node1); LList two = new LList(node2); for (int i = 3; i < 8; i++) { Node newNode = new Node(i); Node newNode2 = new Node(i + 10); one.Append(newNode); two.Append(newNode2); } one.Print(); two.Print(); Merge(one, two); one.Print(); }
public static object FindKthElement(LList list, int k) { int count = 0; object result = null; list.Current = list.Head; while (list.Current != null) { count++; list.Current = list.Current.Next; } list.Current = list.Head; count -= k; if (count < 0) { throw new Exception("Invalid input number."); } for (int i = 0; i < count - 1; i++) { list.Current = list.Current.Next; } result = list.Current.Value; list.Current = list.Head; return(result); }
public static void Main(string[] args) { Node node1 = new Node(7); Node node2 = new Node("node2"); Node node3 = new Node("node3"); LList List = new LList(node1); List.Add(node2); List.Add(node3); Console.WriteLine("======= Original List ======="); List.Print(); Console.WriteLine("======= Node Appended to the End ======="); Node node4 = new Node("node4"); List.Append(node4); List.Print(); Console.WriteLine("======= Node Added Before Node 4 ======="); Node node5 = new Node("node5"); List.AddBefore(node5, node4); List.Print(); Console.WriteLine("======= Node Added After Node 5 ======="); Node node6 = new Node("node6"); List.AddAfter(node6, node5); List.Print(); Console.WriteLine("======= Find Node with value 7 ======="); //Console.WriteLine(List.FindValue(7)); }
/// <summary> /// Takes in two linked lists and merges the second list into the first list, in place /// </summary> /// <param name="a">First linked list</param> /// <param name="b">Second Linked List</param> /// <returns></returns> public static Node Merge(LList a, LList b) { a.Current = a.Head; b.Current = b.Head; while (a.Current.Next != null && b.Current.Next != null) { Node tempA = a.Current.Next; Node tempB = b.Current.Next; a.Current.Next = b.Current; b.Current.Next = tempA; a.Current = tempA; b.Current = tempB; } if (a.Current.Next == null) { a.Current.Next = b.Current; } else if (b.Current.Next == null) { b.Current.Next = a.Current.Next; a.Current.Next = b.Current; } return(a.Head); }
public void LListFind() { var list = new LList <int>(); for (int i = 0; i < 10; i++) { list.AddLast(i); } var node = list.Find(3); Assert.AreEqual(node.Value, 3); var list2 = new LList <string>(); list2.AddFirst("a"); list2.AddFirst("b"); list2.AddFirst("c"); var node2 = list2.Find("b"); Assert.AreEqual(node2.Value, "b"); var list3 = new LList <int>(); var node3 = list3.Find(2); Assert.AreEqual(node3, null); }
public void LListConcatenate() { var list1 = new LList <int>(); for (int i = 0; i < 5; i++) { list1.AddLast(i); } var list2 = new LList <int>(); for (int i = 10; i < 15; i++) { list2.AddLast(i); } LList <int> .Concatenate(ref list1, ref list2); Console.WriteLine(list1.ToString()); Assert.AreEqual(list1.First.Value, 0); Assert.AreEqual(list1.Last.Value, 14); Assert.AreEqual(list2.First.Value, 10); Assert.AreEqual(list2.Last.Value, 14); //Console.WriteLine("---list1 empty state---"); var listEmpty = new LList <int>(); LList <int> .Concatenate(ref listEmpty, ref list1); Assert.AreEqual(listEmpty.First.Value, 0); Assert.AreEqual(listEmpty.Last.Value, 14); }
public void LListContains() { var list = new LList <int>(); list.AddLast(10); if (list.Contains(10)) { Console.WriteLine("list contains 10"); } else { Console.WriteLine("list does not contain 10"); } var list2 = new LList <string>(); list2.AddLast("asdf"); if (list2.Contains("asdf")) { Console.WriteLine("list contains asdf"); } else { Console.WriteLine("list does not contain asdf"); } if (list2.Contains("aaa")) { Console.WriteLine("list contains aaa"); } else { Console.WriteLine("list does not contain aaa"); } }
/// <summary> /// Polygon Unio /// </summary> /// <param name="polya">The polya.</param> /// <param name="polyb">The polyb.</param> /// <param name="union">The union.</param> /// <param name="intersection">The intersection.</param> /// <returns></returns> public static bool PolyUnion(Vector2[] polya, Vector2[] polyb, out Vector2[] union, out Vector2[] intersection) { if (!Intersects(polya, polyb)) { union = polya; intersection = polyb; return false; } LList a = new LList(polya), b = new LList(polyb); List<Intersection> intersections = new List<Intersection>(); Vector2 vert; VNode aNode = a.First, bNode = b.First; //Find intersection points between the polygons do { do { if (EdgeIntersects(aNode.Value, (aNode.Next == null) ? a.First.Value : aNode.Next.Value, bNode.Value, (bNode.Next == null) ? b.First.Value : bNode.Next.Value, out vert)) { //An intersection point has been found! intersections.Add(new Intersection(vert, aNode, bNode, (aNode.Next == null) ? a.First : aNode.Next, (bNode.Next == null) ? b.First : bNode.Next)); } bNode = bNode.Next; } while (bNode != null); bNode = b.First; aNode = aNode.Next; } while (aNode != null); //Perform surgery on these intersections Intersection i; for (int j = 0; j < intersections.Count; j++) { i = intersections[j]; i.aIn.Next = new VNode(i.Position, i.bOut, i.aIn); i.bOut.Prev = i.aIn.Next; i.bIn.Next = new VNode(i.Position, i.aOut, i.bIn); i.aOut.Prev = i.bIn.Next; } //Decompose and simplify polygons into arrays union = a.ToArray(); intersection = b.ToArray(); //Find exterior polygon if (union.Length < intersection.Length) { //Polygons need swapping! Vector2[] u = union; union = intersection; intersection = u; } return true; }
public void Delete() { var list = new LList<int>(); var node = new LNode<int>(6); list.Add(node); list.Add(24); Assert.AreEqual(24, list.HeadContent); }
public void Search() { var list = new LList<int>(); var node = new LNode<int>(6); list.Add(node); list.Add(24); Assert.AreEqual(node, list.Search(6)); }
public void AddStmtLabels(Statement s, LList<Label> node) { if (node != null) { AddStmtLabels(s, node.Next); if (node.Data.Name == null) { // this indicates an implicit-target break statement that has been resolved; don't add it } else { s.Labels = new LList<Label>(new Label(Tok(node.Data.Tok), node.Data.Name), s.Labels); } } }
public void Add() { var list = new LList<int>(); list.Add(new LNode<int>(6)); Assert.AreEqual(6, list.HeadContent); list.Add(new LNode<int>(5)); Assert.AreEqual(5, list.HeadContent); list.Add(new LNode<int>(15)); Assert.AreEqual(15, list.HeadContent); }
/// <summary> ///Mthod to add a new vertex with its neighbors. /// </summary> /// <param name="vertex"></param> /// <param name="neighbors"></param> public void Add(int vertex, IEnumerable<int> neighbors) { if (vertex >= _graph.Length) { throw new ArgumentOutOfRangeException("vertex not compatible with current graph definition"); } var linkedList = new LList<int>(); foreach (var neighbor in neighbors) { if (neighbor >= _graph.Length) { throw new ArgumentOutOfRangeException("vertex not compatible with current graph definition"); } linkedList.Add(neighbor); } _graph[vertex] = linkedList; }
public DGraph(int vertexCount) { _graph = new LList<int>[vertexCount]; }
LList<Field>.Iterator checkField(LList<Field>.Iterator currentField) { // If the two slopes are colinear, and if they pass through either // extremity, remove the field of view. if (currentField.Current.shallow.doesContain(currentField.Current.steep.near) && currentField.Current.shallow.doesContain(currentField.Current.steep.far) && (currentField.Current.shallow.doesContain(shallowOrigin) || currentField.Current.shallow.doesContain(steepOrigin))) { return activeFields.Erase(currentField); } else return currentField; }
void visitSquare(Pos dest, ref LList<Field>.Iterator currentField) { //Debug.Print(string.Format("{0}, {1}", dest.x, dest.y)); // The top-left and bottom-right corners of the destination square. Pos topLeft = new Pos(dest.x, dest.y + fov_size); Pos bottomRight = new Pos(dest.x + fov_size, dest.y); LList<Field>.Iterator end = activeFields.End(); while (currentField != end && currentField.Current.steep.isBelowOrContains(bottomRight)) { //Debug.Print("ABOVE"); // case ABOVE // The square is in case 'above'. This means that it is ignored // for the currentField. But the steeper fields might need it. ++currentField; } if (currentField == activeFields.End()) { //Debug.Print("ABOVE ALL"); // The square was in case 'above' for all fields. This means that // we no longer care about it or any squares in its diagonal rank. return; } // Now we check for other cases. if (currentField.Current.shallow.isAboveOrContains(topLeft)) { //Debug.Print("BELOW"); // case BELOW // The shallow line is above the extremity of the square, so that // square is ignored. return; } // The square is between the lines in some way. This means that we // need to visit it and determine whether it is blocked. bool isBlocked = actIsBlocked(dest, currentField.Current); if (!isBlocked) { //Debug.Print("NOT BLOCKED"); // We don't care what case might be left, because this square does // not obstruct. return; } if (currentField.Current.shallow.isAbove(bottomRight) && currentField.Current.steep.isBelow(topLeft)) { //Debug.Print("BLOCKING"); // case BLOCKING // Both lines intersect the square. This current field has ended. currentField = activeFields.Erase(currentField); } else if (currentField.Current.shallow.isAbove(bottomRight)) { //Debug.Print("SHALLOW BUMP"); // case SHALLOW BUMP // The square intersects only the shallow line. addShallowBump(topLeft, currentField.Current); currentField = checkField(currentField); } else if (currentField.Current.steep.isBelow(topLeft)) { //Debug.Print("STEEP BUMP"); // case STEEP BUMP // The square intersects only the steep line. addSteepBump(bottomRight, currentField.Current); checkField(currentField); } else { // Debug.Print("BETWEEN"); // case BETWEEN // The square intersects neither line. We need to split into two fields. LList<Field>.Iterator steeperField = currentField; LList<Field>.Iterator shallowerField = activeFields.Insert(currentField, currentField.Current.Copy()); addSteepBump(bottomRight, shallowerField.Current); checkField(shallowerField); addShallowBump(topLeft, steeperField.Current); currentField = checkField(steeperField); } }
/// <summary>Resets all values to their defaults.</summary> public override void clear() { base.clear(); InWorld = null; Length = FInt.F0; EndLength = new FInt[2]; Nodes = new Node[2]; People = new LList<FInt>[2]; People[0] = new LList<FInt>(); People[1] = new LList<FInt>(); NumPeople = 0; Capacity = FInt.F0; CurLaneCapacity = FInt.F0; Direction = VectorF.Zero; Direction = VectorF.Zero; Angle = FInt.F0; Destroyed = false; }