程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成的簡略鏈表類實例

C#完成的簡略鏈表類實例

編輯:C#入門知識

C#完成的簡略鏈表類實例。本站提示廣大學習愛好者:(C#完成的簡略鏈表類實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成的簡略鏈表類實例正文


本文實例講述了C#完成的簡略鏈表類。分享給年夜家供年夜家參考。詳細以下:

1、關於C#鏈表

C#鏈表可用類LinkedList來寄存。本文中的類MyLinkedList只是完成了該類的最根本的功效。C#中沒有指針,但由於C#中類在賦值時傳遞的是地址,是以依然可以應用這點制造一個鏈表。

2、結點類Node和鏈表類MyLinkedList代碼

/// <summary>
/// 鏈表結點
/// </summary>
class Node
{
  //結點數據,前後結點
  public object Data;
  public Node PreviousNode;
  public Node NextNode;
  //結構函數
  public Node(object data = null)
  {
    Data = data;
    PreviousNode = null;
    NextNode = null;
  }
  //輸入結點信息
  public override string ToString()
  {
    return Data.ToString();
  }
}
/// <summary>
/// 鏈表類
/// </summary>
class MyLinkedList
{
  //首結點、尾結點
  public Node First;
  public Node Last;
  //下一個結點、上一個結點
  public Node NextNode(Node n) { return n.NextNode; }
  public Node PreviousNode(Node n) { return n.PreviousNode; }
  //結點總數
  public int Count;
  //結構函數
  public MyLinkedList()
  {
    this.First = null;
    this.Last = null;
    Count = 0;
  }
  /// <summary>
  /// 在結點node1以後增長結點node2,假如沒有該結點則在最初增長
  /// </summary>
  /// <param name="node1">結點1</param>
  /// <param name="node2">結點2</param>
  public void AddAfter(Node node1, Node node2)
  {
    //鏈表為空的情形
    if (First == null)
    {
      Console.WriteLine("Linked-list is null! Can not find node1(" + node1 + ")");
      return;
    }
    Node temp = First;
    do
    {
      if (temp.Data.Equals(node1.Data))
      {
        //假如node1是尾結點
        if (node1.NextNode == null)
        {
          node2.NextNode = null;
          node2.PreviousNode = node1;
          node1.NextNode = node2;
        }
        else //假如node1不是尾結點
        {
          node2.NextNode = node1.NextNode;
          node2.PreviousNode = node1;
          node2.NextNode.PreviousNode = node2;
          node1.NextNode = node2; ;
        }
        Count++;
        Console.WriteLine("Node(" + node2 + "): Add Complete!");
        return;
      }
      temp = temp.NextNode;
    }
    while (temp != null);
    Console.WriteLine("Can not find node(" + node1 + "), Misson defeat");
  }
  /// <summary>
  /// 在鏈表尾部增長結點
  /// </summary>
  public void AddLast(Node node)
  {
    //鏈表為空的情形
    if (this.First == null) 
    {
      node.NextNode = null;
      node.PreviousNode = null;
      this.First = node;
      this.Last = node;
    }
    else //鏈表不為空的情形
    {
      Node temp = First;
      while(temp.NextNode != null)
      {
        temp = temp.NextNode;
      }
      temp.NextNode = node;
      node.PreviousNode = temp;
      Last = node;
    }
    Count++;
    Console.WriteLine("Node(" + node + "): Add Complete!");
  }
  /// <summary>
  /// 刪除指定結點
  /// </summary>
  /// <param name="node">被刪除結點</param>
  public void Delete(Node node)
  {
    if (Count == 0)
    {
      Console.WriteLine("Can not find node(" + node + ")");
      return;
    }
    Node temp = First;
    do
    {
      //假如數據部門婚配,則刪去該結點
      if (temp.Data.Equals(node.Data))
      {
        //temp是尾結點
        if (temp.NextNode == null)
        {
          temp.PreviousNode.NextNode = null;
          temp = null;
        }
        else //temp不是尾結點 
        {
          temp.PreviousNode.NextNode = temp.NextNode;
          temp.NextNode.PreviousNode = temp.PreviousNode;
          temp = null;
        }
        Count--;
        Console.WriteLine("Node(" + node + "): Delete Complete!");
        return;
      }
      temp = temp.NextNode;
    }
    while (temp != null);
    Console.WriteLine("Can not find node(" + node + "), Misson defeat");
  }
  /// <summary>
  /// 修正結點值
  /// </summary>
  /// <param name="node">被修正結點</param>
  /// <param name="value">結點值</param>
  public void Modify(Node node, object value)
  {
    if (Count == 0)
    {
      Console.WriteLine("Can not find node(" + node + ")");
      return;
    }
    Node temp = First;
    do
    {
      if (temp.Data.Equals(node.Data))
      {
        Console.WriteLine("Node: " + temp.Data + " → " + value.ToString());
        temp.Data = value;
        return;
      }
      temp = temp.NextNode;
    }
    while (temp != null);
  }
  /// <summary>
  /// 打印鏈表
  /// </summary>
  public void Print()
  {
    if (First == null)
    {
      Console.WriteLine("No nodes in this linked-list.");
      return;
    }
    else
    {
      Console.WriteLine("Print the linked-list...");
      Node temp = First;
      do
      {
        Console.WriteLine(temp.ToString());
        temp = temp.NextNode;
      }
      while (temp != null);
      Console.WriteLine("Mission Complete!");
    }
  }
}

3、Main函數的挪用示例

static void Main(string[] args)
{
  MyLinkedList ll = new MyLinkedList();
  //添加三個結點 1 2(在1後) 3(在2後)
  Node n1 = new Node("node1");      
  Node n2 = new Node("node2");
  Node n3 = new Node("node3");
  ll.AddLast(n1);
  ll.AddLast(n2);
  ll.AddLast(n3);
  //添加三個結點 1.5(在1後) 2.5(在2後) 3.5(在3後)
  Node n1dot5 = new Node("node1dot5");
  Node n2dot5 = new Node("node2dot5");
  Node n3dot5 = new Node("node3dot5");
  ll.AddAfter(n1, n1dot5);
  ll.AddAfter(n2, n2dot5);
  ll.AddAfter(n3, n3dot5);
  Console.WriteLine("========================");
  //打印鏈表
  ll.Print();
  Console.WriteLine("========================");
  //刪除結點 2 和 3,將結點 2.5 的值改成 "ThisNodeIsModified!"
  ll.Delete(n2);
  ll.Delete(n3);
  ll.Modify(n2dot5, "ThisNodeIsModified!");
  Console.WriteLine("========================");
  //打印鏈表
  ll.Print();
  Console.ReadLine();
}

4、運轉成果

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved