当前位置: 首页 > news >正文

C#双向链表实现:在当前节点后插入新数据的方法Insert()

目录

1.定义一个泛型节点类并自动属性

2.定义链表类,并实现Append、Print、MoveFirst、 Insert

3.Main方法


1.定义一个泛型节点类并自动属性

/// <summary>
/// 定义泛型节点类
/// </summary>
/// <typeparam name="T">泛型运算符</typeparam>
/// <param name="value">泛型参数</param>
public class ListNode<T>(T value)
{public T Object { get; set; } = value;public ListNode<T>? Next { get; set; }public ListNode<T>? Previous { get; set; }
}

2.定义链表类,并实现Append、Print、MoveFirst、 Insert

/// <summary>
/// 定义链表类
/// </summary>
public class LinkedList
{private ListNode<int>? _head;private ListNode<int>? _tail;private ListNode<int>? _current;public ListNode<int>? Current { get => _current; set => _current = value; }/// <summary>/// 追加节点到Append方法/// </summary>/// <param name="value"></param>public void Append(int value){var newNode = new ListNode<int>(value);if (_head == null){_head = newNode;_tail = newNode;}else{_tail!.Next = newNode;newNode.Previous = _tail;_tail = newNode;}}/// <summary>/// 输出各节点/// </summary>public void Print(){var current = _head;while (current != null){Console.WriteLine(current.Object);current = current.Next;}}/// <summary>///移动指针到链表头/// </summary>public void MoveFirst(){if (_head != null){_current = _head;}}/// <summary>/// 在当前节点后面插入新数据/// </summary>/// <param name="value">待插入的数据</param>public void Insert(int value){// 创建一个新的节点var newNode = new ListNode<int>(value);// 如果链表为空,将新节点设置为头节点if (_head == null){_head = newNode;_current = newNode;return;}// 找到当前节点var current = _current;if (current == null){//current = _head;_current = _head;while (_current.Next != null){_current = _current.Next;}current = _current;}// 在当前位置插入新节点newNode.Next = current.Next;newNode.Previous = current;current.Next = newNode;_current = newNode;}

3.Main方法

 class Program{static void Main(string[] args){ArgumentNullException.ThrowIfNull(args);var linkedList = new LinkedList();linkedList.Append(5);linkedList.Append(2);linkedList.Append(8);linkedList.Append(1);linkedList.Print();Console.WriteLine("*初始化数据*");linkedList.MoveFirst();linkedList.Insert(3);linkedList.Print();Console.WriteLine("*头结点后插入3*");}}

         运行结果:

//运行结果:
/*
5
2
8
1
*初始化数据*
5
3
2
8
1
*头结点后插入3**/

http://www.lryc.cn/news/310971.html

相关文章:

  • 10-Java装饰器模式 ( Decorator Pattern )
  • Vue.js 实用技巧:深入理解 Vue.set 方法
  • 单词规律00
  • vue3 vite项目一运行就401(Unauthorized)
  • LeetCode102.二叉树的层序遍历
  • Java底层自学大纲_JVM篇
  • 数据可视化?这些平台能处
  • [ai笔记14] 周鸿祎的ai公开课笔记1
  • 在Linux系统中创建新用户并登录
  • Vue.js+SpringBoot开发高校实验室管理系统
  • 文献阅读笔记《Spatial-temporal Forecasting for Regions without Observations》13页
  • 如何学习openfoam
  • vue前端密码加密,springboot后端密码解密
  • selenuim【1】$x(‘xpath’)、WebDriverWait()、try/assert
  • 机器学习模型总结
  • HTML5:七天学会基础动画网页6
  • mybatis中#{}和${}的区别?
  • 常用git 打tag命令
  • Learning from Unlabeled 3D Environments forVision-and-Language Navigation
  • 【算法分析与设计】组合
  • 数仓模型设计方法论
  • MySQL 面试题
  • 计算机专业必看的十部电影
  • 数据库之间数据迁移工具datax
  • uniapp:根据环境(开发、测试、生产)选择服务器接口或者业务
  • Leetcode—63. 不同路径 II【中等】
  • Redis 之三:Redis 的发布订阅(pub/sub)
  • ngx_waf入门教程:保护你的Nginx服务器
  • 视觉Transformers中的位置嵌入 - 研究与应用指南
  • 真香定律!我用这种模式重构了第三方登录