C# 使用Solid Principles-C的Linkedlist#

C# 使用Solid Principles-C的Linkedlist#,c#,linked-list,text-files,singly-linked-list,C#,Linked List,Text Files,Singly Linked List,我在下面附上了我的链表代码,在这里所有的方法都在一个类中声明,现在我正在重构我的代码,所以我将我的方法移动到单独的类中,在将我的数据移动到单独的类中之后,我的数据没有被传递,因此我的数据没有被显示,我认为我通过变量将数据传递给其他类的方式就是问题发生的地方,我不知道如何纠正这一点..这里我附上了我的代码 这是我的密码: //My node class: public class Node { public int info; public Node link; public Nod

我在下面附上了我的链表代码,在这里所有的方法都在一个类中声明,现在我正在重构我的代码,所以我将我的方法移动到单独的类中,在将我的数据移动到单独的类中之后,我的数据没有被传递,因此我的数据没有被显示,我认为我通过变量将数据传递给其他类的方式就是问题发生的地方,我不知道如何纠正这一点..这里我附上了我的代码

这是我的密码:

//My node class:
public  class Node
{
  public int info;
  public Node link;
  public Node(int i)
  {
    info = i;
    link = null;
  }
} 
// My class to print the data:
public class Display
{
 public void DispalyList()//list to display the items
 {
  Node p;
  SingleLinkedList lst = new SingleLinkedList();
  if (lst.start == null) // I have a doubt whether calling the start variable using that object is not correct., If data is being passed here I can proceed further
  {
   Console.WriteLine("List is empty");
   return;
  }
  Console.Write("list is: ");
  p = lst.start; //The way of calling the start object is wrong I guess
  while (p != null) // p value is null here
  {
   Console.Write(p.info + " ");
   p = p.link;
  }
   Console.WriteLine();
  }
}
// My Single Linked list class://for time being i have two methods in this class
public class SingleLinkedList
{
 public Node start;
 public SingleLinkedList()//constructor
 {
  start = null;
 }
 public void CreateList() //Method  where I am inserting the data
 {
  int i, n, data;//variable declaration
  Console.Write("enter the number of nodes");
  n = Convert.ToInt32(Console.ReadLine());
  if (n == 0)
  return;
  for (i = 1; i <= n; i++)
  {
   Console.WriteLine("enter the element to be inserted");
   data = Convert.ToInt32(Console.ReadLine());
   InsertAtEnd(data);
  }
}
public void InsertAtEnd(int data)//Method to insert the data into the list
{
 Node p;
 Node temp = new Node(data);
 if (start == null)
 {
  start = temp;
  return;
 }
  p = start;
  while (p.link != null)
  p = p.link;
  p.link = temp;
}
}
}  
//Main class using switch case:// I am just calling the display method alone for time being
public class Program
{
 static void Main(string[] args)//Main program calling the methods
 {
  int choice;
  SingleLinkedList list = new SingleLinkedList();
  list.CreateList();// calling the createlist method
  while (true)
  {
   Console.WriteLine("1.Display List");
   Console.WriteLine("Enter your choice");
   choice = Convert.ToInt32(Console.ReadLine());
   if (choice == 13) //I have 13 cases for time being I have shown only one below
   break;
   switch(choice)//Switch case calling the methods
   {
    case 1:
    Display dis = new Display();
    dis.DispalyList();//calling display method which prints the data 
    break;
    default://default case
    Console.WriteLine("Wrong Choice");
   }
 }
//我的节点类:
公共类节点
{
公共信息;
公共节点链路;
公共节点(int i)
{
info=i;
link=null;
}
} 
//我的类打印数据:
公开课展示
{
public void DispalyList()//用于显示项目的列表
{
节点p;
SingleLinkedList lst=新的SingleLinkedList();
if(lst.start==null)//我怀疑使用该对象调用start变量是否不正确。如果在此处传递数据,我可以继续
{
Console.WriteLine(“列表为空”);
返回;
}
控制台。写入(“列表为:”);
p=lst.start;//我想调用start对象的方法是错误的
而(p!=null)//p值在这里为null
{
Console.Write(p.info+“”);
p=p.link;
}
Console.WriteLine();
}
}
//我的单链表class://for 目前我在这门课上有两种方法
公共类单链接列表
{
公共节点启动;
公共SingleLinkedList()//构造函数
{
start=null;
}
public void CreateList()//插入数据的方法
{
int i,n,data;//变量声明
Console.Write(“输入节点数”);
n=Convert.ToInt32(Console.ReadLine());
如果(n==0)
返回;

对于(i=1;i我建议改用
ReadAllLines()
,它将返回一组字符串,输入中每行一个:

public class ReadingTextFile: IReadingTextFile {
  public IEnumerable<string> content() {                  // change the return type
   string path = @ "C:\Users\s\Desktop\Datas\Data Input.txt";
   var data = File.ReadAllLines(path);                    // and this
   return data;
  }
 }
或者,如果您仍然需要一个链接列表,则可以使用接受集合的LinkedList构造函数:

 public class Readingtolist {
  public void Input() {
   IReadingTextFile file = new ReadingTextFile();
   Stopwatch sw = new Stopwatch();

   sw.Start();
   IEnumerable<string> inp = file.content();    // the reading the file is probably what you really want to time.
   sw.Stop();

   var data = new LinkedList<string>(inp);    // note the use of (inp) here
   Console.Write("\n time Taken For Read Data: {0} ms",
    sw.Elapsed.TotalMilliseconds);
   Console.WriteLine("\n The items are{0}", inp);
  }
 }
公共类Readingtolist{
公共无效输入(){
IReadingTextFile=new ReadingTextFile();
秒表sw=新秒表();
sw.Start();
IEnumerable inp=file.content();//读取文件可能是您真正想要的时间。
sw.Stop();
var data=newlinkedlist(inp);//注意这里(inp)的用法
Console.Write(“\n读取数据所用的时间:{0}ms”,
总运行时间(毫秒);
WriteLine(“\n项为{0}”,inp);
}
}

@njenson感谢它的成功。但是,如果我也想使用node,这意味着每次阅读时我都要如何处理它,因为它显示了不同的时间。为什么我不这么做understand@ShaktiS它将根据您运行测试时计算机正在运行的其他程序或操作而有所不同。您的程序与计算机上的其他人竞争相同的资源。
 public class Readingtolist {
  public void Input() {
   IReadingTextFile file = new ReadingTextFile();
   Stopwatch sw = new Stopwatch();

   sw.Start();
   IEnumerable<string> inp = file.content();    // the reading the file is probably what you really want to time.
   sw.Stop();

   var data = new LinkedList<string>(inp);    // note the use of (inp) here
   Console.Write("\n time Taken For Read Data: {0} ms",
    sw.Elapsed.TotalMilliseconds);
   Console.WriteLine("\n The items are{0}", inp);
  }
 }