• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

博客园    首页    新随笔    联系   管理    订阅  订阅

02.单向链表

  1     public class CirculLinkedList
  2     {
  3         //元素个数
  4         private int count;
  5 
  6         //尾指针
  7         private Node tail;
  8 
  9         //当前节点的前节点
 10         private Node CurrPrev;
 11 
 12 
 13         //增加元素
 14         public void Add(object value)
 15         {
 16             Node newNode = new Node(value);
 17             if (tail==null)
 18             {//链表为空
 19                 tail = newNode;
 20                 tail.next = newNode;
 21                 CurrPrev = newNode;
 22             }
 23             else
 24             {//链表不为空,把元素增加到链表结尾
 25                 newNode.next = tail.next;
 26                 tail.next = newNode;
 27 
 28                 if (CurrPrev==tail)
 29                 {
 30                     CurrPrev = newNode;
 31                 }
 32                 tail = newNode;
 33             }
 34             count++;
 35         }
 36 
 37         //删除当前元素
 38         public void RemoveCurrNode()
 39         {
 40             //为null代表为空表
 41             if (tail == null) 
 42             {
 43                 throw new NullReferenceException("集合中没有任何元素!");
 44             }
 45             else if (count==1)
 46             {
 47                 tail = null;
 48                 CurrPrev = null;
 49             }
 50             else
 51             {
 52                 if (CurrPrev.next==tail)
 53                 {
 54                     tail = CurrPrev;
 55                 }
 56                 CurrPrev.next = CurrPrev.next.next;
 57             }
 58             count--;
 59         }
 60 
 61         //当前节点移动步数
 62         public void Move(int step)
 63         {
 64             if (step<0)
 65             {
 66                 throw new ArgumentOutOfRangeException("step", "移动步数不可为负数!");
 67             }
 68             if (tail==null)
 69             {
 70                 throw new NullReferenceException("链表为空!");
 71             }
 72             for (int i = 0; i < step; i++)
 73             {
 74                 CurrPrev = CurrPrev.next;
 75             }
 76         }
 77 
 78         //获得当前节点
 79         public object Current
 80         {
 81             get {return CurrPrev.next.item ;}
 82         }
 83 
 84         public override string ToString()
 85         {
 86             if (tail==null)
 87             {
 88                 return string.Empty;
 89             }
 90             string s = "";
 91             Node temp = tail.next;
 92             for (int i = 0; i < count; i++)
 93             {
 94                 s += temp.ToString() + "    ";
 95                 temp = temp.next;
 96             }
 97             return s;
 98         }
 99 
100 
101         public int Count
102         {
103             get {return count ;}
104         }
105 
106         private   class Node
107         {
108             public Node(object  value)
109             {
110                 item = value;
111             }
112             //放置数据
113             public object item;
114             public CirculLinkedList.Node next;
115             public override string ToString()
116             {
117                 return item.ToString();
118             }
119         }
120     }
View Code

 

posted @ 2016-04-05 13:45  福州老吴  阅读(149)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3