带头节点链表的反转 循环和递归 C#实现
来源:互联网 发布:java hello word 编辑:程序博客网 时间:2024/04/29 18:09
练练脑子,呵呵. 要熟悉和理解算法,最好还是亲自动手写一下,发现不管是笔试面试,大家都爱问链表相关的问题, 下午动手调了一下, 实现了带头节点的链表描述,链表建立,链表遍历,链表反转(循环和递归)
Node.cs 节点类
-----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace List
{
public class ListNodes
{
public Node headNode;
public ListNodes()
{
this.headNode = new Node();
}
public ListNodes(int length): this()
{
Node nodePointer = headNode;
for (int i = 1; i <= length; i++)
{
Console.WriteLine("Please input the node data");
int inpuData;
try
{
inpuData = Convert.ToInt32 ( Console.ReadLine());
Node newNode = new Node (inpuData );
nodePointer.next = newNode;
nodePointer = newNode;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
i--;
continue;
}
}
}
public void DisplayListNodes()
{
Node nodePointer = this.headNode;
Console .WriteLine ("Print the List Node");
while (nodePointer.next != null)
{
Console.WriteLine(nodePointer.next.data);
nodePointer = nodePointer.next;
}
}
public void ReverseListNodes()
{
Node nodePointer = this.headNode.next;
if (nodePointer != null)
{
Node nextNode = nodePointer.next ;
Node nextNodeNext = nodePointer;
while (nextNode != null )
{
nextNodeNext = nextNode.next;
nextNode.next = nodePointer;
if( nodePointer == this.headNode .next )
{
nodePointer.next = null;
}
nodePointer = nextNode;
nextNode = nextNodeNext;
}
this.headNode .next = nodePointer ;
}
}
}
}
链表集合类 ListNode.cs
------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace List
{
public class ListNodes
{
public Node headNode;
public ListNodes()
{
this.headNode = new Node();
}
public ListNodes(int length): this()
{
Node nodePointer = headNode;
for (int i = 1; i <= length; i++)
{
Console.WriteLine("Please input the node data");
int inpuData;
try
{
inpuData = Convert.ToInt32 ( Console.ReadLine());
Node newNode = new Node (inpuData );
nodePointer.next = newNode;
nodePointer = newNode;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
i--;
continue;
}
}
}
public void DisplayListNodes()
{
Node nodePointer = this.headNode;
Console .WriteLine ("Print the List Node");
while (nodePointer.next != null)
{
Console.WriteLine(nodePointer.next.data);
nodePointer = nodePointer.next;
}
}
public void ReverseListNodes()
{
Node nodePointer = this.headNode.next;
if (nodePointer != null)
{
Node nextNode = nodePointer.next ;
Node nextNodeNext = nodePointer;
while (nextNode != null )
{
nextNodeNext = nextNode.next;
nextNode.next = nodePointer;
if( nodePointer == this.headNode .next )
{
nodePointer.next = null;
}
nodePointer = nextNode;
nextNode = nextNodeNext;
}
this.headNode .next = nodePointer ;
}
}
public void ReverserListNodeRecur()
{
Node nodePointer = this.headNode .next ;
if( nodePointer !=null )
{
this.headNode .next = ReverseListNoHead(nodePointer);
}
}
static public Node ReverseListNoHead(Node lNode)
{
if (lNode != null)
{
if (lNode.next != null)
{
Node n = ReverseListNoHead(lNode.next);
lNode.next.next = lNode;
lNode.next = null;
return n;
}
else
{
return lNode;
}
}
else
return null;
}
}
}
Program.cs 测试桩
-------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace List
{
class Program
{
static void Main(string[] args)
{
int length=0;
bool continueFlag= true ;
while (continueFlag)
{
try
{
Console.WriteLine("Please input the length of the List");
length = Convert.ToInt32(Console.ReadLine());
if (length < 0)
{
Console.WriteLine("Please input an non negtive number");
continue;
}
break;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
Console.WriteLine("Start inputing the datas now");
ListNodes newList = new ListNodes(length );
newList.DisplayListNodes();
newList.ReverseListNodes();
Console.WriteLine("Reverse the List");
newList.DisplayListNodes();
Console.WriteLine("Reverse the List, Recursion");
newList.ReverserListNodeRecur();
Console.WriteLine("Reverse the List");
newList.DisplayListNodes();
}
}
}
- 带头节点链表的反转 循环和递归 C#实现
- 【C++】模板实现带头节点的双向循环链表
- 带头节点和不带头节点的链表
- C语言实现双向非循环链表(不带头结点)的节点插入
- C语言实现双向非循环链表(带头结点尾结点)的节点插入
- 不带头节点的双向循环链表基本操作
- 约瑟夫问题(带头节点的循环链表)
- 数据结构示例之带头节点的双向循环链表
- 数据结构示例之带头节点的双向循环链表
- josephus问题->不带头节点的循环链表
- 如何创建不带头节点的双向循环链表
- 双向循环带头节点链表
- 反转链表的循环方式和递归方式
- 带头节点链表和不带头节点链表的初始化
- 链表反转的递归和非递归实现方式
- 链表反转的递归和非递归实现方式
- 带头节点的链表
- 单链表反转问题(带头结点 和 不带头结点的 创建链表过程等)
- 什么叫压力测试(Stress Testing)
- 时间转换
- Visual studio 2008切换设计视图无响应以及Visual Studio 2008和office 2007安装的冲突问题
- Public key for filename.rpm is not installed
- C++ STL学习笔记二 vector向量容器
- 带头节点链表的反转 循环和递归 C#实现
- [Excel写入慢-Cells-每回30ms]的解决办法
- 利用SQLite ADO.NET在.NET(C#)中使用SQLite数据库
- 无符号类型和有符号类型的比较是个坑
- scanf 详解
- poi操作excel文件
- javascript 贪吃蛇(三)【附带源码】
- C#比较两个日期的大小
- 2010-3-31