C#数据结构----单链表

本次发布使用C#实现数据结构的单链表的基本操作类
using System;
namespace ShuJuJieGou
{
   //结点类
    public class Node
    {
        object  data;
        Node next;
       //构造函数
        public Node()
        {
            setNode( 0, null);
        }
        public Node(object data)
        {
            setNode(data , null);
        }
        public Node(object  data,Node node)
        {
            setNode(data, node );
        }
        public Node(Node node)
        {
            setNode(node.data, node.next);
        }
        public void setNode(object  dataValue, Node nextValue)
        {
            Data = dataValue;
            Next = nextValue;
       
        //set和get
        public object  Data
        {
            get
            {
                return data;
            }
            set
            {
                data = value;
            }
        }
        public Node  Next
        {
            get
            {
                return next ;
            }
            set
            {
                next = value;
             }
        }     
    }
    class List
    {
        //头指针
        Node head;   
       //当前指针
        int  count;
        //set和get安全存储机制
        public Node  Head
        {
            get
            {
                return head ;
            }
            set
            {
                head  = value;
            }
        }
        public int  Count
        {
            get
            {
                return count;
            }
            set
            {
                count  = value;
            }
        } 

       //链表初始化
        public List()
        {
            Head = null;         
            Count = 0;
        }
        //判断是否链表为空      
        public bool isEmpty()
        {
            return Head == null;
        }
       //头部插入
        public void insetHead(object data)
        {
                Head=new Node (data,Head);           
                Count++;    
        }
        //头部删除
        public object deleteHead()
        {
            Node node;
            if (isEmpty())
            {
                return null;
            }
            else
            {
                node =Head ;//将当前链表的第一个结点复制
                Head = Head.Next; //head指向第二个结点            
                Count--;//链表长度减一
                return node.Data;//返回第一个结点值
        
            }
        }
        //尾部插入
        public void insetTail(object data)
        {
            Node node = new Node(data);
            if (isEmpty())//空链表的处理
            {
                Head = node;
            }
            else
            {
                Node curr = Head;
                while (curr.Next != null)//当前指针循环到尾部
                {
                    curr = curr.Next;
                }
                curr.Next = node;//将尾结点的指针指向插入结点
            }
            Count++; //链表长度加1
        }
        ////尾部删除
        public object deleteTail()
        {
            if (isEmpty())//空链表的处理
            {
                return null;
            }
            else
            {
                Node curr = Head;
                Node finger = null;
                while (curr.Next != null)//当前指针循环到尾部
                {
                    finger = curr;
                    curr = curr.Next;
                }
                finger.Next = null;//将倒数第二个结点Next置为空
                Count--;//链表长度减一
                return curr.Data;//返回尾结点值               
            }
        }
//中间按索引插入元素,addMiddle(1,2)是指在第1个元素之后插入data为2的新结点
        public void addMiddle(int i, object data)
        {
            if (i==0)//表头插入
            {
                insetHead(data);
            }
            else if (i == count)//表末插入
            {
                insetTail(data);
            }
            else
            {
                Node curr = Head;
                Node finger = null;
                while (i > 0)//将当前指针循环到要插入的位置
                {
                    finger = curr;
                    curr = curr.Next;
                    i--;
                }
                Node dataNode = new Node(data, curr);//构造新节点并将其Next指向第i+1个结点
                finger.Next = dataNode;// 将原链表的第i个结点指向插入节点
                Count++;//链表长度加一
            }         
        }

        //中间按索引删除元素,deleteMiddle(1)是删除第1个结点
        public object deleteMiddle(int i)
        {
            if (i==1)//表头删除
            {
               return  deleteHead();
            }
            else if (i == count)//表末删除
            {
                return deleteTail();
            }
            else
            {
                Node curr = Head;
                Node finger = null;
                while (i-1>0)//当前指针循环到删除结点
                {
                    finger = curr;
                    curr = curr.Next;
                    i--;
                }
                finger.Next =curr.Next ;//将第i个结点指向第i+2个结点
                Count--;//链表长度减一
                return curr.Data;//返删除结点值
            }
        }
        //中间按元素删除元素,deleteData(data)是删除元素为data的结点
        public object deleteData(object data)
        {           
             Node curr = head;
             Node finger = null;
    
            if(isEmpty())
            {
                Console.WriteLine("链表为空!无法删除!");
                return null;
            }
            else
            {
                if (head.Data.Equals(data))//删除元素为第一个元素
                {
                    return deleteHead();
                }
                else
                {
                    while (!curr.Data.Equals(data) && curr != null)//当前指针循环到删除结点
                    {
                        finger = curr;
                        curr = curr.Next;
                        if (curr == null & !finger.Data.Equals(data))
                        {
                            Console.WriteLine("链表无此元素!");
                            return null;
                        }                 
                    }
                    if (curr == null)//删除元素为最后一个元素
                    {
                        return deleteTail();
                    }
                    else
                    {          
                       finger.Next = curr.Next;
                       Count--;//链表长度减一
                       return curr.Data;
                    }                  
                }         
            }
        }
       //链表清空
        public void clear()
        {
            Head = null;
        }
      //输出链表
        public void listString()
        {
            Node curr = head;
            Console.WriteLine("\n链表共有{0}个结点,元素如下:",Count);
            while (curr!= null)
            {
                Console.Write("     "+curr.Data);
                curr = curr.Next;               
            }
        }   
    }
    class Program
    {
        //测试
        static void Main(string[] args)
        {
            List list = new List();//构造一个单链表  
                
                 list.insetHead (1);//表头插入元素1
                 list.insetHead(2);//表头插入元素2
                 list.insetHead(3);//表头插入元素3
                 list.insetHead(3);//表头插入元素3
                 list.listString(); //输出链表
                 int a=(int)list.deleteData(3);//删除元素2
                 int b = (int)list.deleteData(3);//删除元素2
                 Console.WriteLine("\n删除链表元素为:"+a);
                 list.listString();         
        }
    }
}

posted on 2007-08-05 18:18  Bēniaǒ  阅读(2011)  评论(5编辑  收藏  举报