C#入门(8)-实现单链表
2006-07-08 13:32
281 查看
实现单链表
using System;
using System.IO;
// 构成链表的结点定义
public class Node
{
public Object data;
public Node next;
public Node( Object d )
{
data = d;
next = null;
}
}
public class List
{
// 用变量来实现表头
private Node Head = null;
private Node Tail = null;
private Node Pointer = null;
private int Length = 0;
//清空整个链表
public void deleteAll( )
{
Head = null;
Tail = null;
Pointer = null;
Length = 0;
}
//链表复位,使第一个结点 成为当前结点
public void reset( )
{
Pointer = null;
}
//判断链表是否为空
public bool isEmpty( )
{
return (Length == 0);
}
//判断当前结点是否 为最后一个结点
public bool isEnd( )
{
if (Length == 0)
throw new System.Exception( );
else if (Length == 1)
return true;
else
return (cursor( ) == Tail);
}
//返回当前结点的下一个结点的值, 并使其成为当前结点
public Object nextNode( )
{
if (Length == 1)
throw new System.Exception( );
else if (Length == 0)
throw new System.Exception( );
else
{
Node temp = cursor();
Pointer = temp;
if (temp != Tail)
return (temp.next.data);
else
throw new System.Exception( );
}
}
//返回当前结点的值
public Object currentNode( )
{
Node temp = cursor( );
return temp.data;
}
//在当前结点前插入一个结点, 并使其成为当前结点
public void insert( Object d )
{
Node e = new Node( d );
if (Length == 0)
{
Tail = e;
Head = e;
}
else
{
Node temp = cursor( );
e.next = temp;
if (Pointer == null)
Head = e;
else
Pointer.next = e;
}
Length++;
}
//返回链表的大小
public int size( )
{
return Length;
}
//将当前结点移出链表,下一个结点成为当前结点
//如果移出的结点是最后一个结点,则第一个结点成为当前结点
public Object remove( )
{
Object temp;
if (Length == 0)
throw new System.Exception( );
else if (Length == 1)
{
temp = Head.data;
deleteAll( );
}
else
{
Node cur = cursor( );
temp = cur.data;
if (cur == Head)
Head = cur.next;
else if (cur == Tail)
{
Pointer.next = null;
Tail = Pointer;
reset( );
}
else
Pointer.next = cur.next;
Length--;
}
return temp;
}
//返回当前结点的指针
private Node cursor( )
{
if (Head == null)
throw new System.Exception( );
else if (Pointer == null)
return Head;
else
return Pointer.next;
}
//链表的简单应用举例
public static void Main( )
{
List a = new List();
for (int i = 1; i <= 10; i++)
a.insert( new IntPtr(i));
Console.WriteLine(a.currentNode( ));
while (!a.isEnd( ))
Console.WriteLine(a.nextNode( ));
a.reset();
while (!a.isEnd( ))
{
a.remove( );
}
a.remove( );
a.reset( );
if (a.isEmpty( ))
Console.WriteLine("There is no Node in List!");
Console.WriteLine("You can press return to quit!");
try
{
// 确保用户看清程序运行结果
Console.Read( );
}
catch (IOException e)
{
}
}
}
判断一个数是否回文数,并做简单演示--回文数
using System;
class Palindrome
{
public static void Main()
{
int num1 = 1285821;
int num2 = 412214;
int num3 = 1234567;
Show( num1 );
Show( num2 );
Show( num3 );
}
//判断一个数是否是回文数
public static bool isPalin( int Val )
{
string s;
s = Val.ToString( );
char x,y;
bool flag = true;
for(int i = 0; i < s.Length / 2; i++)
{
x = s[i];
y = s[s.Length-1-i];
if( x != y ) //若不等,则该数不是回文数
{
flag = false;
}
}
return flag;
}
//打印数字及其回文数判断结果
public static void Show( int Val )
{
if( isPalin( Val ) )
Console.WriteLine("{0}/t是回文数!", Val);
else
Console.WriteLine("{0}/t不是回文数!", Val);
}
}
递归解决汉诺塔问题
using System;
public class HanoiTower
{
// 将n个盘从from柱移到to柱,以aux柱为辅助柱
public static void move(int n, char from, char to, char aux)
{
if (n == 1)
{
// 仅有一个盘时,直接从from柱移到to柱
Console.WriteLine("将 #1 盘从 {0} 移到 {1}", from, to );
}
else
{
// 将n - 1个盘从from柱移到aux柱,以to柱为辅助柱
move(n - 1, from, aux, to);
// 将最下的圆盘从from柱移到to柱
Console.WriteLine("将 #{0} 盘从 {1} 移到 {2}", n, from, to );
// 将n - 1个盘从aux柱移到to柱,以from柱为辅助柱
move(n - 1, aux, to, from);
}
}
public static void Main( )
{
// 将3个圆盘从A柱移到C柱,移动时利用B柱为辅助柱
move(3, 'A', 'C', 'B');
}
}
using System;
using System.IO;
// 构成链表的结点定义
public class Node
{
public Object data;
public Node next;
public Node( Object d )
{
data = d;
next = null;
}
}
public class List
{
// 用变量来实现表头
private Node Head = null;
private Node Tail = null;
private Node Pointer = null;
private int Length = 0;
//清空整个链表
public void deleteAll( )
{
Head = null;
Tail = null;
Pointer = null;
Length = 0;
}
//链表复位,使第一个结点 成为当前结点
public void reset( )
{
Pointer = null;
}
//判断链表是否为空
public bool isEmpty( )
{
return (Length == 0);
}
//判断当前结点是否 为最后一个结点
public bool isEnd( )
{
if (Length == 0)
throw new System.Exception( );
else if (Length == 1)
return true;
else
return (cursor( ) == Tail);
}
//返回当前结点的下一个结点的值, 并使其成为当前结点
public Object nextNode( )
{
if (Length == 1)
throw new System.Exception( );
else if (Length == 0)
throw new System.Exception( );
else
{
Node temp = cursor();
Pointer = temp;
if (temp != Tail)
return (temp.next.data);
else
throw new System.Exception( );
}
}
//返回当前结点的值
public Object currentNode( )
{
Node temp = cursor( );
return temp.data;
}
//在当前结点前插入一个结点, 并使其成为当前结点
public void insert( Object d )
{
Node e = new Node( d );
if (Length == 0)
{
Tail = e;
Head = e;
}
else
{
Node temp = cursor( );
e.next = temp;
if (Pointer == null)
Head = e;
else
Pointer.next = e;
}
Length++;
}
//返回链表的大小
public int size( )
{
return Length;
}
//将当前结点移出链表,下一个结点成为当前结点
//如果移出的结点是最后一个结点,则第一个结点成为当前结点
public Object remove( )
{
Object temp;
if (Length == 0)
throw new System.Exception( );
else if (Length == 1)
{
temp = Head.data;
deleteAll( );
}
else
{
Node cur = cursor( );
temp = cur.data;
if (cur == Head)
Head = cur.next;
else if (cur == Tail)
{
Pointer.next = null;
Tail = Pointer;
reset( );
}
else
Pointer.next = cur.next;
Length--;
}
return temp;
}
//返回当前结点的指针
private Node cursor( )
{
if (Head == null)
throw new System.Exception( );
else if (Pointer == null)
return Head;
else
return Pointer.next;
}
//链表的简单应用举例
public static void Main( )
{
List a = new List();
for (int i = 1; i <= 10; i++)
a.insert( new IntPtr(i));
Console.WriteLine(a.currentNode( ));
while (!a.isEnd( ))
Console.WriteLine(a.nextNode( ));
a.reset();
while (!a.isEnd( ))
{
a.remove( );
}
a.remove( );
a.reset( );
if (a.isEmpty( ))
Console.WriteLine("There is no Node in List!");
Console.WriteLine("You can press return to quit!");
try
{
// 确保用户看清程序运行结果
Console.Read( );
}
catch (IOException e)
{
}
}
}
判断一个数是否回文数,并做简单演示--回文数
using System;
class Palindrome
{
public static void Main()
{
int num1 = 1285821;
int num2 = 412214;
int num3 = 1234567;
Show( num1 );
Show( num2 );
Show( num3 );
}
//判断一个数是否是回文数
public static bool isPalin( int Val )
{
string s;
s = Val.ToString( );
char x,y;
bool flag = true;
for(int i = 0; i < s.Length / 2; i++)
{
x = s[i];
y = s[s.Length-1-i];
if( x != y ) //若不等,则该数不是回文数
{
flag = false;
}
}
return flag;
}
//打印数字及其回文数判断结果
public static void Show( int Val )
{
if( isPalin( Val ) )
Console.WriteLine("{0}/t是回文数!", Val);
else
Console.WriteLine("{0}/t不是回文数!", Val);
}
}
递归解决汉诺塔问题
using System;
public class HanoiTower
{
// 将n个盘从from柱移到to柱,以aux柱为辅助柱
public static void move(int n, char from, char to, char aux)
{
if (n == 1)
{
// 仅有一个盘时,直接从from柱移到to柱
Console.WriteLine("将 #1 盘从 {0} 移到 {1}", from, to );
}
else
{
// 将n - 1个盘从from柱移到aux柱,以to柱为辅助柱
move(n - 1, from, aux, to);
// 将最下的圆盘从from柱移到to柱
Console.WriteLine("将 #{0} 盘从 {1} 移到 {2}", n, from, to );
// 将n - 1个盘从aux柱移到to柱,以from柱为辅助柱
move(n - 1, aux, to, from);
}
}
public static void Main( )
{
// 将3个圆盘从A柱移到C柱,移动时利用B柱为辅助柱
move(3, 'A', 'C', 'B');
}
}
相关文章推荐
- SpringBoot入门-12(springboot配置servlet,利用代码实现)
- Android入门第十五篇之ActivityGroup + GridView 实现Tab分页标签
- 自编码器(AutoEncoder)入门及TensorFlow实现
- 单链表实现大数加法、大数减法、大数乘法、大数指数运算
- 数据结构基础(8) --单链表的设计与实现(1)之基本操作
- 单链表---两种方法简单实现单链表逆置
- java单链表实现一元多项式加法和乘法运算
- 一步步深入学习webpack(入门困惑express和dev-server区别及分别使用dev-server和webpack-hot-middleware实现的热加载区别)
- Spring入门——Bean管理的注解实现及例子
- 【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目(5.1) 登录功能的实现,开始接触Spring IOC、DI
- 学习STL,实现一个单链表的迭代器
- <unity shader入门精要> - unity shader 中实现漫反射光照模型
- C++实现单链表(类和对象)——简单实现
- JAVA实现单链表
- 用java实现单链表模拟栈
- mybatis入门-实现基本CRUD操作
- 单链表的增删查改等操作实现
- mapreduce代码实现入门
- Jqgrid入门-结合Struts2+json实现数据展示(五)
- 入门干货之Electron的.NET实现-Electron.NET