Doubly Linked List Upload

This commit is contained in:
lukejelse04 2021-05-13 22:08:58 +01:00
parent 6dba2935d4
commit 7707a63373
7 changed files with 477 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>

View File

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D7477EF3-B68D-47A5-BFA0-8FDD9354C99C}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>DoublyLinkedList</RootNamespace>
<AssemblyName>DoublyLinkedList</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="LinkedList.cs" />
<Compile Include="Node.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -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");
}
}
}

View File

@ -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;
}
}
}

View File

@ -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();
}
}
}

View File

@ -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")]