简单列表合并 C#(无泛型)
Simple List Merge C# (Without Generics)
我从头开始创建了一个简单的列表 class。这是我已经完成了大约一周的 class 作业 - 对列表来说非常新。我们不能使用泛型,所以试图研究我下面的问题并没有取得成果。虽然我确实通过 BetterCoder 在 youtube 上观看了 7 个教程,并且我在我的书中找到了一些东西,但没有找到 "merging".
的示例
我有三个 classes - 我的节点、我的列表和我的程序。在我的列表 class 中,我正在构建一个 Merge() 方法,该方法最终将比较两个列表中的数据并将它们合并到一个有序列表中。
现在由于某种原因,我的 Merge 方法——这是帮助我理解正在发生的事情的非常基本的方法——无法正常工作。它已将两个列表都传递给它,并且正在将数据从 listTwo 添加到 listOne 但是由于某种原因,当它打印到控制台时,第二个节点的数据显示两次:
EX: 1 -> 2 -> 2
而不是打印头部 (1)、下一个 (2) 和下一个 (3)。
EX: 1 -> 2 -> 3
在程序 class 中,我已经用 (listOne.firstNode.Next.Next.Data) = 3 的写入行证明了。应该是这样。
谁能帮我弄清楚列表一中的节点是否没有正确指向彼此或发生了什么?
我的合并方法必须传递两个列表对象(listOne 和 listTwo),最终我需要将它们作为引用传递,但我还没有完全弄清楚,稍后会重点讨论我想。
namespace LinkedList
{
//This is my Node Class
class Node
{
public object Data { get; set; }
public Node Next { get; set; }
public Node(object dataValue) : this(dataValue, null) { }
public Node(object dataValue, Node nextNode)
{
Data = dataValue;
Next = nextNode;
}
}
//This is my List Class
class List
{
public Node firstNode;
public int count;
public List()
{
firstNode = null;
}
public bool Empty
{
get { return this.count == 0; }
}
public int Count
{
get { return this.count; }
}
public object Add(int index, object o)
{
if (index < 0)
throw new ArgumentOutOfRangeException("Index: " + index);
if (index > count)
index = count;
Node current = this.firstNode;
if (this.Empty || index == 0)
{
this.firstNode = new Node(o, this.firstNode);
}
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 Merge(List a, List b)
{
a.Add(b.firstNode.Data);
return a;
}
public void Print()
{
while (this.count > 0)
{
Console.Write(firstNode.Data + "->");
if(firstNode.Next != null)
firstNode.Data = firstNode.Next.Data;
count--;
}
}
}
//And here is my Program
class Program
{
static void Main(string[] args)
{
List listOne = new List();
List listTwo = new List();
listOne.Add(1);
listOne.Add(2);
listTwo.Add(3);
listTwo.Print();
Console.WriteLine("");
listOne.Merge(listOne, listTwo);
Console.WriteLine("");
listOne.Print();
//This line below shows that the data "3" from listTwo is being added to listOne in the list Merge Method
//Console.WriteLine(listOne.firstNode.Next.Next.Data);
Console.ReadKey();
}
}
}
我会这样写:
public void Merge(List b)
{
Node lastNode = GetLastNode();
if (lastNode != null)
{
lastNode.Next = b.firstNode;
}
else
{
this.firstNode = b.firstNode;
}
}
// this method is used to find the last node in current list
private Node GetLastNode()
{
if (this.firstNode == null)
{
return null;
}
Node current = this.firstNode;
while (current.Next != null)
{
current = current.Next;
}
return current;
}
首先,我将 Merge 的签名从 public object Merge(List a, List b)
更改为 public void Merge(List b)
。现在我们可以这样使用它了:
listOne.Merge(listTwo);
这将 link listOne 的最后一个元素与 listTwo 的第一个元素合并。
现在我们需要更改 Print
方法,因为当前版本修改了列表,这不应该发生:
public void Print()
{
Node currentNode = this.firstNode;
while(currentNode != null)
{
Console.Write(currentNode.Data + ' ');
currentNode = currentNode.Next;
}
}
您的打印方法存在实际问题
public void Print()
{
Node node = firstNode;
for (int i = 0; i < this.count; i++)
{
Console.Write(node.Data + "->");
if (node.Next != null)
node = node.Next;
}
}
Alex Sikilinda,你是对的 merge
方法不完整。
public object Merge(List a, List b)
{
Node bNode = b.firstNode;
while (bNode != null)
{
a.Add(bNode.Data);
bNode = bNode.Next;
}
return a;
}
我没有将数据分配回第一个节点,而是分配了
firstNode = firstNode.Next;
请检查下面的打印代码
public void Print()
{
while (this.count > 0)
{
Console.Write(firstNode.Data + "->");
if (firstNode.Next != null)
firstNode = firstNode.Next;
count--;
}
}
我从头开始创建了一个简单的列表 class。这是我已经完成了大约一周的 class 作业 - 对列表来说非常新。我们不能使用泛型,所以试图研究我下面的问题并没有取得成果。虽然我确实通过 BetterCoder 在 youtube 上观看了 7 个教程,并且我在我的书中找到了一些东西,但没有找到 "merging".
的示例我有三个 classes - 我的节点、我的列表和我的程序。在我的列表 class 中,我正在构建一个 Merge() 方法,该方法最终将比较两个列表中的数据并将它们合并到一个有序列表中。
现在由于某种原因,我的 Merge 方法——这是帮助我理解正在发生的事情的非常基本的方法——无法正常工作。它已将两个列表都传递给它,并且正在将数据从 listTwo 添加到 listOne 但是由于某种原因,当它打印到控制台时,第二个节点的数据显示两次:
EX: 1 -> 2 -> 2
而不是打印头部 (1)、下一个 (2) 和下一个 (3)。
EX: 1 -> 2 -> 3
在程序 class 中,我已经用 (listOne.firstNode.Next.Next.Data) = 3 的写入行证明了。应该是这样。
谁能帮我弄清楚列表一中的节点是否没有正确指向彼此或发生了什么?
我的合并方法必须传递两个列表对象(listOne 和 listTwo),最终我需要将它们作为引用传递,但我还没有完全弄清楚,稍后会重点讨论我想。
namespace LinkedList { //This is my Node Class class Node { public object Data { get; set; } public Node Next { get; set; } public Node(object dataValue) : this(dataValue, null) { } public Node(object dataValue, Node nextNode) { Data = dataValue; Next = nextNode; } } //This is my List Class class List { public Node firstNode; public int count; public List() { firstNode = null; } public bool Empty { get { return this.count == 0; } } public int Count { get { return this.count; } } public object Add(int index, object o) { if (index < 0) throw new ArgumentOutOfRangeException("Index: " + index); if (index > count) index = count; Node current = this.firstNode; if (this.Empty || index == 0) { this.firstNode = new Node(o, this.firstNode); } 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 Merge(List a, List b) { a.Add(b.firstNode.Data); return a; } public void Print() { while (this.count > 0) { Console.Write(firstNode.Data + "->"); if(firstNode.Next != null) firstNode.Data = firstNode.Next.Data; count--; } } } //And here is my Program class Program { static void Main(string[] args) { List listOne = new List(); List listTwo = new List(); listOne.Add(1); listOne.Add(2); listTwo.Add(3); listTwo.Print(); Console.WriteLine(""); listOne.Merge(listOne, listTwo); Console.WriteLine(""); listOne.Print(); //This line below shows that the data "3" from listTwo is being added to listOne in the list Merge Method //Console.WriteLine(listOne.firstNode.Next.Next.Data); Console.ReadKey(); } } }
我会这样写:
public void Merge(List b)
{
Node lastNode = GetLastNode();
if (lastNode != null)
{
lastNode.Next = b.firstNode;
}
else
{
this.firstNode = b.firstNode;
}
}
// this method is used to find the last node in current list
private Node GetLastNode()
{
if (this.firstNode == null)
{
return null;
}
Node current = this.firstNode;
while (current.Next != null)
{
current = current.Next;
}
return current;
}
首先,我将 Merge 的签名从 public object Merge(List a, List b)
更改为 public void Merge(List b)
。现在我们可以这样使用它了:
listOne.Merge(listTwo);
这将 link listOne 的最后一个元素与 listTwo 的第一个元素合并。
现在我们需要更改 Print
方法,因为当前版本修改了列表,这不应该发生:
public void Print()
{
Node currentNode = this.firstNode;
while(currentNode != null)
{
Console.Write(currentNode.Data + ' ');
currentNode = currentNode.Next;
}
}
您的打印方法存在实际问题
public void Print()
{
Node node = firstNode;
for (int i = 0; i < this.count; i++)
{
Console.Write(node.Data + "->");
if (node.Next != null)
node = node.Next;
}
}
Alex Sikilinda,你是对的 merge
方法不完整。
public object Merge(List a, List b)
{
Node bNode = b.firstNode;
while (bNode != null)
{
a.Add(bNode.Data);
bNode = bNode.Next;
}
return a;
}
我没有将数据分配回第一个节点,而是分配了
firstNode = firstNode.Next;
请检查下面的打印代码
public void Print()
{
while (this.count > 0)
{
Console.Write(firstNode.Data + "->");
if (firstNode.Next != null)
firstNode = firstNode.Next;
count--;
}
}