C#定义并实现单链表实例解析
作者:shichen2014 发布时间:2022-04-15 12:53:09
标签:C#,单链表
本文以实例详细描述了C#定义并实现单链表的过程及原理。一般来说C#定义并实现单链表,代码包括构成链表的结点定义、用变量来实现表头、清空整个链表 、链表复位,使第一个结点成为当前结点、判断链表是否为空、判断当前结点是否为最后一个结点、返回当前结点的下一个结点的值,并使其成为当前结点、将当前结点移出链表,下一个结点成为当前结点等内容。
具体实现代码如下所示:
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)
{
}
}
}
0
投稿
猜你喜欢
- 1. 查找1) 顺序查找 SeqSearch.java2) 二分查找【二分法,放在算法讲解】2. 顺序查找有一个数列:白眉鹰王、金毛狮王、紫
- StringString类是不可变类,即一旦一个String对象被创建以后,包含在这个对象中的字符序列是不可改变的,直至这个对象被销毁。这个
- 本文为大家分享了C#基于Socket套接字的网络通信封装代码,供大家参考,具体内容如下摘要之所以要进行Socket套接字通信库封装,主要是直
- C#判断数据类型的简单示例代码:int i = 5; Console
- 什么是字符串字符串或串(String)是由数字、字母、下划线组成的一串字符。一般记为 s=“a1a2···an”(n>=0)。它是编程
- 一、目标效果聊天会话页的列表效果1、聊天数据不满一屏时,顶部显示所有聊天数据2、插入消息时如果最新消息紧靠列表底部时,则插入消息会使列表向上
- 我这里 shiro 并没有集成 springMVC,直接使用 ini 配置文件。shiro.ini[main]# Objects and t
- 1,验证传入路径是否为正确的路径名(Windows系统,其他系统未使用)Java代码 // 验证字符串是否为正确路径名的正则表达式 
- 这篇文章主要介绍了Maven打包jar生成javadoc文件和source文件代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作
- MD5加密简介哈希算法又称散列算法,是将任何数据转换成固定长度的算法的统称。 从本质上讲,MD5也是一种哈希算法,其输出是生成12
- 1、CS、BS架构定义CS(Client/Server):客户端----服务器结构。C/S结构在技术上很成熟,它的主要特点是交互性强、具有安
- 这里我们以拨打电话申请权限来写个小例子,也就是CALL_PHONE,因为拨打电话会涉及用户手机的资费问题,因而被列为了危险权限,在Andro
- 前言C#中Try-Catch语句大家都很熟悉了,但是细究起来,还是有很多东西可讲的。最近在翻看之前总结的常见面试题中,发现关于try...c
- 引言声明:文中的MPChart代指MPAndroidChart.本文主要讲解LineChart中的三个变种Chart,第一个是渐变的Line
- 一、问题场景使用Logger.error方法时只能打印出异常类型,无法打印出详细的堆栈信息,使得定位问题变得困难和不方便。二、先放出结论Lo
- RequestHeaders添加自定义参数在开发过程中有的时候,参数需要绑定到requestHeaders中,而并不是在body中进行传输。
- 这个是jdk1.5以后才引入的新的内容,作为秉承发表是最好的记忆,毅然决定还是用一篇博客来代替我的记忆: java语言规范中说道:在许多情况
- C#实现的Check Password,并根据输错密码的次数分情况锁定账户:如果输入错误3次,登录账户锁定5分钟并提示X点X分后重试登录。如
- 目录RemoveSubstringReplaceSpiltJoinAppendRemoveRemove(int startIndex) 删除
- 在新版的AndroidStudio3.6 中,在项目的包下新建 activity 时,一般会同时生成对应的java和xml文件,例如新建 M