Я реализую связанные списки. Я пытаюсь кодировать различные типы функций, которые я могу использовать для меня. Причина, по которой я не использую встроенный список <>(), - это из-за моего требования моей лабораторной работы по внедрению связанных списков, которые мы будем использовать для любых дополнительных функций.Связанные списки C#, используя обработку исключений для проверки условий во время удаления узла
Мои текущие функции включают печать одного узла, печать всех узлов, добавление узла в конце, замену узла и одного, над которым я сейчас работаю, удаление/удаление узла. (Кредиты для youtuber Jeff Chastine Я в основном использовал то, что он сделал в своем учебнике)
Я надеялся, что другие люди будут смотреть на то, что я делаю в узле delete (в LinkedList.cs), потому что я чувствую, что использую исключение обработка не может быть правильным путем.
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LinkedListImplementation
{
class Program
{
static void Main(string[] args)
{
LinkedList myList = new LinkedList();
myList.append(3);
myList.append(14);
myList.append(14);
myList.append(34);
myList.append(89);
myList.append(556);
myList.print();
myList.deleteNode(1);
myList.print();
//myList.replaceData(5, 80);
Console.ReadLine();
}
}
}
Node.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LinkedListImplementation
{
class Node
{
public int data;
public Node nextNode;
public Node(int data)
{
this.data = data;
nextNode = null;
}
public void singleDisplay(int data)
{
if(nextNode != null)
{
if (this.data == data)
{
Console.WriteLine("My data {0}", data);
}
nextNode.singleDisplay(data);
}
if (nextNode == null)
{
if(this.data != data)
{
Console.WriteLine("Value not found");
}
else
{
Console.WriteLine("My data {0}", data);
}
}
}
public void completeDisplay()
{
if(nextNode != null)
{
Console.Write(data + " -> ");
nextNode.completeDisplay();
}
else
{
Console.WriteLine(data);
}
}
public void addNodeAtEnd(int data)
{
if(nextNode == null)
{
nextNode = new Node(data);
}
else
{
nextNode.addNodeAtEnd(data);
}
}
public void replaceNodeData(int data, int repData)
{
if(this.data == data)
{
this.data = repData;
}
else
{
if(nextNode == null)
{
Console.WriteLine("Value Not Found");
}
else
{
nextNode.replaceNodeData(data, repData);
}
}
}
}
}
LinkedList.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LinkedListImplementation
{
class LinkedList
{
public Node head;
public LinkedList()
{
head = null;
}
public void append(int data)
{
if (head == null)
{
head = new Node(data);
}
else
{
head.addNodeAtEnd(data);
}
}
public void print()
{
if (head != null)
{
head.completeDisplay();
}
else
{
Console.WriteLine("List Not Found");
}
}
public void singlePrint(int data)
{
if(head != null)
{
head.singleDisplay(data);
}
else
{
Console.WriteLine("List Not Found");
}
}
public void replaceData(int data, int repData)
{
if(head != null)
{
head.replaceNodeData(data, repData);
}
else
{
Console.WriteLine("List Not Found");
}
}
public void deleteNode(int data)
{
Node current = head;
Node temp = new Node(0);
if(current == null)
{
Console.WriteLine("List does not exist");
}
else
{
while (current.data != data)
{
temp = current;
current = current.nextNode;
try
{
if (current.data != data && current == null)
{}
}
catch(Exception)
{
//Console.WriteLine(ex.Message);
break;
}
}
try
{
if (current.data == data && current.nextNode == null)
{
current = temp;
current.nextNode = null;
}
else if (current.data == data && current.nextNode != null)
{
current = current.nextNode;
temp.nextNode = current;
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
отдельный или дважды связанный список удалить реализацию? –
Как не похоже, что вы задаете вопрос, но запрашиваете обзор кода, возможно, это будет хорошим кандидатом для http://codereview.stackexchange.com/ – chadnt
Ах, простите, я не знал об этом. Я обязательно отвечу за него за любые будущие обзоры кода. Спасибо. –