数据结构(线性结构,单链表,双链表)

本文详细介绍了三种常见数据结构的实现:线性表(顺序表和链表)、单链表以及双链表。内容包括它们的基本操作如添加、删除、查找等,并提供了具体的C#代码实现。这些数据结构是计算机科学的基础,广泛应用于各种算法和数据管理中。
摘要由CSDN通过智能技术生成
//接口
 interface IListDS<T>
    {
        int GetLength();//获取长度
        void Clear();//清空

        bool IsEmpty();//判断列表是否为空

        void Append(T item);//添加数据

        void Insert(T item,int i);//在某个位置添加数据

        T Delete(int i);//根据位置删除数据

        T GetElement(int i);//根据位置得到元素

        int Locate(T value);//根据元素得到位置
    }

//线性表

 class SeqLise<T> : IListDS<T>
    {
        private int maxsize;
        private T[] data;
        private int last;
        public int Maxsize
        {
            get { return maxsize; }
            set { maxsize = value; }
        }
        public int Last
        {
            get { return last; }
        }
        public SeqLise(int size)
        {
            data = new T[size];
            maxsize = size;
            last = -1;
        }

        public T this[int index]
        {
            get { return data[index]; }
            set { data[index] = value; }
        }

        bool isFull()
        {
            if (Last == maxsize-1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public void Append(T item)
        {
            if (isFull())
            {
                Console.WriteLine ("列表已存满");
                return;
            }
            data[++last] = item;
        }

        public void Clear()
        {

             last=-1;
            Array.Clear(data,0,maxsize);
           
        }

        public T Delete(int i)
        {
            T tmp = default(T);
            if (IsEmpty())
            {
                Console.WriteLine("List is empty !列表为空");
                return tmp ;
            }
            if (i<1||i>last +1)
            {
                Console.WriteLine("Position is error !无法删除");
                return tmp;
            }
            if (i==last +1)
            {
                tmp = data[last];
            }
            else
            {
                for (int k=i ;k<=last ;++k)
                {
                    data[k-1]=data[k];
                    data[k] = default(T);
                }
              tmp= data[i - 1];
            }
            --last;
            return tmp;
        }

        public T GetElement(int i)
        {
            if (IsEmpty()||i>last+1)
            //123456
            //012345
            {
                Console.WriteLine("List is empty or Position is error !");
                return default(T);
            }
            return data[i-1];
        }

        public int GetLength()
        {
            return last + 1;
        }

        public void Insert(T item, int i)
        {
            if (isFull ())
            {
                Console.WriteLine("List is full 列表已存满");
                return;
            }
            if (i<1||i>last+2)
            {
                Console.WriteLine("Position is error 无法插入");
                return;
            }
            if (i==last+2)
            {
                data[last+1] = item;
            }
            else
            {
                for (int k =last;k>=i-1; --k)
                {
                  data[k+1]=data[k];
                }
                data[i-1]=item;
            }
            ++last;
        }

        public bool IsEmpty()
        {
            if (Last==-1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int Locate(T value)
        {
            if (IsEmpty())
            {
                Console.WriteLine("List is empty!");
                return -1;
            }
            int i = 0;
            for (i = 0; i <= last; ++i)
            {
                if (data[i].Equals(value))
                {
                    break;
                }
            }
            if (i>last )
            {
                return -1;
            }
            return i+1;
        }
    }

//单链表

 class Node<T>
    {
        private T data;//数据域 存储数据的对象

        private Node<T> next;//引用域  指向下一个对象

        public Node (T val,Node <T> p)
        {
            data = val;
            next = p;
        }
        public Node( Node<T> p)
        {         
            next = p;
        }
        public Node(T val)
        {
            data = val;
            next = null;
        }
        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;}
        }
    }
 class LinkList<T> : IListDS<T>
    {

        private Node<T> head;
        public Node<T> Head
        {
            get { return head; }
            set { head = value; }
        }
        public LinkList()
        {
            head = null;
        }
        public void Append(T item)
        {
            Node<T> q = new Node<T>(item);
            Node<T> p = new Node<T>();
            if (head ==null)
            {
                head = q;
                return;
            }
            p = head;
            while (p.Next!=null )
            {
                p = p.Next;

            }
            p.Next = q;
        }
        public T this [int i]
        {
            get { return GetElement(i);}
        }
        public bool isEmpty()
        {
            if (head==null )
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public void Clear()
        {
            head = null;
        }

        public T Delete(int i)
        {
            if (isEmpty ()||i<1)
            {
                Console.WriteLine("List is empty or position is errors !");
                return default(T);
            }
            Node<T> q = new Node<T>();
            if (i==1)
            {
                q = head;
                head = head.Next;
                return q.Data ;
            }
            Node<T> p = head ;       
            int j = 1;
            while (p.Next != null && j < i)
            {
                ++j;
                q = p;
                p = p.Next;
            }
            if (j == i)
            {
                q.Next = p.Next;
                return p.Data;
            }
            else
            {
                Console.WriteLine("The ith node is not exist!");
                return default(T);
            }
        }

        public T GetElement(int i)
        {
            if (isEmpty())
            {
                Console.WriteLine("List is empty!");
                return default(T);
            }
            Node<T> p = new Node<T>();
            p = head;
            int j = 1;
            while (p.Next!=null &&j<i)
            {
                ++j;
                p = p.Next;
            }
            if (j==i)
            {
                return p.Data;
            }
            else
            {
                Console.WriteLine("The ith node is not exist!");
                return default(T);
            }
        }

        public int GetLength()
        {
            Node<T> p = head;
            int len = 0;
            while (p!=null )//节点是否存在
            {
                ++len;
                p = p.Next;
            }
            return len;
        }

        public void Insert(T item, int i)
        {
            if (IsEmpty ()||i <1)
            {
                Console.WriteLine("List is empty or position is errors !");
                return ;
            }
            if (i==1)
            {
                Node<T> q = new Node<T>(item);
                q.Next = head;
                head = q;
                return;
            }
            Node<T> p = head;
            Node<T> r = new Node<T>();
            int j = 1;
            while (p.Next!=null ||j<i)
            {             
                r  = p;
                p = p.Next;
                ++j;
            }
            if (j==i)
            {
                Node<T> q = new Node<T>(item);            
                q.Next = p;
                r.Next = q;
            }
        }

        public bool IsEmpty()
        {
            if (head == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int Locate(T value)
        {
            if (isEmpty ())
            {
                Console.WriteLine("List is empty");
                return -1;
            }
            Node<T> p = new Node<T>();
            p = head;
            int i = 1;
            while (!p.Data.Equals(value )&&p.Next!=null )
            {
                p = p.Next;
                ++i;               
            }
            if (p.Data.Equals(value))
            {
                return i;
            }
            else
            {
                Console.WriteLine("数据不存在");
                return -2;
            }
           
        }
    }

//双链表

```csharp
 class dbLinkList<T> : IListDS<T>
    {
        private DoubleNode<T> head;//头
        private DoubleNode<T> tail;//尾
        private int length;//长度
        public DoubleNode<T> Head
        {
            get { return head; }
            set { head = value; }
        }
        public DoubleNode<T> Tail
        {
            get { return tail; }
            set { tail = value; }
        }
        public int Length
        {
            get { return length; }
        }
        public dbLinkList ()
        {
            head = null;
            tail = new DoubleNode<T>(default(T));
            length = 0;
        }
        public T this[int i]
        {
            get { return GetNode(i).Data ; }
        }
        public void Append(T item)
        {
            DoubleNode<T> q = new DoubleNode<T>(item);
            if (head==null )
            {
                head = q;
                head.Next = tail;
                tail.Prev = head;
            }
            else
            {
                DoubleNode<T> temp = tail.Prev;
                temp.Next = q;
                q.Prev = temp;
                q.Next = tail;
                tail.Prev = q;
            }
            ++length;
        }


        public void Clear()
        {
            head = null;
            tail = new DoubleNode<T>(default(T));
            length  = 0;
        }

        public T Delete(int i)
        {
            T data;
            if (i==1)
            {
                data = head.Data;
                head = head.Next;
                --length;
            }
            else if (i>GetLength()||i<1)
            {
                Console.WriteLine("The ith does not exist!数值错误");
                data = default(T);
            }
            else 
            {
                data = GetNode(i).Data;
                DoubleNode<T> tempNext = GetNode(i).Next ;
                DoubleNode<T> tempPrev = GetNode(i).Prev;
                tempNext.Prev = tempPrev;
                tempPrev.Next = tempNext;
                --length;
            }
            return data;
        }

        public T GetElement(int i)
        {
            return GetNode(i).Data;
        }

        public int GetLength()
        {
            return length;
        }

        public void Insert(T item, int i)
        {
            DoubleNode<T> q = new DoubleNode<T>(item);
            if (i==1)
            {
                DoubleNode<T> temp = head;
                head = q;
                temp.Prev = head;
                head.Next = temp;
                ++length;
            }
            else if (i>GetLength())
            {
                Append(item );
            }
            else if (i<1)
            {
                Console.WriteLine("The ith does not exist!");
                return;
            }
            else
            {
                DoubleNode<T> temp = GetNode(i);
                DoubleNode<T> tempprev = GetNode(i).Prev;
                temp.Prev = q;
                tempprev.Next = q;
                q.Prev = tempprev;
                q.Next = temp;
                ++length;
            }
        }

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

        public int Locate(T value)
        {
            for (int i = 1; i <= GetLength (); ++i)
            {
                if (GetNode(i).Data.Equals(value ))
                {
                    return i;
                }
            }
            Console.WriteLine("Value does not exist!");
            return -1;
        }
        private DoubleNode <T>GetNode(int i)
        {
            if (i==1)
            {
                if (head!=null )
                {
                    return head;
                }
                else
                {
                    Console.WriteLine("List is empty!");
                    return null ;
                }
            }
            else if (i==GetLength())
            {
                return tail .Prev;
            }
            else if (i<1||i>GetLength ())
            {
                Console.WriteLine("Position is error!");
                return null;
            }
            else if (i<=GetLength()/2)
            {
                DoubleNode <T >temp = head;
                for (int j = 1; j <=i; ++j)
                {
                    temp = temp.Next;
                }           
                return temp;
            }
            else
            {
                DoubleNode<T> temp = tail;
                for (int j = GetLength()-i;j >=0; --j)
                {
                    temp = temp.Prev ;
                }
                return temp;
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卿屿­­­­­­­-轻尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值