浅析数据结构系列(二)

前一篇大概介绍了一下数据结构与算法是个什么东东,那么这一篇我们正式来介绍一下常用的数据结构的啦~

  • 线性表
    线性表是最简单、最基本、最常用的数据结构。线性表是线性结构的抽象(Abstract),线性结构的特点是结构中的数据元素之间存在一对一的线性关系。这种一对一的关系指的是数据元素之间的位置关系,即:( 1)除第一个位置的数据元素外,其它数据元素位置的前面都只有一个数据元素;( 2)除最后一个位置的数据元素外,其它数据元素位置的后面都只有一个元素。也就是说,数据元素是一个接一个的排列。因此,可以把线性表想象为一种数据元素序列的数据结构。
    线性表就是位置有先后关系,一个接着一个排列的数据结构。
    线性表的实现方式有下面几种 顺序表 单链表 双向链表 循环链表

  • 顺序表
    在计算机内,保存线性表最简单、最自然的方式,就是把表中的元素一个接一个地放进顺序的存储单元,这就是线性表的顺序存储(Sequence Storage)。线性表的顺序存储是指在内存中用一块地址连续的空间依次存放线性表的数据元素,用这种方式存储的线性表叫顺序表(Sequence List),如图所示。顺序表的特点是表中相邻的数据元素在内存中存储位置也相邻。
    C#语言中的数组在内存中占用的存储空间就是一组连续的存储区域,因此,数组具有任意存取的特点。所以,数组天生具有表示顺序表的数据存储区域的特性。

具体的代码实现如下:

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

namespace DataStructure
{
    public interface IList<T>
    {
        /// 
        /// 获取长度
        /// 
        /// 
        int GetLength();

        /// 
        /// 清空数据
        /// 
        void Clear();

        /// 
        /// 判断是否为空
        /// 
        /// 
        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);
    }
}

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

namespace DataStructure
{
    /// 
    /// 顺序表实现方式
    /// 
    /// 
    public class SeqList<T> : IList<T>
    {

        /// 
        /// 用来存储数据
        /// 
        private T[] data;
        /// 
        /// 用来存储了多少个数据
        /// 
        private int count = 0;


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

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

        }

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


        /// 
        /// 添加数据
        /// 
        /// 
        public void Add(T item)
        {
            if (count == data.Length)
            {
                Console.WriteLine("当前顺序表已经存满了,不允许再存数据了~");
            }
            else
            {
                data[count] = item;
                count++;
            }
        }

        public void Clear()
        {
            count = 0;
        }

        /// 
        /// 删除一个元素
        /// 
        /// 
        /// 
        public T Delete(int index)
        {
            T temp = data[index];
            for (int i = index + 1; i < count; i++)
            {
                data[i - 1] = data[i];//把数据向前移动
            }

            count--;

            return temp;
        }

        /// 
        /// 取元素
        /// 
        /// 
        /// 
        public T GetEle(int index)
        {
            if (index >= 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)
        {
            for (int i = count - 1; i >= index; i--)
            {
                data[i + 1] = data[i];
            }

            data[index] = item;

            count++;
        }

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

        public int Locate(T value)
        {
            for (int i = 0; i < count; i++)
            {
                if (data[i].Equals(value))
                {
                    return i;
                }
            }

            return -1;//表示值不存在
        }
    }
}

  • 单链表
    顺序表是用地址连续的存储单元顺序存储线性表中的各个数据元素,逻辑上相邻的数据元素在物理位置上也相邻。因此,在顺序表中查找任何一个位置上的数据元素非常方便,这是顺序存储的优点。但是,在对顺序表进行插入和删除时,需要通过移动数据元素来实现,影响了运行效率。线性表的另外一种存储结构——链式存储(Linked Storage),这样的线性表叫链表(Linked List)。链表不要求逻辑上相邻的数据元素在物理存储位置上也相邻,因此,在对链表进行插入和删除时不需要移动数据元素,但同时也失去了顺序表可随机存储的优点。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStructure
{
    /// 
    /// 节点类
    /// 
    public class Node<T>
    {
        private T data;//存储数据
        private Node<T> next;//指针,用来指向下一个元素的

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

        public Node(T value)
        {
            data = value;
            next = null;
        }


        public Node(T value, Node<T> next)
        {
            this.data = value;
            this.next = next;
        }

        public Node(Node<T> next)
        {
            this.data = default(T);
            this.next = next;
        }

        public T Data
        {

            get { return data; }
            set { data = value; }
        }

        public Node<T> Next
        {

            get { return next; }
            set { next = value; }
        }



    }
}

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

namespace DataStructure
{
    public class LinkList<T> : IList<T>
    {
        /// 
        /// 头结点
        /// 
        private Node<T> head;//存储一个头结点


        public LinkList()
        {
            head = null;

        }


        public T this[int index]
        {
            get
            {
                Node<T> temp = head;
                for (int i = 1; i < index; i++)
                {
                    //让temp后移一个位置
                    temp = temp.Next;
                }

                return temp.Data;
            }

        }

        public void Add(T item)
        {
            Node<T> newNode = new Node<T>(item);//根据新的数据创建一个新的节点
            //如果头结点不为空,把新来的结点放到链表的尾部, 要访问到链表的尾节点
            if (head != null)
            {
                Node<T> temp = head;//临时节点
                while (true)
                {
                    if (temp.Next != null)
                    {
                        temp = temp.Next;
                    }
                    else
                    {
                        break;
                    }
                }
                temp.Next = newNode;
            }
            else//如果头结点为空,那么这个新的结点就是头结点
            {
                head = newNode;//把新来的结点放到链表的尾部
            }


        }

        public void Clear()
        {
            head = null;

        }

        public T Delete(int index)
        {
            T data = default(T);

            if (index == 0)//删除头节点
            {
                data = head.Data;
                head = head.Next;//把头节点的下一个节点给头节点
            }
            else
            {
                Node<T> temp = head;
                for (int i = 1; i <= index - 1; i++)
                {
                    //让temp向后移动一个位置
                    temp = temp.Next;
                }

                Node<T> preNode = temp;
                Node<T> cureentNode = preNode.Next;
                Node<T> nextNode = temp.Next.Next;
                preNode.Next = nextNode;

                data = cureentNode.Data;
            }

            return data;
        }

        public T GetEle(int index)
        {
            Node<T> temp = head;
            for (int i = 1; i < index; i++)
            {
                //让temp后移一个位置
                temp = temp.Next;
            }

            return temp.Data;
        }

        public int GetLength()
        {


            if (head == null)
            {
                return 0;
            }

            Node<T> 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<T> newNode = new Node<T>(item);
            if (index == 0)//插入到头结点
            {
                newNode.Next = head;
                head = newNode;
            }
            else
            {
                Node<T> temp = head;
                for (int i = 1; i <= index - 1; i++)
                {
                    //让temp向后移动一个位置
                    temp = temp.Next;
                }

                Node<T> preNode = temp;
                Node<T> currentNode = temp.Next;
                preNode.Next = newNode;
                newNode.Next = currentNode;
            }
        }

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


        /// 
        /// 定位位置
        /// 
        /// 
        /// 
        public int Locate(T value)
        {
            Node<T> 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;
                            index++;
                        }
                        else
                        {
                            break;
                        }

                    }
                }

                return -1;
            }


        }
    }
}

以上就是以两种不同的方式实现的List线性表,这里仅作记录作为参考,高手请绕道~

你可能感兴趣的:(c#,数据结构,单链表)