• 重新整理数据结构与算法——单双链表模拟队列[四]


    前言

    整理c# 基础算法,大概81篇,相互学习交流。

    正文

    单链表

    单链表,实在不想介绍,直接上代码,写了不晓得多少遍。。。

    public class SingleLinkList
    {
    	//一个无任何业务意义的头部节点
    	public StudentNode headNode = new StudentNode(-1,"");
    
    	public void addNode(StudentNode node)
    	{
    		StudentNode temp = headNode;
    		while (true)
    		{
    			if (temp.next == null)
    			{
    				break;
    			}
    			temp = temp.next;
    		}
    		temp.next = node;
    	}
    	/// <summary>
    	/// 添加node 通过顺序
    	/// </summary>
    	/// <param name="node"></param>
    	public void addNodeOrderByNo(StudentNode node)
    	{
    		StudentNode temp = headNode;
    		bool flag = false;
    		while (true)
    		{
    			if (temp.next == null)
    			{
    				break;
    			}
    			if (temp.next.no > node.no)
    			{
    				flag = true;
    				break;
    			}
    			if (temp.next.no == node.no)
    			{
    				Console.WriteLine("已经存在");
    			}
    			temp = temp.next;
    		}
    		//判断是否找到
    		if (flag)
    		{
    			node.next = temp.next.next;
    			temp.next = node;
    		}
    		else
    		{
    			temp.next = node;
    		}
    	}
    
    	public void delNode(int no)
    	{
    		StudentNode temp = headNode;
    		bool flag = false;
    		while (true)
    		{
    			if (temp.next == null)
    			{
    				break;
    			}
    			if (temp.next.no == no)
    			{
    				flag = true;
    				break;
    			}
    			temp = temp.next;
    		}
    		//判断是否找到
    		if (flag)
    		{
    			temp.next = temp.next.next;
    		}
    		else
    		{
    			Console.WriteLine("删除对象不存在!");
    		}
    	}
    	/// <summary>
    	/// 更新节点
    	/// </summary>
    	/// <param name="node"></param>
    	public void updateNode(StudentNode node)
    	{
    		if (headNode.next == null)
    		{
    			Console.WriteLine("更新对象不存在!");
    			return;
    		}
    		StudentNode temp = headNode.next;
    		bool flag = false;
    		while (true)
    		{
    			if (temp == null)
    			{
    				break;
    			}
    			if (temp.no == node.no)
    			{
    				flag = true;
    				break;
    			}
    			temp = temp.next;
    		}
    		if (flag)
    		{
    			temp.name = node.name;
    		}else {
    			Console.WriteLine("更新对象不存在!");
    		}
    	}
    
    	/// <summary>
    	/// 遍历节点
    	/// </summary>
    	/// <param name="node"></param>
    	public void ShowNodes()
    	{
    		//特殊处理节点
    		if (headNode.next == null)
    		{
    			return;
    		}
    		StudentNode temp = headNode.next;
    		while (true)
    		{
    			if (temp == null)
    			{
    				break;
    			}
    			Console.WriteLine(temp.ToString());
    			temp = temp.next;
    		}
    	}
    }
    public class StudentNode {
    	public int no;
    	public string name;
    	public StudentNode next;
    	/// <summary>
    	/// 实例化
    	/// </summary>
    	/// <param name="no">学号</param>
    	/// <param name="name">名字</param>
    	public StudentNode(int no,string name,StudentNode next)
    	{
    		this.no = no;
    		this.name = name;
    		this.next = next;
    	}
    
    	public StudentNode(int no, string name):this(no,name,null)
    	{
    	  
    	}
    
    	public override string ToString() {
    		return "姓名:" + name + "学号:" + no;
    	}
    }
    

    双链表

    public StudentNode headNode = new StudentNode(-1, "");
    
    public void addNode(StudentNode node)
    {
    	StudentNode temp = headNode;
    	while (true)
    	{
    		if (temp.next == null)
    		{
    			break;
    		}
    		temp = temp.next;
    	}
    	temp.next = node;
    	node.pre = temp;
    }
    /// <summary>
    /// 添加node 通过顺序
    /// </summary>
    /// <param name="node"></param>
    public void addNodeOrderByNo(StudentNode node)
    {
    	if (headNode.next == null)
    	{
    		headNode.next = node;
    		return;
    	}
    	StudentNode temp = headNode.next;
    	bool flag = false;
    	while (true)
    	{
    		if (temp == null)
    		{
    			break;
    		}
    		if (temp.no > node.no)
    		{
    			flag = true;
    			break;
    		}
    		if (temp.no == node.no)
    		{
    			Console.WriteLine("已经存在");
    			return;
    		}
    		temp = temp.next;
    	}
    	//判断是否找到
    	if (flag)
    	{
    		temp.pre.next = node;
    		node.pre = temp.pre;
    		node.next = temp;
    		temp.pre = node;
    	}
    	else
    	{
    		temp.next = node;
    		node.pre = temp;
    	}
    }
    
    public void delNode(int no)
    {
    	if (headNode.next==null)
    	{
    		Console.WriteLine("删除对象不存在!");
    		return;
    	}
    	StudentNode temp = headNode.next;
    	bool flag = false;
    	while (true)
    	{
    		if (temp == null)
    		{
    			break;
    		}
    		if (temp.no == no)
    		{
    			flag = true;
    			break;
    		}
    		temp = temp.next;
    	}
    	//判断是否找到
    	if (flag)
    	{
    		temp.pre.next = temp.next;
    		temp.next.pre = temp.pre;
    	}
    	else
    	{
    		Console.WriteLine("删除对象不存在!");
    	}
    }
    /// <summary>
    /// 更新节点
    /// </summary>
    /// <param name="node"></param>
    public void updateNode(StudentNode node)
    {
    	if (headNode.next == null)
    	{
    		Console.WriteLine("更新对象不存在!");
    		return;
    	}
    	StudentNode temp = headNode.next;
    	bool flag = false;
    	while (true)
    	{
    		if (temp == null)
    		{
    			break;
    		}
    		if (temp.no == node.no)
    		{
    			flag = true;
    			break;
    		}
    		temp = temp.next;
    	}
    	if (flag)
    	{
    		temp.name = node.name;
    	}
    	else
    	{
    		Console.WriteLine("更新对象不存在!");
    	}
    }
    
    /// <summary>
    /// 遍历节点
    /// </summary>
    /// <param name="node"></param>
    public void ShowNodes()
    {
    	//特殊处理节点
    	if (headNode.next == null)
    	{
    		return;
    	}
    	StudentNode temp = headNode.next;
    	while (true)
    	{
    		if (temp == null)
    		{
    			break;
    		}
    		Console.WriteLine(temp.ToString());
    		temp = temp.next;
    	}
    }
    }
    
    public class StudentNode
    {
    public int no;
    
    public string name;
    
    public StudentNode next;
    
    public StudentNode pre;
    /// <summary>
    /// 实例化
    /// </summary>
    /// <param name="no">学号</param>
    /// <param name="name">名字</param>
    public StudentNode(int no, string name, StudentNode next, StudentNode pre)
    {
    	this.no = no;
    	this.name = name;
    	this.next = next;
    	this.pre = pre;
    }
    
    public StudentNode(int no, string name) : this(no, name, null,null)
    {
    
    }
    
    public override string ToString()
    {
    	return "姓名:" + name + "学号:" + no;
    }
    }
    
  • 相关阅读:
    spring-security使用-权限控制(八)
    Spring源码阅读(六)-ConfigurationClassPostProcessor
    java陷阱之应用缓存
    Spring Batch源码阅读-Job之SimpleJob(五)
    Spring Batch源码阅读-JobLauncher之SimpleJobLauncher(四)
    Spring Batch源码阅读-初始化(三)
    Spring Batch源码阅读-简单使用(二)
    Spring Batch源码阅读-基本概念(一)
    JS函数的定义与调用方法
    百度地图离线化(API v=1.3)
  • 原文地址:https://www.cnblogs.com/aoximin/p/13074226.html
Copyright © 2020-2023  润新知