当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现单链表(线性表)完整实例

C#实现单链表(线性表)完整实例

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#实现单链表(线性表)的方法。分享给大家供大家参考,具体如下:

顺序表由连续内存构成,链表则不同。顺序表的优势在于查找,链表的优势在于插入元素等操作。顺序表的例子:

要注意的是,单链表的add()方法最好不要频繁调用,尤其是链表长度较长的时候,因为每次add,都会从头节点到尾节点进行遍历,这个缺点的优化方法是将节点添加到头部,但顺序是颠倒的。

所以,在下面的例子中,执行purge(清洗重复元素)的时候,没有使用add()方法去添加元素,而是定义一个节点,让它始终指向目标单链表的最后一个节点,这样就不用每次都从头到尾遍历。

此外,链表还可以做成循环链表,即最后一个结点的next属性等于head,主要操作与单链表相似,判断最后一个结点,不是等于null,而是等于head

using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace linearlist
{
  //定义线性表的行为,可供顺序表类和单链表类继承
  public interface ilistds<t>
  {
    int getlength();
    void insert(t item, int i);
    void add(t item);
    bool isempty();
    t getelement(int i);
    void delete(int i);
    void clear();
    int locateelement(t item);
    void reverse();
  }
  //链表节点类
  class node<t>
  {
    private t tdata;
    private node<t> nnext;
    public t data
    {
      get { return this.tdata; }
      set { this.tdata = value; }
    }
    public node<t> next
    {
      get { return this.nnext; }
      set { this.nnext = value; }
    }
    public node()
    {
      this.tdata = default(t);
      this.nnext = null;
    }
    public node(t t)
    {
      this.tdata = t;
      this.nnext = null;
    }
    public node(t t,node<t> node)
    {
      this.tdata = t;
      this.nnext = node;
    }
  }
  //该枚举表示单链表add元素的位置,分头部和尾部两种
  enum addposition {head,tail};
  //单链表类
  class linkedlist<t>:ilistds<t>
  {
    private node<t> thead;//单链表的表头
    public node<t> head
    {
      get { return this.thead; }
      set { this.thead = value; }
    }
    public linkedlist()
    {
      this.thead = null;
    }
    public linkedlist(node<t> node)
    {
      this.thead = node;
    }
    public void add(t item,addposition p)//选择添加位置
    {
      if (p == addposition.tail)
      {
        this.add(item);//默认添加在末尾
      }
      else//从头部添加会节省查找的开销,时间复杂度为o(1)不必每次都循环到尾部,这恰好是顺序表的优点
      {
        node<t> node = this.head;
        node<t> nodetmp = new node<t>(item);
        if (node == null)
        {
          this.head = nodetmp;
        }
        else
        {
          nodetmp.next = node;
          this.thead = nodetmp;
        }
      }
    }
    #region ilistds<t> 成员
    public int getlength()
    {
      node<t> node = new node<t>();
      int count = 0;
      node = this.thead;
      while (node != null)
      {
        count++;
        node = node.next;
      }
      return count;
    }
    public void insert(t item, int i)//i最小从1开始
    {
      node<t> insertnode = new node<t>(item, null);//实例化待添加的node
      if (this.thead == null && i == 1)
      {
        this.thead = insertnode;
        return;
      }
      if (i < 1 || i > this.getlength() || (this.thead == null && i != 1))
      {
        console.writeline("there are no elements in this linked list!");
        return;
      }
      int j = 1;
      node<t> node = this.thead;
      node<t> nodetmp;
      while (node != null && j < i)//循环结束时,保证node为第i个node
      {
        node = node.next;
        j++;
      }
      nodetmp = node.next;//原来的单链表的第i+1个node
      node.next = insertnode;//第i个node后的node修改为待插入的node
      insertnode.next = nodetmp;//待插入的node插入后,其后继node为原来链表的第i+1个node
    }
    public void add(t item)//添加至尾部,时间复杂度为o(n),如果添加至头部,则会节省循环的开销
    {
      node<t> lastnode = new node<t>(item, null);//实例化待添加的node
      if (this.thead == null)
      {
        this.thead = lastnode;
      }
      else
      {
        node<t> node = this.thead;
        while (node.next != null)
        {
          node = node.next;
        }
        node.next = lastnode;
      }
    }
    public bool isempty()
    {
      return this.thead == null;
    }
    public t getelement(int i)//设i最小从1开始
    {
      if (i < 1 || i > this.getlength())
      {
        console.writeline("the location is not right!");
        return default(t);
      }
      else
      {
        if (i == 1)
        {
          return this.thead.data;
        }
        else
        {
          node<t> node = this.thead;
          int j = 1;
          while (j < i)
          {
            node = node.next;
            j++;
          }
          return node.data;
        }
      }
    }
    public void delete(int i)//设i最小从1开始
    {
      if (i < 1 || i > this.getlength())
      {
        console.writeline("the location is not right!");
      }
      else
      {
        if (i == 1)
        {
          node<t> node = this.thead;
          this.thead = node.next;
        }
        else
        {
          node<t> node = this.thead;
          int j = 1;
          while (j < i-1)
          {
            node = node.next;
            j++;
          }
          node.next = node.next.next;
        }
      }
    }
    public void clear()
    {
      this.thead = null;//讲thead设为null后,则所有后继结点由于失去引用,等待gc自动回收
    }
    public int locateelement(t item)//返回值最小从1开始
    {
      if (this.thead == null)
      {
        console.writeline("there are no elements in this linked list!");
        return -1;
      }
      node<t> node = this.thead;
      int i = 0;
      while (node != null)
      {
        i++;
        if (node.data.equals(item))//如果data是自定义类型,则其equals函数必须override
        {
          return i;
        }
        node = node.next;
      }
      console.writeline("no found!");
      return -1;
    }
    public void reverse()
    {
      if (this.thead == null)
      {
        console.writeline("there are no elements in this linked list!");
      }
      else
      {
        node<t> node = this.thead;
        if (node.next == null)//如果只有头节点,则不作任何改动
        {
        }
        else
        {
          node<t> node1 = node.next;
          node<t> node2;
          while (node1 != null)
          {
            node2 = node.next.next;
            node.next = node2;//可以发现node始终未变,始终是原来的那个头节点
            node1.next = this.thead;
            this.thead = node1;
            node1 = node2;
          }
        }
      }
    }
    #endregion
  }
  class program
  {
    static void main(string[] args)
    {
      /*测试单链表的清空
      llist.clear();
      node<int> n = new node<int>();
      n = llist.head;
      while (n != null)
      {
        console.writeline(n.data);
        n = n.next;
      }
      console.readline();
       */
      /*测试单链表返回元素个数
      linkedlist<int> llist = new linkedlist<int>();
      llist.add(3);
      console.writeline(llist.getlength());
      console.readline();
      */
      /*测试单链表插入
      linkedlist<int> llist = new linkedlist<int>();
      llist.insert(0,1);
      llist.add(1);
      llist.add(2);
      llist.add(3);
      llist.add(4);
      llist.insert(99,3);
      node<int> n = new node<int>();
      n = llist.head;
      while (n != null)
      {
        console.writeline(n.data);
        n = n.next;
      }
      console.readline();
      */
      /*测试单链表获取某位置的值
      linkedlist<int> llist = new linkedlist<int>();
      llist.add(1);
      llist.add(2);
      llist.add(3);
      llist.add(4);
      console.writeline(llist.getelement(1));
      console.readline();
       */
      /*测试单链表删除某位置的值
      linkedlist<int> llist = new linkedlist<int>();
      llist.add(1);
      llist.add(2);
      llist.add(3);
      llist.add(4);
      node<int> n = new node<int>();
      n = llist.head;
      while (n != null)
      {
        console.writeline(n.data);
        n = n.next;
      }
      console.readline();
      llist.delete(2);
      node<int> m = new node<int>();
      m = llist.head;
      while (m != null)
      {
        console.writeline(m.data);
        m = m.next;
      }
      console.readline();
      */
       /*测试单链表按值查找元素位置
      linkedlist<int> llist = new linkedlist<int>();
      llist.add(1);
      llist.add(2);
      llist.add(3);
      llist.add(4);
      console.writeline(llist.locateelement(3));
      console.readline();
      */
      /*测试单链表reverse操作(逆序)
      linkedlist<int> llist = new linkedlist<int>();
      llist.add(1);
      llist.add(2);
      llist.add(3);
      llist.add(4);
      llist.add(5);
      node<int> m = new node<int>();
      m = llist.head;
      while (m != null)
      {
        console.writeline(m.data);
        m = m.next;
      }
      console.readline();
      llist.reverse();
      node<int> n = new node<int>();
      n = llist.head;
      while (n != null)
      {
        console.writeline(n.data);
        n = n.next;
      }
      console.readline();
      */
      /*测试单链表从头部添加元素
      linkedlist<int> llist = new linkedlist<int>();
      llist.add(1,addposition.head);
      llist.add(2, addposition.head);
      llist.add(3, addposition.head);
      llist.add(4, addposition.head);
      llist.add(5, addposition.head);
      node<int> m = new node<int>();
      m = llist.head;
      while (m != null)
      {
        console.writeline(m.data);
        m = m.next;
      }
      console.readline();
      */
      /*测试对单链表清除重复元素操作(返回另一链表)。这个例子中避免使用add()方法,因为每个add()都要从头到尾进行遍历,不适用add()方法
       就要求对目标链表的最后一个元素实时保存。另一种避免的方法在于从头部add,但这样的最终结果为倒序
      linkedlist<int> llist = new linkedlist<int>();//原链表
      linkedlist<int> llist2 = new linkedlist<int>();//保存结果的链表
      llist.add(1);
      llist.add(2);
      llist.add(1);
      llist.add(3);
      llist.add(3);
      llist.add(4);
      llist.add(5);
      node<int> m = new node<int>();
      m = llist.head;
      while (m != null)
      {
        console.writeline(m.data);
        m = m.next;
      }
      console.readline();
      node<int> node1 = llist.head;//标识原链表的当前要参与比较大小的元素,即可能放入链表2中的元素
      node<int> node2;//标识结果单链表的最后一个元素,避免使用add函数造成多次遍历
      node<int> node3;//对node1的后继进行暂时保存,并最终付给node1
      node3 = node1.next;
      llist2.head = node1;//链表1的头结点肯定要加入链表2
      node2 = llist2.head;//node2表示链表2的最后一个元素,此时最后一个元素为头结点
      node2.next = null;//由于是把node1赋给了链表2的头结点,必须把它的后续结点设为null,否则会一起带过来
      node1 = node3;//如果没有node3暂存node1的后继,对llist2.head后继赋为null的就会同时修改node1的后继,因为两者指向同一块内存
      while (node1 != null)
      {
        //在ilist2中比较大小
        node<int> tmp = llist2.head;
        if (node1.data.equals(tmp.data))
        {
          node1 = node1.next;
          continue;//若相等,则node1向后移一位,重新计算
        }
        else
        {
          node<int> tmp2 = tmp;
          tmp = tmp.next;//tmp标识链表2的用于循环的节点,与node比较
          if (tmp == null)//当链表2中现有元素与node1都不相等时
          {
            node3 = node1.next;
            node2.next = node1;
            node2 = node1;
            node2.next = null;
            node1 = node3;
            continue;
          }
          while (tmp != null)//tmp不为null时,一直循环到它为null
          {
            if (node1.data.equals(tmp.data))
            {
              node1 = node1.next;
            }
            else
            {
              tmp2 = tmp;
              tmp = tmp.next;
              if (tmp == null)
              {
                node3 = node1.next;
                node2.next = node1;
                node2 = node1;
                node2.next = null;
                node1 = node3;
              }
            }
          }
        }
      }
      //输出清除重复处理后的数组
      node<int> n = new node<int>();
      n = llist2.head;
      while (n!= null)
      {
        console.writeline(n.data);
        n = n.next;
      }
      console.readline();
      */
    }
  }
}

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#数据结构与算法教程》、《c#遍历算法与技巧总结》、《c#程序设计之线程使用技巧总结》、《c#操作excel技巧总结》、《c#中xml文件操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数组操作技巧总结》及《c#面向对象程序设计入门教程

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网