c#-使用链接列表实现队列-创建链接列表,但在打印后停止工作
作者:互联网
这是我到目前为止所拥有的.我已经生成了一个链表,但是当程序打印出链表时,会弹出一个对话框,说我的程序已停止工作.我正在使用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永远不会为空.
您应该打印!=空.
标签:list,linked-list,c 来源: https://codeday.me/bug/20191029/1957081.html