【发布时间】:2014-10-14 02:03:43
【问题描述】:
这是我目前所拥有的。我已经生成了一个链表,但是当程序打印出链表时,会弹出一个窗口,它说我的程序已经停止工作。我正在使用视觉工作室。我需要使用链表实现一个队列。我可以创建它并将其打印出来,但是当它打印出来时,程序就会停止。我已经尝试了一切,但我似乎无法让这个错误消失。当我尝试在链表类中使用其他方法但我没有包含这些方法时也会发生这种情况。
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
【问题讨论】:
-
在 PrintNodes 中,您正在检查前面!=null。而你正在改变打印。
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
标签: c# list linked-list