public void AddAfter(string existingValue, string value) { if (ElementAt(-1) == existingValue) { AddLast(value); } else { SinglyLinkedListNode prevNode = firstNode; bool found = false; while (!prevNode.IsLast()) { if (prevNode.Value == existingValue) { SinglyLinkedListNode newNode = new SinglyLinkedListNode(value); SinglyLinkedListNode oldNext = prevNode.Next; newNode.Next = oldNext; prevNode.Next = newNode; found = true; count++; break; } prevNode = prevNode.Next; } if (!found) { throw new ArgumentException(); } } }
public override string ToString() { string leftBr = "{"; string rightBr = "}"; string comma = ","; string space = " "; string quote = "\""; StringBuilder newList = new StringBuilder(leftBr); SinglyLinkedListNode currentNode = firstNode; while (currentNode != null) { newList.Append(space); newList.Append(quote); newList.Append(currentNode.Value); newList.Append(quote); if (currentNode.IsLast()) { break; } else { newList.Append(comma); } currentNode = currentNode.Next; } newList.Append(space).Append(rightBr); return(newList.ToString()); }
public string[] ToArray() { LinkedList <string> sentence = new LinkedList <string>(); SinglyLinkedListNode currentNode = firstNode; if (this.First() == null) { return(sentence.ToArray()); } else if (firstNode.IsLast()) { sentence.AddFirst(firstNode.ToString()); return(sentence.ToArray()); } else { while (currentNode != null) { sentence.AddLast(currentNode.ToString()); if (currentNode.IsLast()) { break; } currentNode = currentNode.Next; } return(sentence.ToArray()); } }
private SinglyLinkedListNode NodeAt(int index) { if (firstNode != null && index >= 0) { SinglyLinkedListNode node = firstNode; for (int i = 0; i <= index; i++) { if (index == i) { break; } if (node.IsLast()) { throw new ArgumentOutOfRangeException(); } node = node.Next; } return(node); } else if (index < 0) { return(this.NodeAt(Count() + index)); //Positive index/offset } else { throw new ArgumentOutOfRangeException(); } }
public void AddAfter(string existingValue, string value) { if (ElementAt(-1) == existingValue) { AddLast(value); } else { SinglyLinkedListNode current = firstNode; bool found = false; while (!current.IsLast()) { if (current.Value == existingValue) { SinglyLinkedListNode new_node = new SinglyLinkedListNode(value); SinglyLinkedListNode old_next = current.Next; new_node.Next = old_next; current.Next = new_node; found = true; break; } current = current.Next; } if (!found) { throw new ArgumentException(); } } }
public int IndexOf(string value) { SinglyLinkedListNode Current_node = first; int position = 0; bool found = false; if (first.Value == null) { position = -1; } else { while (!Current_node.IsLast()) { if (value == Current_node.Value) { found = true; break; } Current_node = Current_node.Next; position++; } if (!found) { position = -1; } } return(position); }
public int IndexOf(string value) { SinglyLinkedListNode node = first; if (node.Value == null) { return(-1); } int count = 0; while (!node.IsLast()) { if (node.Value == value) { return(count); } node = node.Next; count++; } if (node.Value == value) { return(count); } else { return(-1); } }
public SinglyLinkedListNode NodeAt(int index) { if (index < 0) { SinglyLinkedListNode currentNode = firstNode; int count = 1; while (!currentNode.IsLast()) { count++; currentNode = currentNode.Next; } return(NodeAt(count + index)); } if (index > this.size) { throw new ArgumentOutOfRangeException(); } if (this.firstNode == null) { throw new ArgumentOutOfRangeException(); } else { SinglyLinkedListNode currentNode = firstNode; for (int j = 0; j < index; j++) { currentNode = currentNode.Next; } return(currentNode); } }
public void NodeLastWhenLast() { SinglyLinkedListNode node1 = new SinglyLinkedListNode("foo"); SinglyLinkedListNode node2 = new SinglyLinkedListNode("bar"); node1.Next = node2; Assert.IsFalse(node1.IsLast()); }
public string ElementAt(int index) { if (firstNode != null && index >= 0) { SinglyLinkedListNode node = firstNode; for (int i = 0; i <= index; i++) { if (index == i) { break; } if (node.Next == null) { throw new ArgumentOutOfRangeException(); } node = node.Next; } return(node.Value); } else if (index < 0) { SinglyLinkedListNode node = firstNode; int length = 1; while (!node.IsLast()) { length++; node = node.Next; } return(this.ElementAt(length + index)); } else { throw new ArgumentOutOfRangeException(); } }
public string[] ToArray() { SinglyLinkedListNode node = firstNode; if (node != null) { int length = 1; while (!node.IsLast()) { length++; node = node.Next; } string[] answer = new string[length]; for (int i = 0; i < length; i++) { answer[i] = ElementAt(i); } return(answer); } else { return(new string[] { }); } }
public override string ToString() { string left = "{"; string right = "}"; string space = " "; string comma = ","; string quote = "\""; StringBuilder s = new StringBuilder(left); SinglyLinkedListNode current_node = firstNode; while (current_node != null) { s.Append(space); s.Append(quote); s.Append(current_node.Value); s.Append(quote); if (current_node.IsLast()) { break; } else { s.Append(comma); } current_node = current_node.Next; } s.Append(space + right); return(s.ToString()); }
public bool IsSorted() { //throw new NotImplementedException(); SinglyLinkedListNode node = first; if (node == null || node.Value == null || node.IsLast()) { return(true); } else { SinglyLinkedListNode left = node; SinglyLinkedListNode right = node.Next; while (right != null) { //if(node.CompareTo(node.Next) >= 0) //{ // return false; //} //node = node.Next; if (left > right) { return(false); } left = right; right = left.Next; } return(true); } }
private SinglyLinkedListNode LastNode() { SinglyLinkedListNode currentNode = firstNode; while (!currentNode.IsLast()) { currentNode = currentNode.Next; } return(currentNode); }
public override string ToString() { SinglyLinkedListNode currentNode = this.firstNode; string output = "{ "; if (currentNode != null) { while (!currentNode.IsLast()) { output += "\"" + currentNode.Value + "\", "; currentNode = currentNode.Next; } if (currentNode.IsLast()) { output += "\"" + currentNode.Value + "\" "; currentNode = currentNode.Next; } } return(output + "}"); }
public void AddLast(string value) { SinglyLinkedListNode node = first; if (first.Value == null) { first = new SinglyLinkedListNode(value); } while (!node.IsLast()) { node = node.Next; } node.Next = new SinglyLinkedListNode(value); }
public override string ToString() { string left_br = "{"; string right_br = "}"; string space = " "; string comma = ","; string quote = "\""; StringBuilder s = new StringBuilder(left_br); SinglyLinkedListNode current_node = firstNode; while (current_node != null) { s.Append(space); s.Append(quote); s.Append(current_node.Value); s.Append(quote); if (current_node.IsLast()) { break; } else { s.Append(comma); } current_node = current_node.Next; } s.Append(space + right_br); return(s.ToString()); //The following code also worked //SinglyLinkedListNode current = firstNode; //if (current == null) //{ // return "{ }"; //} //StringBuilder builder = new StringBuilder(); //builder.Append("\"" + current.Value + "\""); //while (current.Next != null) //{ // current = current.Next; // builder.Append(", " + "\"" + current.Value + "\""); //} //string innerString = builder.ToString(); //return "{ " + innerString + " }"; }
// NOTE: There is more than one way to accomplish this. One is O(n). The other is O(1). public int Count() { if (this.firstNode == null) { return(0); } SinglyLinkedListNode currentNode = this.firstNode; int count = 1; while (!currentNode.IsLast()) { currentNode = currentNode.Next; count++; } return(count); }
public void AddLast(string value) { if (this.First() == null) { firstNode = new SinglyLinkedListNode(value); } else { SinglyLinkedListNode nextNode = firstNode; while (!nextNode.IsLast()) { nextNode = nextNode.Next; } nextNode.Next = new SinglyLinkedListNode(value); } }
public void AddLast(string value) { if (firstNode == null) { firstNode = new SinglyLinkedListNode(value); } else { SinglyLinkedListNode current_node = firstNode; while (!current_node.IsLast()) { current_node = current_node.Next; } current_node.Next = new SinglyLinkedListNode(value); } }
// NOTE: There is more than one way to accomplish this. One is O(n). The other is O(1). public int Count() { if (firstNode == null) { return(0); } int counter = 1; SinglyLinkedListNode currentNode = firstNode; while (!currentNode.IsLast()) { currentNode = currentNode.Next; counter++; } return(counter); }
public override string ToString() { StringBuilder builtString = new StringBuilder("{ "); if (firstNode != null) { SinglyLinkedListNode testNode = firstNode; while (!testNode.IsLast()) { builtString.Append("\"").Append(testNode.ToString()).Append("\", "); testNode = testNode.Next; } builtString.Append("\"").Append(testNode.ToString()).Append("\" "); } builtString.Append("}"); return(builtString.ToString()); }
public void AddLast(string value) { if (firstNode == null) { firstNode = new SinglyLinkedListNode(value); } else { lastNode = firstNode; while (!lastNode.IsLast()) { lastNode = lastNode.Next; } lastNode.Next = new SinglyLinkedListNode(value); } count++; }
public override string ToString() { string left = "{"; string right = "}"; string space = " "; string comma = ","; string quote = "\""; StringBuilder s = new StringBuilder(left); SinglyLinkedListNode current_node = firstNode; while (current_node != null) { s.Append(space); s.Append(quote); s.Append(current_node.Value); s.Append(quote); if (current_node.IsLast()) { break; } else { s.Append(comma); } current_node = current_node.Next; } s.Append(space + right); return(s.ToString()); /* * if (firstNode == null) * { * return "{ }"; * } * else * { * StringBuilder s = new StringBuilder("{ "); //allows you to build strings without eating all your computers memory * s.Append("\"" + firstNode.Value + "\""); * s.Append(" }"); * return s.ToString(); * } */ }
public override string ToString() { StringBuilder sb = new StringBuilder(); if (firstNode != null) { SinglyLinkedListNode nodes = firstNode; while (!nodes.IsLast()) { sb.Append("\""); sb.Append(nodes.Value.ToString()); sb.Append("\""); sb.Append(", "); nodes = nodes.Next; } sb.Append("\"" + nodes.Value.ToString() + "\" "); } return("{ " + sb + "}"); }
public bool IsSorted() { bool sorted = true; if (count < 2) { } else { SinglyLinkedListNode testNode = firstNode; while (!testNode.IsLast()) { if (testNode > testNode.Next) { sorted = false; } testNode = testNode.Next; } } return(sorted); }
public void AddAfter(string existingValue, string value) { if (firstNode == null) { firstNode = new SinglyLinkedListNode(value); return; } SinglyLinkedListNode currentNode = firstNode; while (currentNode.Value != existingValue) { currentNode = currentNode.Next; if (currentNode.IsLast() && currentNode.Value != existingValue) { throw new ArgumentException(); } } SinglyLinkedListNode insert = new SinglyLinkedListNode(value); insert.Next = currentNode.Next; currentNode.Next = insert; }
public void AddAfter(string existingValue, string value) { SinglyLinkedListNode node = first; while (node.Value != existingValue && node != null) { node = node.Next; if (node == null) { throw new ArgumentException(); } } if (node.IsLast()) { AddLast(value); return; } SinglyLinkedListNode insertedNode = new SinglyLinkedListNode(value); insertedNode.Next = node.Next; node.Next = insertedNode; }
public void AddAfter(string existingValue, string value) { //throw new NotImplementedException(); if (Count() == 0) { first = new SinglyLinkedListNode(value); } else { SinglyLinkedListNode node = first; while (node.Value != existingValue) { if (node.IsLast()) { throw new ArgumentException(); } node = node.Next; } SinglyLinkedListNode storage = node.Next; node.Next = new SinglyLinkedListNode(value); node.Next.Next = storage; } }
private SinglyLinkedListNode NodeAt(int index) { if (firstNode == null) { throw new ArgumentOutOfRangeException(); } if (index < 0) { index = this.Count() + index; } SinglyLinkedListNode currentNode = firstNode; for (int i = 0; i < index; i++) { if (currentNode.IsLast()) { throw new ArgumentOutOfRangeException(); } currentNode = currentNode.Next; } return(currentNode); }
public void NodeLastWhenNotLast() { SinglyLinkedListNode node = new SinglyLinkedListNode("foo"); Assert.IsTrue(node.IsLast()); }