C# 数据结构 - 线性表和单链表的实现

 

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

namespace 线性表
{
    interface IlistDS<T>  //接口
    {
        int GetLength();
        T Delete(int index);
        T GetEle(int index);
        int Locate(T value); 
        bool IsEmpty();
        void Insert(T item, int index);
        void Add(T item);
        void Clear();
        T this[int index] { get; } //索引器 取值
    }
}


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

namespace 线性表
{
    class SeqList<T>:IlistDS<T> //顺序表(线性表)
    {
        int Count;//表示储存了多少个数据
        private T[] data;//用来存储数据

        public SeqList(int size)  //size为最大容量,构造方法初始化数组
        {
            data = new T[size];
            Count = 0;
        }

        public SeqList():this(10) //默认构造函数 容量为10
        {
        
        }
        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)
            {
                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 bool IsEmpty()
        {
            return Count == 0;
        }

        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 void Add(T item)
        {
            if (Count == data.Length)
            {
                Console.WriteLine("数组已满无法添加");
            }
            else 
            {
                data[Count] = item;
                Count++;
            }
        }

        public void Clear()
        {
            Count = 0;
        }

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

        public int GetLength()
        {
            return Count;
        }

    }
}


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

namespace 线性表
{
    class Node<T> //实现单链表节点类
    {
        private T data;//用于存储数据
        private Node<T> next;//指针 用来指向下一个数据

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

        public Node() 
        {
            data = default(T);
            next = null;
        }
        public T GetData
        {
         get{return data;}
         set{data = value;}
        }
        public Node<T> GetNext
        {
            get { return next; }
            set { next = value; }
        }
    }
}

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

namespace 线性表
{
    class LinkList<T>:IlistDS<T> // 实现链表
    {
        public Node<T> head;//头节点
        public LinkList()
        {
            head = null;
        }
        public int GetLength()
        {
            int Count = 1;
            if (head == null)
            {
                return 0;
            }
            else 
            {
                Node<T> temp = head;
                while (true) 
                {
                    if(temp.GetNext!=null)
                    {
                        Count++;
                        temp = temp.GetNext;
                    }
                    else
                    {
                    break;
                    }
                }
                return Count;
            }
        }

        public T Delete(int index)
        {
            T data = default(T);
            if (index == 0)
            {
                data = head.GetData;
                head = head.GetNext;
            }
            else 
            {
                Node<T> temp = head;
                for (int i = 1; i <= index - 1; i++)
                {
                    temp = temp.GetNext;
                }
                Node<T> preNode = temp;
                Node<T> newNode = temp.GetNext;
                data = newNode.GetData;
                Node<T> curNode = temp.GetNext.GetNext;
                preNode.GetNext = curNode;
            }
            return data;
        }

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

        public int Locate(T value)
        {
            Node<T> temp = head;
            int count = 0;
            if(temp == null)
            {
                return -1;
            }
            else
            {
            while (true)
              { 
                if (temp.GetData.Equals(value))
                {
                    return count;
                }
                else 
                {
                    if (temp.GetNext != null)
                    {
                        count++;
                        temp = temp.GetNext;
                    }
                    else 
                    {
                        break;
                    }
                }
              }
            return -1;
            }
        }

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

        public void Insert(T item, int index)
        {
            Node<T> newNode = new Node<T>(item);
            if (index == 0)
            {
                newNode.GetNext = head;
                head = newNode;
            }
            else 
            {
                Node<T> temp = head;
                for (int i = 1; i <= index-1; i++)
                {
                    temp = temp.GetNext;
                }
                Node<T> preNode = temp;
                Node<T> curNode = temp.GetNext;
                preNode.GetNext = newNode;
                newNode.GetNext = curNode;
            }
        }

        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.GetNext != null)
                    {
                        temp = temp.GetNext;
                    }
                    else
                    {
                        break;
                    }
                }
                temp.GetNext = newNode;
            }
        }

        public void Clear()
        {
            head = null;
        }

        public T this[int index]
        {
            get 
            {
                Node<T> temp = head;
                for (int i = 0; i <index ; i++)
                {
                    temp = temp.GetNext;
                }
                return temp.GetData;
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值