欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现的简单链表类实例

程序员文章站 2023-12-01 23:20:34
本文实例讲述了c#实现的简单链表类。分享给大家供大家参考。具体如下: 一、关于c#链表 c#链表可用类linkedlist来存放。本文中的类mylinkedlist只是...

本文实例讲述了c#实现的简单链表类。分享给大家供大家参考。具体如下:

一、关于c#链表

c#链表可用类linkedlist来存放。本文中的类mylinkedlist只是实现了该类的最基本的功能。c#中没有指针,但因为c#中类在赋值时传递的是地址,因此仍然可以利用这点制作一个链表。

二、结点类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!");
    }
  }
}

三、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();
}

四、运行结果

C#实现的简单链表类实例

希望本文所述对大家的c#程序设计有所帮助。