C#实现一个顺序表和单链表

1. 顺序表

    定义了一个接口类IListDS,在类SeqList中实现。

1.1 IListDS.cs

namespace _401_线性表
{
    interface IListDS
    {
        int GetLength();
        void Claer();
        bool IsEmpty();
        void Add(T item);
        void Insert(T item, int index);
        T Delete(int index);
        T this[int index] { get; }
        T GetEle(int index);
        int Locate(T value);
    }
}

1.2 SeqList.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _401_线性表
{
    class SeqList : IListDS
    {
        /// 
        /// 顺序表实现方式
        /// 
        /// 
        /// 


        public int count=0;//计数器,表示存了多少个数据
        public T[] data;//用来存储数据

        public SeqList(int size)//size就是最大容量
        {
            data = new T[size];
            count = 0;
        }

        public SeqList() : this(10)//默认构造函数容量是10
        {

        }
        public T this[int index]
        {
            get { return GetEle(index); }
        }
        

        public void Claer()
        {
            count = 0;
        }

        public T Delete(int index)
        {
            T temp = data[index];
            if (index >= 0 && index <= count - 1)
            {
                for(int i=index+1;i=0&&index<=count-1)//索引存在
            {
                return data[index];
            }
            else
            {
                Console.WriteLine("索引不存在");
                return default(T);//取得类型的默认值
            }
            
        }

        /// 
        /// 取得数据的个数
        /// 
        /// 
        public int GetLength()
        {
            return count;
        }

        public void Insert(T item, int index)
        {
            if(index>=0&&index<=count-1)
            {
                for(int i=count-1;i>=index;i--)
                {
                    data[i + 1] = data[i];
                }
                data[index] = item;
                count++;
            }
            else
            {
                Console.WriteLine("插入位置有误");
            }
        }

        public bool IsEmpty()
        {
            return count == 0;
        }

        public int Locate(T value)
        {
            for(int i=0;i

1.3 Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _401_线性表
{
    class Program
    {
        static void Main(string[] args)
        {
            //1.使用BCL中的List线性表
            //List strList = new List();
            //strList.Add("34");
            //strList.Add("3435");
            //strList.Add("6767");

            //strList.Remove("3435");

            //foreach(var temp in strList)
            //{
            //    Console.WriteLine(temp);
            //}
            //Console.WriteLine(strList.IndexOf("34"));
            //Console.WriteLine(strList.Count);
            //Console.ReadKey();

            //2.使用自己定义的顺序表
            SeqList seqList = new SeqList();

            seqList.Add("45");
            seqList.Add("657");
            seqList.Add("243");

            Console.WriteLine(seqList.GetEle(1));
            Console.WriteLine("长度:" + seqList.GetLength());
            seqList.Insert("888", 2);

            seqList.Delete(3);
            Console.WriteLine(seqList.IsEmpty());
            Console.WriteLine(seqList.Locate("45"));

            Console.WriteLine("遍历结果:");
            for (int i = 0; i < seqList.count; i++)
            {

                Console.WriteLine(seqList[i]);
            }
            Console.WriteLine("清空后");
            seqList.Claer();
            Console.WriteLine("长度:" + seqList.GetLength());

            Console.ReadKey();
}
}}

2. 单链表

    定义了一个j节点类Node,接口类IListDS,在类LinkList中实现。

2.1 Node.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _401_线性表
{
    /// 
    /// 单链表的节点
    /// 
    class Node
    {
        private T data;//数据域
        private Node next;//指针,用来指向下一个节点

        public Node()
        {
            data = default(T);
            next = null;
        }
        public Node(T value)
        {
            data = value;
            next = null;
        }
        public Node(T value,Node next)
        {
            data = value;
            this.next = next;
        }
        public Node(Node next)
        {
            this.next = next;
        }

        public T Data { get { return data; } set { data = value; } }
        public Node Next { get { return next; } set { next = value; } }
    }
}

2.2 IListDS.cs

namespace _401_线性表
{
    interface IListDS
    {
        int GetLength();
        void Claer();
        bool IsEmpty();
        void Add(T item);
        void Insert(T item, int index);
        T Delete(int index);
        T this[int index] { get; }
        T GetEle(int index);
        int Locate(T value);
    }
}

2.3 LinkList.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _401_线性表
{
    class LinkList : IListDS
    {
        private Node head;//储存一个头结点

        public LinkList()
        {
            head = null;
        }
        public T this[int index]
        {
            get
            {
                Node temp = head;
                for (int i = 1; i <= index; i++)
                {
                    temp = temp.Next;
                }
                return temp.Data;
            }
        }
        public void Add(T item)
        {
            Node newNode = new Node(item);//根据新数据创建一个节点
            
            if(head==null)
            {
                head = newNode;
            }
            else
            {//把新来的节点放到链表尾部
                
                Node temp = head;

                //要访问到链表尾部
                while(true)
                {
                    if(temp.Next!=null)
                    {
                        temp = temp.Next;
                    }
                    else
                    {
                        break;
                    }
                }
                temp.Next = newNode;
            }
        }

        public void Claer()
        {
            head = null;
        }

        public T Delete(int index)
        {
            T data = default(T);
            if(index==0)
            {
                data = head.Data;
                head = head.Next;
            }
            else
            {
                Node temp = head;
                for(int i=1;i<=index-1;i++)
                {
                    temp = temp.Next;
                }
                Node preNode = temp;
                Node currentNode = temp.Next;
                Node nextNode = temp.Next.Next;
                preNode.Next = nextNode;
            }
            return data;
        }

        public T GetEle(int index)
        {
            return this[index];
        }

        public int GetLength()
        {
            if (head == null) return 0;
            Node temp = head;
            int count = 1;
            while(true)
            {
                if(temp.Next!=null)
                {
                    count++;
                    temp = temp.Next;
                }
                else
                {
                    break;
                }
            }
            return count;
        }

        public void Insert(T item, int index)
        {
            Node newNode = new Node(item);
            if(index==0)//插入到头结点
            {
                newNode.Next = head;
                head = newNode;
            }
            else
            {
                Node temp = head;
                for(int i=1;i<=index-1;i++)
                {
                    //让temp向后移动一个位置
                    temp = temp.Next;
                }
                Node preNode = temp;
                Node currentNode = temp.Next;
                preNode.Next = newNode;
                newNode.Next = currentNode;
            }
        }

        public bool IsEmpty()
        {
            return head == null;
        }

        public int Locate(T value)
        {
            Node temp = head;
            if(temp==null)
            {
                return -1;
            }
            else
            {
                int index = 0;
                while(true)
                {
                    if(temp.Data.Equals(value))
                    {
                        return index;
                    }
                    else
                    {
                        if(temp.Next!=null)
                        {
                            temp = temp.Next;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                return -1;
            }
        }
    }
}

2.4 Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _401_线性表
{
    class Program
    {
        static void Main(string[] args)
        {
//3.使用链表
            LinkList seqList = new LinkList();
            seqList.Add("45");
            seqList.Add("657");
            seqList.Add("243");

            Console.WriteLine(seqList.GetEle(1));
            Console.WriteLine("长度:" + seqList.GetLength());
            seqList.Insert("888", 2);

            seqList.Delete(3);
            Console.WriteLine(seqList.IsEmpty());
            Console.WriteLine(seqList.Locate("45"));

            Console.WriteLine("遍历结果:");
            for (int i = 0; i < seqList.GetLength(); i++)
            {

                Console.WriteLine(seqList[i]);
            }
            Console.WriteLine("清空后");
            seqList.Claer();
            Console.WriteLine("长度:" + seqList.GetLength());
            Console.ReadKey();
        }
    }
}

你可能感兴趣的:(数据结构)