当前位置:   article > 正文

C# 双向链表_c# 图形化编程 双向链表

c# 图形化编程 双向链表
  1. /// <summary>
  2. /// 双向链表节点
  3. /// </summary>
  4. /// <typeparam name="T"></typeparam>
  5. public class BdNode<T>
  6. {
  7. public T Data { set; get; }
  8. public BdNode<T> Next { set; get; }
  9. public BdNode<T> Prev { set; get; }
  10. public BdNode(T val, BdNode<T> prev, BdNode<T> next)
  11. {
  12. this.Data = val;
  13. this.Prev = prev;
  14. this.Next = next;
  15. }
  16. }
  17. public class DoubleLink<T>
  18. {
  19. //表头
  20. private readonly BdNode<T> _linkHead;
  21. //节点个数
  22. private int _size;
  23. public DoubleLink()
  24. {
  25. _linkHead = new BdNode<T>(default(T), null, null);//双向链表 表头为空
  26. _linkHead.Prev = _linkHead;
  27. _linkHead.Next = _linkHead;
  28. _size = 0;
  29. }
  30. //获取节点数
  31. public int GetSize() => _size;
  32. //链表是否为空
  33. public bool IsEmpty() => (_size == 0);
  34. //通过索引查找
  35. private BdNode<T> GetNode(int index)
  36. {
  37. if (index < 0 || index >= _size)
  38. throw new IndexOutOfRangeException("索引溢出或者链表为空");
  39. if (index < _size / 2)//正向查找
  40. {
  41. BdNode<T> node = _linkHead.Next;
  42. for (int i = 0; i < index; i++)
  43. node = node.Next;
  44. return node;
  45. }
  46. //反向查找
  47. BdNode<T> rnode = _linkHead.Prev;
  48. int rindex = _size - index - 1;
  49. for (int i = 0; i < rindex; i++)
  50. rnode = rnode.Prev;
  51. return rnode;
  52. }
  53. public T Get(int index) => GetNode(index).Data;
  54. public T GetFirst() => GetNode(0).Data;
  55. public T GetLast() => GetNode(_size - 1).Data;
  56. // 将节点插入到第index位置之前
  57. public void Insert(int index, T t)
  58. {
  59. if (_size < 1 || index >= _size)
  60. throw new Exception("没有可插入的点或者索引溢出了");
  61. if (index == 0)
  62. Append(_size, t);
  63. else
  64. {
  65. BdNode<T> inode = GetNode(index);
  66. BdNode<T> tnode = new BdNode<T>(t, inode.Prev, inode);
  67. inode.Prev.Next = tnode;
  68. inode.Prev = tnode;
  69. _size++;
  70. }
  71. }
  72. //追加到index位置之后
  73. public void Append(int index, T t)
  74. {
  75. BdNode<T> inode;
  76. if (index == 0)
  77. inode = _linkHead;
  78. else
  79. {
  80. index = index - 1;
  81. if (index < 0)
  82. throw new IndexOutOfRangeException("位置不存在");
  83. inode = GetNode(index);
  84. }
  85. BdNode<T> tnode = new BdNode<T>(t, inode, inode.Next);
  86. inode.Next.Prev = tnode;
  87. inode.Next = tnode;
  88. _size++;
  89. }
  90. //删除元素
  91. public void Del(int index)
  92. {
  93. BdNode<T> inode = GetNode(index);
  94. inode.Prev.Next = inode.Next;
  95. inode.Next.Prev = inode.Prev;
  96. _size--;
  97. }
  98. public void DelFirst() => Del(0);
  99. public void DelLast() => Del(_size - 1);
  100. public void ShowAll()
  101. {
  102. Console.WriteLine("******************* 链表数据如下 *******************");
  103. for (int i = 0; i < _size; i++)
  104. Console.WriteLine("(" + i + ")=" + Get(i));
  105. Console.WriteLine("******************* 链表数据展示完毕 *******************\n");
  106. }
  107. }

调用脚本

  1. DoubleLink<int> dlink = new DoubleLink<int>();// 创建双向链表
  2. Console.WriteLine("将 20 插入到表头之后");
  3. dlink.Append(0, 10);
  4. dlink.ShowAll();
  5. Console.WriteLine("将 40 插入到表头之后");
  6. dlink.Append(1, 30);
  7. dlink.ShowAll();
  8. Console.WriteLine("将 10 插入到表头之前");
  9. dlink.Insert(0, 40);
  10. dlink.ShowAll();
  11. Console.WriteLine("将 30 插入到第一个位置之前");
  12. dlink.Insert(1, 20);
  13. dlink.ShowAll();
  14. Console.WriteLine("展示第一个:" + dlink.GetFirst());
  15. Console.WriteLine("删除第一个");
  16. dlink.DelFirst();
  17. Console.WriteLine("展示第一个:" + dlink.GetFirst());
  18. Console.WriteLine("展示最后一个:" + dlink.GetLast());
  19. Console.WriteLine("删除最后一个");
  20. dlink.DelLast();
  21. Console.WriteLine("展示最后一个:" + dlink.GetLast());
  22. dlink.ShowAll();
  23. Console.ReadKey();

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号