首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Solid Principles的链接表--C#

使用Solid Principles的链接表--C#
EN

Stack Overflow用户
提问于 2017-09-21 20:52:11
回答 1查看 383关注 0票数 0

我有我的代码链表在这里我附加在下面,在这个所有的方法都声明在一个单一的类中,现在我正在重构我的代码,所以我正在移动我的方法分离类,在移动我的数据到独立的类后,我的数据不会被传递,所以我的数据不会被显示,我认为我通过变量传递数据到其他类的方式是问题发生的地方,我不知道如何纠正this..here我已经附加了我的代码。

下面是我的代码:

代码语言:javascript
复制
//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");
   }
 }

}}

下面是我的上述代码,其中DisplayList()方法中的start值将数据传递给null.If。在此方法中,我也可以对其他类执行相同的操作。我不知道如何分配这里的数据。

EN

回答 1

Stack Overflow用户

发布于 2017-09-21 23:09:32

我建议使用ReadAllLines(),它将返回一个字符串集合,在您的输入中每行一个:

代码语言:javascript
复制
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;
  }
 }

如果您需要的只是一个列表,那么您可以只使用file.content()

代码语言:javascript
复制
 IReadingTextFile file = new ReadingTextFile();
 LinkedList < string > data = new LinkedList < string > ();
 IEnumerable<string> inp = file.content();  // this is a collection of strings, one per line of your input
 // ...

或者,如果您仍然需要一个链表,您可以只使用接受集合的LinkedList构造函数:

代码语言:javascript
复制
 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);
  }
 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46344463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档