熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

一道求單向鏈表倒數第N個結點的算法題

2022-06-13   來源: .NET編程 
     namespace LinkedList
      {
           class Program
           {
               static void Main(string[] args)
               {
     
     
                   while (true)
                  {
                      ConsoleWriteLine(請輸入你需要得到倒數第幾個元素最大為輸入其它程序將出錯);
                      string iNum = ConsoleReadLine()ToString();
                      if (iNum ==Q)
                      {
                          return ;
                      }
                      int index = ConvertToInt(iNum);
    
                      Node node = new Node();
                      LinKHead link = new LinKHead(node);
                      for (int i = ; i (i);
                          node = nodeNexNode;
                      }
    
                      ConsoleWriteLine( 倒數第N個元素為+linkGetNode(index true)ValueToString());
                      ConsoleWriteLine(退出請按輸入Q繼續測試請輸入測試位置Num);
    
                  }
              }
          }
          class Node
          {
    
              private T value;
    
              public T Value
              {
                  get { return thisvalue; }
                  set { thisvalue = value; }
              }
              private Node nextNode;
    
              public Node NexNode
              {
                  get { return nextNode; }
                  set { nextNode = value; }
              }
              public Node(T value)
              {
                  thisvalue = value;
              }
          }
          class LinKHead
          {
              private Node headNode;
    
              internal Node HeadNode
              {
                  get { return headNode; }
                  set { headNode = value; }
              }
    
              public LinKHead(Node node)
              {
                  headNode = node;
              }
              public Node GetValue(Node node Node node)
              {
                  while (true)
                  {
                      if (nodeNexNode == null)
                      {
                          return node;
                      }
                      node = nodeNexNode;
                      node = nodeNexNode;
                  }
              }
              public Node GetNode(int index bool flag)
              {
                  if (!flag)
                  {
                      return null;
                  }
                  else
                  {
                      Node node = headNode;
                      Node node node;
                      node = node = headNode;
                      int p p tmp;
                      p = p = tmp = ;
                      while (true)
                      {
                          if (nodeNexNode == null)
                          {
                              p = tmp;
                             node = node;
                             return GetValue(node node);
                         }
                         else if (p tmp == index)
                         {
                             node = node;
                             p = tmp;
                             node = node;
                             tmp = p;
                             p++;
   
                             node = nodeNexNode;
                         }
                         else
                         {
   
                             p++;
                             node = nodeNexNode;
   
                         }
   
                     }
   
                 }
             }
         }
    }

From:http://tw.wingwit.com/Article/program/net/201311/11676.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.