【问题标题】:Implementation of Queue using Linked List- It creates the Linked List but stops working after it prints使用链接列表实现队列 - 它创建链接列表但在打印后停止工作
【发布时间】: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

【问题讨论】:

标签: c# list linked-list


【解决方案1】:

你的程序在这里运行到一个无限循环:

Node print = front;
while (front != null)
{
    Console.WriteLine(print.data);
    print = print.next;
}

front 永远不会为空。 你应该有 print!=null。

【讨论】:

猜你喜欢
  • 2023-03-21
  • 1970-01-01
  • 2017-10-29
  • 1970-01-01
  • 2017-08-16
  • 1970-01-01
  • 1970-01-01
  • 2017-03-09
相关资源
最近更新 更多