C#线性表

129 阅读2分钟

线性表

线性表的实现方式

顺序表 地址连续,逻辑上相邻的数据元素,在物理位置上也相邻

单链表

​ 双向链表

​ 循环链表

线性表接口

public interface IListDS<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);    //查找元素所在位置
}

顺序表实现

public class SeqList<T>:IListDS<T>
    {
        private T[] data;//用来存储数据
        private int count = 0;//表示存了多少数据

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

        public SeqList() : this(10)//默认构造函数容量为10
        {
            
        }
        
        // 取得数据的个数
        public int GetLength()
        {
            return count;
        }

        public void Clear()
        {
            count = 0;
        }

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

        public void Add(T item)
        {
            if (count == data.Length)
            {
                Console.WriteLine("当前顺序表已经存满,不允许再存入");
            }
            else
            {
                data[count] = item;
                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 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 this[int index]
        {
            get { return GetEle(index); }
        }

        public T GetEle(int index)
        {
            if (index >= 0 && index <= count - 1)//索引存在
            {
                return data[index];
            }
            else
            {
                Console.WriteLine("索引不存在");
                return default(T);
            }
        }

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

单向链表实现

单链表节点

public class Node<T>
    {
        private T data;//存储数据
        private Node<T> next;//指针 用来指向下一个元素

        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.next = next;
        }

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

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

        public Node<T> Next
        {
            get { return next; }
            set { next = value; }
        }
    }

单向链表实现

public class LinkList<T>:IListDS<T>
    {
        private Node<T> head;//存储一个头节点

        public LinkList()
        {
            head = null;
        }

        public int GetLength()
        {
            if (head == null) return 0;
            Node<T> temp = head;
            int count = 1; //默认节点数为1
            while (true)
            {
                if (temp.Next != null)
                {
                    count++;
                    temp = temp.Next;
                }
                else
                {
                    break;
                }
            }
            return count;
        }

        public void Clear()
        {
            head = null;
        }

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

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

                temp.Next = newNode;
            }
            
        }

        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 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> currentNode = temp.Next;
                data = currentNode.Data;
                Node<T> nextNode = temp.Next.Next;
                preNode.Next = nextNode;
            }
            return data;
        }

        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 T GetEle(int index)
        {
            return this[index];
        }

        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;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                return -1;
            }
        }
    }

双向链表

节点中设两个引用域,一个保存前驱节点的地址,叫prev,一个接后继节点的地址,叫next,这样的链表就是双向链表(Doubly Linked List)

循环链表

最后一个节点的引用域不是空引用,而是保存的第一个节点的地址(如果该链表带节点,则保存的是头节点的地址),也就是头引用的值。