一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp.net中C#定义并实现单链表示例

时间:2014-08-28 编辑:简简单单 来源:一聚教程网

示例

 代码如下 复制代码

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)
  {
  }
 }
}


 

示例2

 代码如下 复制代码

Node 单链表#region Node 单链表
    /**////


    /// 单链表
    ///

    public class Node
    {
        public object item;
        public Node next;
        private  Node head;
        private int index;

        构造函数#region 构造函数
        public Node()
        {
            head=new Node("head");
            index=0;       
        }
        public Node(object v)
        {
            item=v;next=null;
        }
        /**////


        /// 可以自定义开始头节点
        ///

        public void headNode()
        {
           head=new Node("head");
           index=0;
        }
        #endregion

        插入#region 插入
        /**////


        /// 从后面插入
        ///

        /// 要插入的数据
        public void insertNode(object ob)//从后面插入
        {
            Node x=head;
            Node t=new Node(ob);

            for(int i=0;i                 x=x.next;

            t.next=x.next;
            x.next=t;

            index=index+1;
        }
        /**////


        /// 指定插入(插入指定参数的下面)l从0开始插入第一个的前面
        ///

        /// 要插入的数据
        /// 要插入的数据的位置
        /// true为插入成功,反之失败
        public bool insertNode(object ob,int l)//指定插入(插入指定参数的下面)l从0开始插入第一个的前面
        {
            if((l>=0)&&(l<=index))
            {
                Node x=head;
                for(int i=0;i                     x=x.next;

                Node t=new Node(ob);
                t.next=x.next;
                x.next=t;

                index=index+1;
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        删除#region 删除
        /**////


        /// 删除最后一个
        ///

        public void delNode()//删除最后一个
        {
            Node x=head;
            for(int i=0;i                 x=x.next;
            x.next=x.next.next;
            index=index-1;
        }
        /**////
        /// 指定删除(l从1开始)
        ///

        /// 指定删除位置
        /// true删除成功,反之失败
        public bool delNode(int l)//指定删除l从1开始
        {
            if((l>0)&&(l<=index))
            {
                Node x=head;
                for(int i=0;i                     x=x.next;
                x.next=x.next.next;
                index=index-1;
                return true;
            }           
            else
            {
                return false;
            }
        }
        /**////
        /// 查找删除
        ///

        /// 输入要删除的输入
        /// true删除成功,反之失败
        public bool delNode(object ob)//查找删除
        {
            Node x=head;
            Node t;
            bool b=false;
            for(int i=0;i             {
               
                t=x.next;
                if(t.item==ob)
                {
                    x.next=x.next.next;
                    index=index-1;
                    b=true;
                }
                x=x.next;
            }
            return b;

        }
        #endregion

        上下移动#region 上下移动
        /**////


        /// 上移动
        ///

        /// 指定要移动的位置
        public void Upnode(int l)
        {
            if((l>1)&&(l<=index))
            {
                object o=this.showNode(l-1);
                this.delNode(l-1);
                this.insertNode(o,l-1);
            }
        }
        /**////
        /// 下移动
        ///

        /// 指定要移动的位置
        public void Downnode(int l)
        {
            if((l>0) && (l             {
                object o=this.showNode(l);
                this.delNode(l);
                this.insertNode(o,l);
            }
        }
        #endregion

        排序 和反链#region 排序 和反链
        /**////


        /// 排序
        ///

        /// true(正)从小到大,false(反)
        public void compositorNode(bool b)//排序true(正)从小到大,false(反)
        {
            if(b==true)
            {
                for(int i=1;i                     for(int j=1;j                       if(this.CharNode(j)>this.CharNode(j+1))
                        this.Downnode(j);                   
            }
            else
            {
                for(int i=1;i                     for(int j=1;j                         if(this.CharNode(j)                            this.Downnode(j);
            }
        }
        private char CharNode(int l)
        {
           string s=this.showNode(l).ToString();
           char[] c=s.ToCharArray();
           return c[0];
        }
        /**////
        /// 反链
        ///

        public void rollbackNode()//反链(其实是反链head后的)
        {
            Node t,y,r=null;
            y=head.next;
            while(y!=null)
            {
                t=y.next;y.next=r;r=y;y=t;
            }
            head.next=r;//把head链上最后一个

        }
        #endregion

        显示节点和接点数#region 显示节点和接点数
        /**////


        /// 返回节点数方法
        ///

        /// 节点数
        public int showNodenum()
        {
            return index;
        }
        /**////
        /// 显示指定数据
        ///

        /// 指定位置
        /// 返回数据
        public object showNode(int l)//显示指定l从1开始
        {
            if((l<=index)&&(l>0))
            {
                Node x=head;
                for(int i=0;i                 {
                    x=x.next;
                }
                return x.item;
            }
            else
            {
                return head.item;
            }
        }
        /**////
        /// 显示所有
        ///

        /// 返回一个ObQueue对象
        public ObQueue showAllNode()//显示所有
        {
            ObQueue oq=new ObQueue();
            Node x=head;
            for(int i=0;i             {
                x=x.next;
                oq.qput(x.item);
            }       
            return oq;
        }
        #endregion

    }
    #endregion

    循环链表#region 循环链表
    /**////


    /// 循环链表
    ///

    public class CircularList
    {
        public  class Node
        {
            public object val;
            public Node next;
            public Node(object v)
            {
                val=v;
            }
        }
        public Node next(Node x)
        {
            return x.next;
        }
        public object val(Node x)
        {
            return x.val;
        }
        /**////
        /// 插入
        ///

        ///
        ///
        ///
        public Node insert(Node x,object v)
        {
            Node t=new Node(v);
            if(x==null)t.next=t;
            else{t.next=x.next;x.next=t;}
            return t;

        }
        /**////


        /// 移除
        ///

        ///
        public void remove(Node x)
        {
            x.next=x.next.next;
        }
    }
    #endregion

    间接用循环链表解决,约瑟芬问题#region 间接用循环链表解决,约瑟芬问题
    /**////


    /// 间接用循环链表解决,约瑟芬问题
    ///

    public class Josephuses
    {
        public static object GetJosephuses(object[] item,int M)
        {
            int N=item.Length;
            CircularList L=new CircularList();
            CircularList.Node x=null;
            for(int i=1;i<=N;i++)
            {
                x=L.insert(x,item[i-1]);
            }
            while(x!=L.next(x))
            {
                for(int i=1;i                 {
                    x=L.next(x);
                }
                L.remove(x);
            }
            return L.val(x);
        }
    }
    #endregion

    循环链表列子(约瑟芬问题)#region 循环链表列子(约瑟芬问题)
    /**////


    /// 循环链表列子(约瑟芬问题)
    ///

    public class Josephus
    {
        public static object GetJosephus(object[] item,int M)
        {
            object[] o=item;
            int N=o.Length;
            Node t=new Node(o[0]);
            Node x=t;
            for(int i=1;i             {
                x=(x.next=new Node(o[i]));
            }
            x.next=t;
            while(x!=x.next)
            {
                for(int i=1;i                 x.next=x.next.next;
            }
            return x.item;
        }
    }
    #endregion

}

热门栏目