C#数据结构单链表的简单实现

using System;
using System.Collections.Generic;
using System.Text;

namespace linklist
{
    public class Node  //链表的结点类定义
    {
        int data;//字段data表示结点的数据域
        internal Node next;//字段next表示结点的指针域,实际上这是一个引用
        public int Data//属性
        {
            set { data = value; }
            get { return data; }
        }
        public Node()  //初始化一个结点,数据域默认为0,指针域默认为空
        {
            Data = 0;
            next = null;
        }
        public Node(int data1, Node next1)//初始化一个结点,指定数据域和指针域
        {
            Data = data1;
            next = next1;
        }
        public Node(int data1)//初始化一个结点,指定数据域,指针域默认为空
        {
            Data = data1;
            next = null;
        }
        public string ToString()
        {
            return (Data + "");
        }
        public static void PrintNode(Node ptr)//显示引用所指向的结点的值域
        {
            if (ptr == null)
                return;
            Console.WriteLine("node:{0}", ptr.Data);
        }
    }
    public class LinkList
    {
        int count;//表示链表结点总数
        public Node head;//链表头指针
        public int Count
        {
            set { count = value; }
            get { return count; }
        }
        public LinkList()//构造函数,初始化单链表为空链表
        {
            count = 0;
            head = null;
        }
        public virtual void PrintList(LinkList list)//按顺序显示单链表所有结点,参数是链表对象
        {
            Node p = list.head;
            Console.Write("(");
            while (p != null)
            {
                Console.Write(p.ToString());
                if (p.next == null)  //当前结点是最后一个结点
                    break;
                else
                {
                    Console.Write(",");
                    p = p.next;
                }
            }
            Console.Write(")");
            Console.WriteLine();
        }
        public virtual Node Contains(int data1)//在链表中查找值为data1的结点,找到返回该结点的引用,找不到,则返回空引用
        {
            Node ptr;
            ptr = head;
            while (ptr != null && ptr.Data != data1)
                ptr = ptr.next;
            return (ptr);
        }
        public virtual Node Locate(int i)//在链表中查找第i个结点,找到则返回该结点的引用,找不到则返回空引用
        {
            Node ptr;
            if (i <= 0 || i > count)
            {
                return (null);
            }
            ptr = head;
            int j = 1;
            while (j < i && ptr != null)
            {
                ptr = ptr.next; j++;
            }
            return (ptr);
        }
        public virtual void AddFirst(int x)//在链表头结点前插入结点,可用于建立链表
        {
            head =new Node (x ,head);
            count++;
        }
        public bool Add(int x) //在链表尾部添加一个结点, 可用于建立链表
        {
            if (count == 0)
            {
                head = new Node(x, head);
                count++; return true;
            }
            else
            {
               Node newnode = new Node(x);
                Node p = Locate(count);
                newnode.next = p.next;
                p.next = newnode;
                count++;
                return true ;
            }

        }
         public virtual void PrintList(Node list)//按顺序显示单链表中从指定结点开始到尾部的所有结点,参数是一个结点的引用
         {
             Node p = list;
             Console.Write("(");
             while (p != null)
             {
                 Console.Write(p.ToString ());
                 if (p.next == null)
                     break;
                 else
                 {
                     Console.Write(",");
                     p = p.next;
                 }
             }
             Console.Write(")");
             Console.WriteLine();
         }
        
