C#定义并实现单链表实例解析

 更新时间:2014年07月17日 14:40:12   投稿:shichen2014  
这篇文章主要介绍了C#定义并实现单链表实例解析,有助于读者加深对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) 
 {
 }
 }
}

相关文章

  • C#实现压缩和解压缩的方法示例【Gzip和Zip方式】

    C#实现压缩和解压缩的方法示例【Gzip和Zip方式】

    这篇文章主要介绍了C#实现压缩和解压缩的方法,结合具体实例形式分析了Gzip和Zip两种压缩操作实现方法,需要的朋友可以参考下
    2017-06-06
  • C#超市收银系统设计

    C#超市收银系统设计

    这篇文章主要为大家详细介绍了C#超市收银系统设计,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • C# protobuf自动更新cs文件

    C# protobuf自动更新cs文件

    这篇文章主要介绍了C# protobuf自动更新cs文件的相关资料,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-02-02
  • Winform中实现图片格式转换

    Winform中实现图片格式转换

    这篇文章主要介绍了Winform中实现图片格式转换的示例代码,帮助大家更好的理解和使用winform开发,感兴趣的朋友可以了解下
    2020-12-12
  • ADO.NET实用技巧两则

    ADO.NET实用技巧两则

    ADO.NET实用技巧两则...
    2007-03-03
  • C#实现单例模式的多种方式

    C#实现单例模式的多种方式

    这篇文章介绍了C#实现单例模式的多种方式,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-01-01
  • C#基础知识之new关键字介绍

    C#基础知识之new关键字介绍

    在 C# 中,new 关键字可用作运算符、修饰符或约束,本文主要介绍这三种用法。
    2016-04-04
  • C# 设置防火墙的创建规则

    C# 设置防火墙的创建规则

    这篇文章主要介绍了C# 设置防火墙的创建规则,帮助大家更好的利用c#操作防火墙,感兴趣的朋友可以了解下
    2020-11-11
  • 关于C#基础知识回顾--反射(一)

    关于C#基础知识回顾--反射(一)

    其实说白了,反射就是能知道我们未知类型的类型信息这么一个东西.没什么神秘可讲!反射的核心是System.Type。System.Type包含了很多属性和方法,使用这些属性和方法可以在运行时得到类型信息
    2013-07-07
  • C#遍历文件夹及子目录下所有图片

    C#遍历文件夹及子目录下所有图片

    这篇文章主要为大家详细介绍了C# 遍历文件夹及子目录下所有图片的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01

最新评论