反转单链表 C#
来源:互联网 发布:上海行知教育 英语培训 编辑:程序博客网 时间:2024/06/06 16:49
目前网上关于c#的反转链表的比较少 所以发个给学c#的朋友们看看,大家共同进步哈~
//main
namespace 单链表反转
{
class Program
{
static void Main(string[] args)
{
int flag = 0;
LinkedList Lin = new LinkedList();
Node Current = new Node();
Current.Element = "header";
Current=Lin.CreateNodeList(Current,flag);
Console.WriteLine("原先的链表,包括一个链表头");
Lin.PrintListByParameter(Current);
Console.WriteLine("递归方式反转链表,考虑了链表头前置的情况");
Lin.PrintListByParameter(Lin.ReversalList(Current, Current.Link));
Console.WriteLine("非递归方式反转链表,没有考虑连表头前置的情况");
Current = Lin.CreateNodeList(Current, flag);
Lin.PrintListByParameter(Lin.ReversalList(Current));
Console.Read();
}
}
}
//linkedlist 包括对链表进行操作的类
namespace 单链表反转
{
public class LinkedList
{
private Node header;
public LinkedList()
{
header = new Node("header");
}
public Node CreateNodeList(Node Nodelist,int i)//建立一个0-9的链表
{
if (i != 10)
{
Nodelist.Link = new Node(i.ToString());
i++;
CreateNodeList(Nodelist.Link, i);
}
return Nodelist;
}
public void PrintListByParameter(Node N)//将参数链表打印出来
{
Console.WriteLine(N.Element);
while (!(N.Link == null))
{
Console.WriteLine(N.Link.Element);
N = N.Link;
}
}
public Node ReversalList(Node Node1,Node Node2)// 递归方式考虑了header
{
if (Node2 ==null)
{
return null;
}
else
{
if (Node2.Link == null)
{
header.Link = Node2;
Node2.Link = Node1;
Node1.Link = null;
return header;
}
else
{
ReversalList(Node2, Node2.Link);
Node2.Link = Node1;
if (Node2.Link.Element.ToString() == "header")
{
Node2.Link = null;
return header;
}
Node1.Link = null;
return header;
}
}
}
public Node ReversalList(Node Current)//非递归方式没有考虑header
{
Node Medium = new Node();
Node Medium1 = new Node();
while (Current != null)
{
Medium1 = Current.Link;
Current.Link = Medium;
Medium = Current;
Current = Medium1;
}
return Medium;
}
}
}
//用class来定义一个链表
namespace 单链表反转
{
public class Node
{
public Object Element;//用来储存节点数据
public Node Link;//指向表内下一个节点的引用
public Node()
{
Element = null;
Link = null;
}
public Node(Object theElement)
{
Element = theElement;
Link = null;
}
}
}
- 反转单链表 C#
- C#反转字符串
- c#反转字符串
- C#字符串反转
- c# 字符串的反转
- C#字符串反转
- C#字符串反转
- C#字符串反转
- C#反转字符串
- C#字符串反转
- C#字符串反转
- c#字符串的反转
- C#反转字符串
- 单链表反转
- 单链表反转
- 单链表反转
- 反转单链表
- 单链表反转
- matlab里删除矩阵一些列的函数
- WebConfig(数据库连接)
- linux read() 函数
- 11款有用的Web开发在线工具
- 作为软件开发人员应该知道的问题
- 反转单链表 C#
- 数据库
- 添加环境变量arm-linux-gcc
- 为什么执行测试用例时报需要 org.junit 4???
- 想写个系列的
- 一次插入多条数据
- 嵌入式Linux开机Logo的制作
- SQLite数据库图形界面工具
- 嵌入式系统