        public virtual void PrintBackward()//反向显示单链表所有结点值
         {
             Console.Write("(");
             for (int i = count; i > 0; i--)
             {
                 Console.Write("{0}", Locate(i).Data);
                 if (i>1)
                     Console.Write(",");
             }
                
                 Console.Write(")");
         }
         public virtual void AddAfter(Node p, int x)//后插操作,在p结点后插入值为x的结点
         {
             if (p == null)
             {
                 Console.WriteLine("指定引用是空引用,无法插入!");
                 return;
             }
             Node newnode = new Node(x);
             newnode.next = p.next;
             p.next = newnode;
             count++;
         }
         public virtual void AddBefore(Node p, int x)//前插操作,在p结点之前插入值为x的结点
         {
             if (p == null)
             {
                 Console.WriteLine("指定引用是空引用,无法插入!");
                 return;
             }
             Node newnode = new Node(x);
             Node pre = head;
             if (p == head)
             {
                 newnode.next = head; head = newnode; count++; return;
             }
             while (pre.next != p)
                 pre = pre.next;
             newnode.next = p;
             pre.next = newnode;
             count++;

         }
         public virtual bool Insert(int i, int x)// 在链表第i个位置插入值为x的结点
         {
             Node newnode, prt;
             if (i <= 0 || i > count)
             {
                 Console.WriteLine("插入位置不合法!"); return false ;
             }
             newnode =new Node(x ) ;
             if (newnode ==null )
             {
                 Console.WriteLine("内存空间申请失败!"); return false ;
             }
             newnode.Data = x;
             if (i == 1)
             {
                 newnode.next = head;
                 head = newnode;
                 count++;
                 return true;
             }
             prt = Locate(i-1);
             if (prt != null)
             {
                 AddAfter(prt, x);
                 return true;
             }
             else
             {
                 Console.WriteLine("error"); return false;
             }
         }
         public virtual void RemoveAfter(Node p)//删除p对象后的结点
         {
             Node q;
             q = p.next;
             p.next = q.next;
             count--;
         }
         public virtual bool RemoveAt(int i)//删除第i个元素
         {
             Node p, q;
             if (i == 1)
             {
                 q = head; head = head.next;
                 count--;
                 return true;
             }
             p = Locate(i -1);
             if (p != null && p.next!= null)
             {
                 RemoveAfter(p);
                 return (true);
             }
             else
             {
                 Console.WriteLine("error");
                 return (false );
             }
         }
         public virtual bool Remove(int x)//删除值为x的结点
         {
             Node p = Contains(x);
             Node q;
             q = p.next;
             p.Data = q.Data;
             p.next = q.next;
             count--;
             return true;
         }
         public virtual void Reverse()//逆置链表,即把链表变为反向链表
         {
             int t = count;
             for (int i = 1; i <=count/2; i++)
                 {
                     Node a = Locate(i);
                     Node b = Locate(t);
                     Node temp = new Node(a.Data);
                     a.Data = b.Data;
                     b.Data = temp.Data;
                     t--;
                 }
                
         }
         public void Clear() //清空链表
         {
             head  = null;
             count = 0;
         }
       
    }//end of class LinkList

    class Program
    {
        static void Main(string[] args)
        {
            LinkList list = new LinkList();//实例化
            for (int i = 1; i < 7; i++)
            {
                list.AddFirst(i * 10);
                //list.Add(i*10);
            }
            list.PrintList(list);
            Console.WriteLine();
            list.PrintBackward();
            Console.WriteLine();
            Console.WriteLine("以链表中的某个结点开始输出其余结点:");
            list.PrintList(list.head.next);
            Console.WriteLine();
            Console.WriteLine("删除值为50的结点");
            list.Remove(50);
            list.PrintList(list );
            Console.WriteLine();

           Console.WriteLine("查找第8个结点值:");
            Node ptr = list.Locate(8);
            if (ptr == null)
                Console.WriteLine("没有找到指定的结点");
            else
            {
                Console.WriteLine("第8个结点值为:");
                Node.PrintNode(ptr);
            }
             ptr= list .Locate (2);
            if (ptr == null)
                Console.WriteLine("没有找到指定的结点");
            else
            {
                Console.WriteLine("第2个结点值为:");
                Node.PrintNode(ptr );
            }
            Console.WriteLine("查找值为10的结点:");
            ptr = list.Contains(30);
            if (ptr == null)
                Console.WriteLine("没有找到指定的结点");
            else
            {
                Console.WriteLine("找到指定结点,值为:");
                Node.PrintNode(ptr);
            }
            Console.WriteLine();
            Console.WriteLine("进行后插和前插");
            list.AddAfter(ptr,111);
            list.PrintList(list);
            Console.WriteLine("链表长度={0}",list.Count );
            list.AddBefore(ptr, 222);
             list.PrintList(list);
            Console.WriteLine("链表长度={0}", list.Count);
            Console.WriteLine();
            Console.WriteLine("在指定位置插入指定值的结点后:Insert(4, 666)");
            list.Insert(4, 666);
            list.PrintList(list);
            Console.WriteLine("链表长度={0}", list.Count);
            Console.WriteLine();
            Console.WriteLine("删除指定结点:RemoveAt(1)");
            list.RemoveAt(1);
            list.PrintList(list);
            Console.WriteLine("链表长度={0}", list.Count);
            Console.WriteLine();
            Console.WriteLine("删除指定值的结点:Remove(20)");
            list.Remove(20);
            list.PrintList(list);
            Console.WriteLine("链表长度={0}", list.Count);
            Console.WriteLine();
            list.Add(123);
            Console.WriteLine("尾部添加结点:Add(123)");
            list.PrintList(list);
            Console.WriteLine("链表长度={0}", list.Count);
            list.Reverse();
            Console.WriteLine();
            Console.WriteLine("逆置链表");
            list.PrintList(list);
            Console.WriteLine("链表长度={0}", list.Count);
            list.Clear();
            Console.WriteLine();
            Console.WriteLine("执行clear");
            list.PrintList(list);
            Console.WriteLine("链表长度={0}", list.Count);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值