以下是我迄今为止所拥有的。我已经生成了一个链表,但当程序打印出链表时,会出现一个弹出窗口,显示我的程序已停止工作。我正在使用visual studio。我需要使用链表实现一个队列。我可以创建它并打印出来,但当它打印出来时,程序就会停止。我已经尝试了所有的方法,但我似乎无法消除这个错误。当我尝试在链表类中使用其他方法,但没有包括这些方法时,也会发生这种情况。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace CSCI3230_Project2_Zack_Davidson
{
class Program
{
static void Main(string[] args)
{
int count = 0;
LinkedList list = new LinkedList();
for (int i = 1; i <= 20; i++)
{
list.Add(new Node(i));
count++;
}
Console.WriteLine("I have generated a queue with 20 elements. I have printed the queue below.");
list.PrintNodes();
}
}
public class Node
{
public Node next;
public int data;
public Node(int val)
{
data = val;
next = null;
}
}
public class LinkedList
{
public TimeSpan runTimer;
public System.Diagnostics.Stopwatch
stopwatch = new System.Diagnostics.Stopwatch();
Node front;
Node current;
public void Add(Node n)
{
if (front == null)
{
front = n;
current = front;
}
else
{
current.next = n;
current = current.next;
}
}
public void PrintNodes()
{
Node print = front;
while (front != null)
{
Console.WriteLine(print.data);
print = print.next;
}
/*while (front != null)
{
Console.WriteLine(front.data);
front = front.next;
}*/
}
}//EndofNameSpace
您的程序运行到一个无限循环中:
Node print = front;
while (front != null)
{
Console.WriteLine(print.data);
print = print.next;
}
front永远不会为空。你应该有印刷品=无效的