【发布时间】:2020-06-28 17:10:58
【问题描述】:
我已经尝试创建链表/节点类,但我不确定下一步该去哪里。我的尝试并不顺利,因为在创建类之后我只是不确定下一步是什么。
我正在尝试创建一个具有恐龙节点的程序,该节点保存有关恐龙的信息,例如 id、物种等,我希望允许用户从列表中创建和删除恐龙。所以我需要允许用户输入数据,我假设有一种方法可以让恐龙 id 自动设置,但我不确定。
我已经包含了LinkedList.cs 和Node.cs,所以你可以看到我要去哪里,但我不知道在我的程序类中做什么来利用链接列表并实现我想要的做。
添加了Program.cs 类以帮助识别/显示我在程序中的位置/我需要做什么。
链表类:
using System;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using System.Text;
namespace JurrasicFinal
{
public class LinkedList
{
private Node head;
private int count;
public LinkedList()
{
this.head = null;
this.count = 0;
}
public bool Empty
{
get { return this.count == 0; }
}
public int Count
{
get { return this.count; }
}
public object this[int index]
{
get { return this.Get(index); }
}
public object Add(int index, object o)
{
if (index < 0)
throw new ArgumentOutOfRangeException("Index: " + index);
if (index > count)
index = count;
Node current = this.head;
if (this.Empty || index == 0)
{
this.head = new Node(o, this.head);
}
else
{
for (int i = 0; i < index - 1; i++)
{
current = current.Next;
current.Next = new Node(o, current.Next);
}
}
count++;
return o;
}
public object Add(object o)
{
return this.Add(count, o);
}
public object Remove(int index)
{
if (index < 0)
throw new ArgumentOutOfRangeException("Index: " + index);
if (this.Empty)
return null;
if (index >= this.count)
index = count - 1;
Node current = this.head;
object result = null;
if (index == 0)
{
result = current.Data;
this.head = current.Next;
}
else
{
for (int i = 0; index < index - 1; i++) ;
current = current.Next;
result = current.Next.Data;
current.Next = current.Next.Next;
}
count--;
return result;
}
public void Clear()
{
this.head = null;
this.count = 0;
}
public int IndexOf(object o)
{
Node current = this.head;
for (int i = 0; i < this.count; i++)
{
if (current.Data.Equals(o))
return i;
current = current.Next;
}
return -1;
}
public bool Contains(object o)
{
return this.IndexOf(o) >= 0;
}
public object Get(int index)
{
if (index < 0)
throw new ArgumentOutOfRangeException("Index: " + index);
if (this.Empty)
return null;
if (index >= this.count)
index = this.count - 1;
Node current = this.head;
for (int i = 0; i < index; i++)
current = current.Next;
return current.Data;
}
}
}
节点类:
using System;
using System.Collections.Generic;
using System.Text;
namespace JurrasicFinal
{
public class Node
{
private object data;
private Node next;
private string DinoSpecies;
private string DinoName;
public Node(object data, Node next)
{
this.data = data;
this.next = next;
}
public object Data
{
get { return this.data; }
set { this.data = value; }
}
public Node Next
{
get { return this.next; }
set { this.next = value; }
}
}
}
程序类:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq.Expressions;
namespace JurrasicFinal
{
class Program
{
class Dinosaur
{
public string Name;
public string Classification;
public char Sex;
}
static void Main(string[] args)
{
LinkedList<Dinosaur> DinoList = new LinkedList<Dinosaur>();
Dinosaur Dino1 = new Dinosaur();
Dino1.Name = "Tyrannosaurus Rex";
Dino1.Classification = "Carnivorous";
Dino1.Sex = 'M';
Dinosaur Dino2 = new Dinosaur();
Dino2.Name = "Velociraptor";
Dino2.Classification = "Carnivorous";
Dino2.Sex = 'F';
Dinosaur Dino3 = new Dinosaur();
Dino3.Name = "Procompsognathus";
Dino3.Classification = "Carnivorous";
Dino3.Sex = 'M';
void printList()
{
Console.WriteLine("Current Queue: ");
Console.WriteLine("\n");
foreach (Dinosaur d in DinoList)
{
Console.WriteLine("Name: " + d.Name);
Console.WriteLine("Classification: " + d.Classification);
Console.WriteLine("Sex " + d.Sex);
Console.WriteLine("\n");
}
Console.WriteLine(Dino1.Name + Dino1.Sex);
}
DinoList.AddLast(Dino1);
DinoList.AddLast(Dino2);
DinoList.AddLast(Dino3);
printList();
Console.WriteLine(DinoList.Count);
FileStream fileStream = File.OpenWrite("E:/Work/Dinosaur.txt");
BinaryWriter writer = new BinaryWriter(fileStream);
foreach (Dinosaur d in DinoList)
{
writer.Write(d.Name);
writer.Write(d.Classification);
writer.Write(d.Sex);
}
writer.Close();
Console.WriteLine("Reading Back From File");
FileStream file = File.OpenRead("E:/Work/Dinosaur.txt");
BinaryReader reader = new BinaryReader(file);
for (int i = 1; i < 3; i++)
{
Dinosaur d = new Dinosaur();
d.Name = reader.ReadString();
d.Classification = reader.ReadString();
d.Sex = reader.ReadChar();
DinoList.AddLast(d);
}
reader.Close();
Console.ReadKey();
}
}
}
【问题讨论】:
-
这是一种运动吗?如果没有,为什么不使用已经提供的
LinkedList<T>? -
是的,这是一种锻炼。我之前尝试过使用`LinkedList
`,但也不知道我在做什么,我需要在不使用C#中已经提供的情况下实现它。我按照教程创建了链接列表和节点类,现在我只是不确定如何使用它来完成我想要实现的目标。 -
I'm just not sure how to use it to do what I'm trying to achieve。好的,那你想达到什么目的?标准的List不起作用吗?通常,当您不了解某件事的工作原理时,一般建议不会是“尝试编写自己的实现”(除非练习是编写自己的实现) -
我正在尝试从用户那里获取用户输入的dinoName,其中dinoID 将自动设置,对于将创建的每个新恐龙,其增量为1>2>3 等。我希望用户能够决定它在链接列表中的存储位置,以便他们可以将其放在开头、中间、结尾等位置,并且他们还应该能够从列表中的任何位置删除。我的任务是使用 LinkedList 并实现我自己的 LinkedList,如果我不必使用它,我就不会
-
我猜这是编程课程中令人讨厌的入门级任务之一,这是一篇带有解释的文章dzone.com/articles/…。我建议您使用泛型参数来实现这一点,因为它们在 C# 中很重要,而且实现起来并不难。
标签: c# linked-list nodes user-input