diff --git a/DoublyLinkedList/DoublyLinkedList.sln b/DoublyLinkedList/DoublyLinkedList.sln new file mode 100644 index 0000000..2e437f8 --- /dev/null +++ b/DoublyLinkedList/DoublyLinkedList.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31229.75 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DoublyLinkedList", "DoublyLinkedList\DoublyLinkedList.csproj", "{D7477EF3-B68D-47A5-BFA0-8FDD9354C99C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D7477EF3-B68D-47A5-BFA0-8FDD9354C99C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D7477EF3-B68D-47A5-BFA0-8FDD9354C99C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D7477EF3-B68D-47A5-BFA0-8FDD9354C99C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D7477EF3-B68D-47A5-BFA0-8FDD9354C99C}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {643B29EA-AE0F-4413-8548-0A354F07F465} + EndGlobalSection +EndGlobal diff --git a/DoublyLinkedList/DoublyLinkedList/App.config b/DoublyLinkedList/DoublyLinkedList/App.config new file mode 100644 index 0000000..5754728 --- /dev/null +++ b/DoublyLinkedList/DoublyLinkedList/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/DoublyLinkedList/DoublyLinkedList/DoublyLinkedList.csproj b/DoublyLinkedList/DoublyLinkedList/DoublyLinkedList.csproj new file mode 100644 index 0000000..a03071e --- /dev/null +++ b/DoublyLinkedList/DoublyLinkedList/DoublyLinkedList.csproj @@ -0,0 +1,55 @@ + + + + + Debug + AnyCPU + {D7477EF3-B68D-47A5-BFA0-8FDD9354C99C} + Exe + DoublyLinkedList + DoublyLinkedList + v4.7.2 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/DoublyLinkedList/DoublyLinkedList/LinkedList.cs b/DoublyLinkedList/DoublyLinkedList/LinkedList.cs new file mode 100644 index 0000000..5d015f0 --- /dev/null +++ b/DoublyLinkedList/DoublyLinkedList/LinkedList.cs @@ -0,0 +1,257 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoublyLinkedList +{ + class LinkedList + { + + public Node Head { get; set; } + public Node Current { get; set; } + public Node Tail { get; set; } + + + //Class Constructors ----------------------------------------------- + + public LinkedList() + { + Head = new Node(null); + Tail = Head; + } + + public LinkedList(dynamic data) + { + Head = new Node(data); + Tail = Head; + } + //----------------------------------------------------------------- + + + public void AddItem(dynamic data) + { + //Creates a new object and attatches it as the tail of the list. + Node newNode = new Node(data, Tail); + Tail.setNewNextNode(newNode); + Tail = newNode; + + } + + + public void AddItemInOrder(dynamic data) + { + + Node newItem; + + + //I have added this try statement so that if the user enters a value that is not comparable, then it goes straight to the tail + //As well as this, if any of the items in the list are not comparable, the value we are trying to add will also go to the tail + try + { + + //Check if the data is smaller than the Head + if (data <= Head.Data) + { + + newItem = new Node(data, null, Head); + Head.setNewPreviousNode(newItem); + Head = newItem; + + return; + + } + else + { + + Current = Head; + //checks to ensure the head isn't the only item in the list + if (Current.getNextNode() != null) + { + + //Itterate throught the list to see if the new node belongs in any of the positions + do + { + Current = Current.getNextNode(); + + if (data <= Current.Data) + { + newItem = new Node(data, Current.getPreviousNode(), Current); + Current.getPreviousNode().setNewNextNode(newItem); + Current.setNewPreviousNode(newItem); + + return; + } + + + + } while (Current.getNextNode() != null); + } + + //Append the new node onto the end of the list + AddItem(data); + + + } + + } + catch (Exception) + { + //If the data is not sortable, then it will just be appended to the end of the list. + AddItem(data); + + } + + + } + + + public dynamic getItemData(int index) + { + + Current = getItem(index); + + //Checks to see if there was actally a value placed into the list + if (Current == null) + { + return "Index out of Bounds"; + } + else + { + if (Current.Data == null) + { + return "null"; + } + else + { + return Current.Data; + } + } + } + + public Node getItem(int index) + { + Current = Head; + + //Itterate through the loop until we can set the current item to nth item in the list + for (var i = 0; i < index; i++) + { + //If we have reached the end and can't go further... + if (Current.getNextNode() != null) + { + Current = Current.getNextNode(); + } + else + { + return null; + } + + } + + return Current; + } + + public void removeItem(int index) + { + Current = getItem(index); + + //only execute it if the item is actuall valid; + if (Current != null) + { + + + + + + //If the item is at the end of the list + if (Current.getNextNode() == null) + { + //set the previous node as tail. + Tail = Current.getPreviousNode(); + Tail.setNewNextNode(null); + } + else + { + //Set the next nodes previous pointer to the previous item. + Current.getNextNode().setNewPreviousNode(Current.getPreviousNode()); + } + + //If the item is at the start of the list + if (Current.getPreviousNode() == null) + { + Head = Current.getNextNode(); + Head.setNewPreviousNode(null); + } + else + { + //Set the previous items pointer to the next item. + Current.getPreviousNode().setNewNextNode(Current.getNextNode()); + } + + } + + + } + + + public void printList() + { + Current = Head; + + do + { + //check to see if the data is null + if (Current.Data == null) + { + Console.Write("null" + "-->"); + } + else + { + Console.Write(Current.Data + "-->"); + } + + Current = Current.getNextNode(); + + //In this piece of code, we iterate onto the next item at the end of the loop in order to allow the head to be the first item. + //This means that we have to check for the current item being the end of the list as opposed to the next item being the end. + + } while (Current != null); + + Console.Write("null \n"); + } + + + + public void printListReverse() + { + //Start from the tail and itterate backwards + Current = Tail; + + do + { + //check to see if the data is null + if (Current.Data == null) + { + Console.Write("null" + "-->"); + } + else + { + Console.Write(Current.Data + "-->"); + } + + Current = Current.getPreviousNode(); + + //In this piece of code, we iterate onto the previous item at the end of the loop in order to allow the tail to be the first item. + //This means that we have to check for the current item being the start of the list as opposed to the next item being the start. + + } while (Current != null); + + Console.Write("null \n"); + } + + + + + } +} diff --git a/DoublyLinkedList/DoublyLinkedList/Node.cs b/DoublyLinkedList/DoublyLinkedList/Node.cs new file mode 100644 index 0000000..b9de958 --- /dev/null +++ b/DoublyLinkedList/DoublyLinkedList/Node.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoublyLinkedList +{ + class Node + { + public dynamic Data { get; set; } = null; + private Node Next { get; set; } = null; + private Node Previous { get; set; } = null; + + + //Class Constructors for each Node ----------------- + public Node(dynamic _Data) + { + Data = _Data; + } + + public Node(dynamic _Data, Node _Previous) + { + Data = _Data; + Previous = _Previous; + } + + public Node(dynamic _Data, Node _Previous, Node _Next) + { + Data = _Data; + Previous = _Previous; + Next = _Next; + } + //-------------------------------------------------- + + + public void setNewPreviousNode(Node _Previous) + { + Previous = _Previous; + } + + public Node getPreviousNode() + { + return Previous; + } + + public void setNewNextNode(Node _Next) + { + Next = _Next; + } + + public Node getNextNode() + { + return Next; + } + } +} diff --git a/DoublyLinkedList/DoublyLinkedList/Program.cs b/DoublyLinkedList/DoublyLinkedList/Program.cs new file mode 100644 index 0000000..6bf1198 --- /dev/null +++ b/DoublyLinkedList/DoublyLinkedList/Program.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoublyLinkedList +{ + class Program + { + static void Main(string[] args) + { + LinkedList list = new LinkedList(50); + + list.AddItemInOrder(2); + list.AddItemInOrder(3); + list.AddItemInOrder(0); + list.AddItemInOrder(20); + list.AddItemInOrder(2.56); + list.AddItemInOrder(-5); + + list.printList(); + list.printListReverse(); + Console.WriteLine(list.getItemData(0)); + Console.WriteLine(list.getItemData(1)); + Console.WriteLine(list.getItemData(3)); + Console.WriteLine(list.getItemData(7)); + + list.removeItem(2); + list.printList(); + + list.removeItem(0); + list.printList(); + + list.removeItem(4); + list.printList(); + + Console.ReadLine(); + } + } +} diff --git a/DoublyLinkedList/DoublyLinkedList/Properties/AssemblyInfo.cs b/DoublyLinkedList/DoublyLinkedList/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..c6a0614 --- /dev/null +++ b/DoublyLinkedList/DoublyLinkedList/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("DoublyLinkedList")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("DoublyLinkedList")] +[assembly: AssemblyCopyright("Copyright © 2021")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("d7477ef3-b68d-47a5-bfa0-8fdd9354c99c")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/DoublyLinkedList/DoublyLinkedList/bin/Debug/DoublyLinkedList.exe b/DoublyLinkedList/DoublyLinkedList/bin/Debug/DoublyLinkedList.exe new file mode 100644 index 0000000..3110e40 Binary files /dev/null and b/DoublyLinkedList/DoublyLinkedList/bin/Debug/DoublyLinkedList.exe differ diff --git a/DoublyLinkedList/DoublyLinkedList/bin/Debug/DoublyLinkedList.exe.config b/DoublyLinkedList/DoublyLinkedList/bin/Debug/DoublyLinkedList.exe.config new file mode 100644 index 0000000..5754728 --- /dev/null +++ b/DoublyLinkedList/DoublyLinkedList/bin/Debug/DoublyLinkedList.exe.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/DoublyLinkedList/DoublyLinkedList/bin/Debug/DoublyLinkedList.pdb b/DoublyLinkedList/DoublyLinkedList/bin/Debug/DoublyLinkedList.pdb new file mode 100644 index 0000000..b240034 Binary files /dev/null and b/DoublyLinkedList/DoublyLinkedList/bin/Debug/DoublyLinkedList.pdb differ diff --git a/DoublyLinkedList/DoublyLinkedList/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/DoublyLinkedList/DoublyLinkedList/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache new file mode 100644 index 0000000..dbb90c5 Binary files /dev/null and b/DoublyLinkedList/DoublyLinkedList/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ diff --git a/DoublyLinkedList/DoublyLinkedList/obj/Debug/DoublyLinkedList.csproj.CoreCompileInputs.cache b/DoublyLinkedList/DoublyLinkedList/obj/Debug/DoublyLinkedList.csproj.CoreCompileInputs.cache new file mode 100644 index 0000000..98edc72 --- /dev/null +++ b/DoublyLinkedList/DoublyLinkedList/obj/Debug/DoublyLinkedList.csproj.CoreCompileInputs.cache @@ -0,0 +1 @@ +6fc905fca675478f89c5e9ba4ef545ac371a1927 diff --git a/DoublyLinkedList/DoublyLinkedList/obj/Debug/DoublyLinkedList.csproj.FileListAbsolute.txt b/DoublyLinkedList/DoublyLinkedList/obj/Debug/DoublyLinkedList.csproj.FileListAbsolute.txt new file mode 100644 index 0000000..4a3c07e --- /dev/null +++ b/DoublyLinkedList/DoublyLinkedList/obj/Debug/DoublyLinkedList.csproj.FileListAbsolute.txt @@ -0,0 +1,7 @@ +C:\Users\Administrator\Desktop\DoublyLinkedList\DoublyLinkedList\bin\Debug\DoublyLinkedList.exe.config +C:\Users\Administrator\Desktop\DoublyLinkedList\DoublyLinkedList\bin\Debug\DoublyLinkedList.exe +C:\Users\Administrator\Desktop\DoublyLinkedList\DoublyLinkedList\bin\Debug\DoublyLinkedList.pdb +C:\Users\Administrator\Desktop\DoublyLinkedList\DoublyLinkedList\obj\Debug\DoublyLinkedList.csprojAssemblyReference.cache +C:\Users\Administrator\Desktop\DoublyLinkedList\DoublyLinkedList\obj\Debug\DoublyLinkedList.csproj.CoreCompileInputs.cache +C:\Users\Administrator\Desktop\DoublyLinkedList\DoublyLinkedList\obj\Debug\DoublyLinkedList.exe +C:\Users\Administrator\Desktop\DoublyLinkedList\DoublyLinkedList\obj\Debug\DoublyLinkedList.pdb diff --git a/DoublyLinkedList/DoublyLinkedList/obj/Debug/DoublyLinkedList.csprojAssemblyReference.cache b/DoublyLinkedList/DoublyLinkedList/obj/Debug/DoublyLinkedList.csprojAssemblyReference.cache new file mode 100644 index 0000000..e62c0a5 Binary files /dev/null and b/DoublyLinkedList/DoublyLinkedList/obj/Debug/DoublyLinkedList.csprojAssemblyReference.cache differ diff --git a/DoublyLinkedList/DoublyLinkedList/obj/Debug/DoublyLinkedList.exe b/DoublyLinkedList/DoublyLinkedList/obj/Debug/DoublyLinkedList.exe new file mode 100644 index 0000000..3110e40 Binary files /dev/null and b/DoublyLinkedList/DoublyLinkedList/obj/Debug/DoublyLinkedList.exe differ diff --git a/DoublyLinkedList/DoublyLinkedList/obj/Debug/DoublyLinkedList.pdb b/DoublyLinkedList/DoublyLinkedList/obj/Debug/DoublyLinkedList.pdb new file mode 100644 index 0000000..b240034 Binary files /dev/null and b/DoublyLinkedList/DoublyLinkedList/obj/Debug/DoublyLinkedList.pdb differ