单链表和实现 SinglyLinkedList

/*
单向链表(又名单链表、线性链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过从头部开始,依序往下读取。
 */

namespace DataStructure
{

  public class SinglyLinkedList
  {
    ListNode head;

    public bool IsEmpty()
    {
      return head == null;
    }

    public void Clear()
    {
      head = null;
    }

    public ListNode GetParent(ListNode node)
    {
      if (head == null)
      {
        return null;
      }

      var cur = head;
      while (cur != null && cur.next != node)
      {
        cur = cur.next;
      }

      return cur;
    }

    public int IndexOf(ListNode node)
    {
      if (head == null)
      {
        return -1;
      }
      var cur = head;
      var i = 0;
      while (cur != null && cur != node)
      {
        cur = cur.next;
        i++;
      }
      if (cur == null)
      {
        return -1;
      }
      return i;
    }

    public void Add(ListNode node, int index)
    {
      var target = Get(index);
      RemoveAllFrom(target);
      Add(node);
      Add(target);
    }

    public void RemoveAllFrom(ListNode node){
      var parent = GetParent(node);
      parent.next = null;
    }

    public void Add(ListNode node)
    {
      if (head == null)
      {
        head = node;
        return;
      }
      //追加到末尾
      var cur = head;
      while (cur.next != null)
      {
        cur = cur.next;
      }
      cur.next = node;
    }

    public void Remove(ListNode node)
    {
      if (head == null)
      {
        return;
      }

      if (head == node)
      {
        if (head.next != null)
        {
          head = head.next;
        }
        else
        {
          head = null;
        }
        return;
      }

      //找到node前一个、node
      var parent = GetParent(node);
      //can not find node
      if (parent == null)
      {
        return;
      }
      parent.next = node.next;
    }

    public ListNode Get(int index)
    {
      if (null == head)
      {
        throw new System.Exception("empty list");
      }

      //计数,需要找多少次
      var count = index + 1;
      var temp = head;
      var c = 1;
      while (c < count && temp != null)
      {
        temp = temp.next;
        c++;
      }

      //计数不够,index超了
      if (c < count)
      {
        throw new System.IndexOutOfRangeException(string.Format("index:{0}", index));
      }

      return temp;
    }

    public int Size()
    {
      var temp = head;
      var count = 0;

      while (temp != null)
      {
        count++;
        temp = temp.next;
      }
      return count;
    }

  }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值