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

前言

整理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;
	}
	/// 
	/// 添加node 通过顺序
	/// 
	/// 
	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("删除对象不存在!");
		}
	}
	/// 
	/// 更新节点
	/// 
	/// 
	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("更新对象不存在!");
		}
	}

	/// 
	/// 遍历节点
	/// 
	/// 
	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(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;
}
/// 
/// 添加node 通过顺序
/// 
/// 
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("删除对象不存在!");
	}
}
/// 
/// 更新节点
/// 
/// 
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("更新对象不存在!");
	}
}

/// 
/// 遍历节点
/// 
/// 
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;
/// 
/// 实例化
/// 
/// 学号
/// 名字
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;
}
}

你可能感兴趣的:(重新整理数据结构与算法——单双链表模拟队列[四])