Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
linked list (add to end and beginning, and looking for cycles based on Floyd's algorithm)
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { Console.WriteLine("Add First:"); LinkedList myList1 = new LinkedList(); //myList1.initilizeTheList(); myList1.AddFirst("Hello"); myList1.AddFirst("Magical"); myList1.AddFirst("World"); myList1.printAllNodes(); Console.WriteLine(); Console.WriteLine("Add Last:"); LinkedList myList2 = new LinkedList(); //myList2.initilizeTheList(); myList2.AddLast("Hello"); myList2.AddLast("Magical"); myList2.AddLast("World"); myList2.printAllNodes(); bool hasCycle = myList2.HasCycle(); Console.WriteLine("myList2 " + ((hasCycle == true) ? "has a cycle" : "has no cycle")); Console.ReadLine(); } public class Node { public Node next; public Object data; } public class LinkedList { Node head; Node tail; public void printAllNodes() { Node current = head; while (current != null) { Console.WriteLine(current.data); current = current.next; } } public void AddFirst(Object data) { Node toAdd = new Node(); if (tail == null && head == null) //empty list { head = toAdd; tail = toAdd; } else { toAdd.next = head; head = toAdd; } toAdd.data = data; } public void AddLast(Object data) { Node toAdd = new Node(); if (tail == null && head == null) //empty list { head = toAdd; tail = toAdd; } else { tail.next = toAdd; tail = toAdd; } toAdd.data = data; } public bool HasCycle() { Node slowRabbit; Node fastRabbit; if (head == null) return false; if (head.next == null) return false; if (head.next.next == null) return false; slowRabbit = head; fastRabbit = slowRabbit.next; while (fastRabbit.next != null) { if (slowRabbit == fastRabbit.next) return true; else { slowRabbit = slowRabbit.next; if (fastRabbit.next != null) if (fastRabbit.next.next != null) { fastRabbit = fastRabbit.next.next; } else { return false; } else { return false; } } } return false; } } } }
run
|
edit
|
history
|
help
0
IFormatProvider Caveat
kjk
Hello-Heath-World
bytes to string to bytes.
C# INDEXER and OVERRIDE INDEXER
Enum.IsDefined
Fórum ➡ Rotating a Square Matrix One Cell Counter Clockwise
Print("This is a Test")
f
Compiler