将链表逆序(Revert)的C#实现
2012-02-27 12:29
162 查看
// 链表类
class LL
{
public string value;
public LL link;
// used for outputing the link data
public void OutPut()
{
Console.Write(value);
if (link != null)
{
Console.Write(",");
link.OutPut();
}
}
}
逆序:
private LL Revert(LL t)
{
LL newList = null;
while (t != null)
{
LL mid = new LL();
mid.value = t.value;
mid.link = newList;
newList = mid;
t = t.link;
}
return newList;
}
全部代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _08ConsoleTest
{
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t.T1();
}
}
public class Test
{
public void T1()
{
// Create a Link
LL l = CreateLL("A");
LL l2 = CreateLL("B");
l.link = l2;
l2.link = CreateLL("C");
OutputLL(l); // Output the original link: A B C
LL revertLL = Revert(l);
OutputLL(revertLL); // Output the target link: C B A
OutputLL(l); // Output the original link: A B C (The original link will not change)
}
private void OutputLL(LL l)
{
l.OutPut();
Console.WriteLine("---------------");
}
private LL Revert(LL t)
{
LL newList = null;
while (t != null)
{
LL mid = new LL();
mid.value = t.value;
mid.link = newList;
newList = mid;
t = t.link;
}
return newList;
}
private LL CreateLL(string a)
{
LL l = new LL();
l.value = a;
return l;
}
}
// 链表类
class LL
{
public string value;
public LL link;
// used for outputing the link data
public void OutPut()
{
Console.Write(value);
if (link != null)
{
Console.Write(",");
link.OutPut();
}
}
}
}
class LL
{
public string value;
public LL link;
// used for outputing the link data
public void OutPut()
{
Console.Write(value);
if (link != null)
{
Console.Write(",");
link.OutPut();
}
}
}
逆序:
private LL Revert(LL t)
{
LL newList = null;
while (t != null)
{
LL mid = new LL();
mid.value = t.value;
mid.link = newList;
newList = mid;
t = t.link;
}
return newList;
}
全部代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _08ConsoleTest
{
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t.T1();
}
}
public class Test
{
public void T1()
{
// Create a Link
LL l = CreateLL("A");
LL l2 = CreateLL("B");
l.link = l2;
l2.link = CreateLL("C");
OutputLL(l); // Output the original link: A B C
LL revertLL = Revert(l);
OutputLL(revertLL); // Output the target link: C B A
OutputLL(l); // Output the original link: A B C (The original link will not change)
}
private void OutputLL(LL l)
{
l.OutPut();
Console.WriteLine("---------------");
}
private LL Revert(LL t)
{
LL newList = null;
while (t != null)
{
LL mid = new LL();
mid.value = t.value;
mid.link = newList;
newList = mid;
t = t.link;
}
return newList;
}
private LL CreateLL(string a)
{
LL l = new LL();
l.value = a;
return l;
}
}
// 链表类
class LL
{
public string value;
public LL link;
// used for outputing the link data
public void OutPut()
{
Console.Write(value);
if (link != null)
{
Console.Write(",");
link.OutPut();
}
}
}
}
相关文章推荐
- C#实现单链表
- java实现单链表(增加节点,删除节点,根据下标获取节点,获取链表大小,链表逆序)
- 几个链表逆序的实现方法
- 实现链表逆序(头插)
- C#实现--单链表(链式)
- 【python中单链表的实现】——包括初始化、创建、逆序、遍历等
- 实现链表逆序
- C语言实现单链表逆序与逆序输出实例
- C#双向链表LinkedList排序实现方法
- 链表逆序(递归实现和非递归实现)
- 用C#实现单链表(插入,在第i个前插,在第i个后插)
- C语言:链表的逆序,两种思考方法,第一种用rev_1()实现,第二种用rev_2( )实现.
- 单链表的头插、中插、尾插、删除、逆序、显示(C语言实现)
- 链表逆序最少空间实现
- C# 泛型实现链表、代码重用
- C#实现单链表(线性表)
- 用 C# 实现的一个链表
- C#中的泛型链表的实现
- 链表的各种操作实现 链表逆序 链表排序 有序链表归并 链表存在环的判定
- 剑指offer面试题java实现之题5:逆序打印链表