c#:线性表,链表,栈,队列

有关线性列表、栈、队列的记录。
来源:30.数据结构(C#语言版).pdf

1.线性表

    /// <summary>
    /// 线性列表接口
    /// 基本操作:在线性列表的任务位置插入和删除元素
    /// 获取元素的值、读出这个值或者修改这个值
    /// 生成和清除(或者重新初始化)线性表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IListDS<T>
    {
        int GetLength();            //求长度
        void Clear();               //清空操作
        bool IsEmpty();             //判断线性表是否为空
        void Append(T item);        //附加操作
        void Insert(T item, int i); //插入操作
        T Delete(int i);            //删除操作
        T GetElem(int i);           //取表元
        int Locate(T value);        //按值查找
    }
    /// <summary>
    /// 顺序表SequenceList
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SeqList<T> : IListDS<T>
    {
        private int maxsize; //顺序表的容量
        private T[] data;    //数组,用于存储顺序表中的数据元素
        private int last;    //指示顺序表最后一个元素的位置,如果顺序表为空则last=-1
        /// <summary>
        /// 索引器 
        /// 索引器的定义
        ///C#中的类成员可以是任意类型,包括数组和集合。当一个类包含了数组和集合成员时,
        ///索引器将大大简化对数组或集合成员的存取操作。
        ///定义索引器的方式与定义属性有些类似,其一般形式如下:
        ///[修饰符] 数据类型 this[索引类型 index]
        ///{
        ///    get{//获得属性的代码}                                                 
        ///    set{ //设置属性的代码}
        ///}
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public T this[int index]
        {
            get { return data[index]; }
            set { data[index] = value; }
        }
        //顺序表最后一个元素的位置,如果顺序表为空则last=-1
        public int Last
        {
            get { return last; }
        }
        //容量属性
        public int Maxsize
        {
            get { return maxsize; }
            set { maxsize = value; }
        }
        //构造器
        public SeqList(int size)
        {
            data = new T[size];
            maxsize = size;
            last = -1;
        }
        #region IListDS<T> 成员
        /// <summary>
        /// 求顺序表的长度
        /// </summary>
        /// <returns></returns>
        public int GetLength()
        {
            return last + 1;
        }
        /// <summary>
        /// 清空顺序表
        /// </summary>
        public void Clear()
        {
            last = -1;
        }
        /// <summary>
        /// 判断顺序表是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
            if (last == -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 在顺序表的末尾添加新元素
        /// </summary>
        /// <param name="item"></param>
        public void Append(T item)
        {
            if (IsFull())
            {
                Console.WriteLine("List is full");
                return;
            }
            data[++last] = item;
        }
        /// <summary>
        /// 在顺序表的第i个数据元素的位置插入一个数据元素
        /// 注意:位置变量i的初始值为1不是0
        /// </summary>
        /// <param name="item"></param>
        /// <param name="i"> 1<=i<=n+1 </param>
        public void Insert(T item, int i)
        {
            if (IsFull())
            {
                Console.WriteLine("List is full");
                return;
            }
            //判断插入的位置是否正确
            //i小于1表示在第一个位置之前插入
            //i大于last+2表示在最后一个元素后面的第2个位置插入
            // 1<=i<=n+1 为合法位置
            if (i < 1 || i > last + 2)
            {
                Console.WriteLine("Position is error");
                return;
            }
            if (i == last + 2) //在最后一个位置插入
            {
                //data[last + 1] = item;
                data[i - 1] = item;
            }
            else
            {
                for (int j = last; j >= i - 1; --j)//后移an...ai 
                {
                    data[j + 1] = data[j];
                }
                data[i - 1] = item;//插入
            }
            ++last;//修改表长,长度加1
        }
        /// <summary>
        /// 删除顺序表的第i个数据元素
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public T Delete(int i)
        {
            T temp = default(T);
            if (IsEmpty())
            {
                Console.WriteLine("List is empty");
                return temp;
            }
            //判断删除的位置是否正确
            //i小于1表示删除第一个位置之前的元素
            //i大于last+1表示删除最后一个元素后面的第1个位置的元素
            //取值范围:1<=i<=n 
            if (i < 1 || i > last + 1)
            {
                Console.WriteLine("Position is error");
                return temp;
            }
            if (i == last + 1)//删除最后一个元素
            {
                temp = data[last--]; return temp;
            }
            else
            {
                temp = data[i - 1];
                for (int j = i; j <= last; ++j)//An..Ai+1前移
                {
                    data[j] = data[j + 1];
                }
            }
            --last;
            return temp;
        }
        /// <summary>
        ///获取顺序表的第i个数据元素
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public T GetElem(int i)
        {
            if (IsEmpty() || (i < 1) || (i > last + 1))
            {
                Console.WriteLine("List is empty or Position is error");
                return default(T);
            }
            return data[i - 1];
        }
        /// <summary>
        /// 在顺序表中查找值为value的数据元素
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public int Locate(T value)
        {
            if (IsEmpty())
            {
                Console.WriteLine("List is empty");
                return -1;
            }
            int i = 0;
            for (; i <= last; ++i)
            {
                if (value.Equals(data[i]))
                {
                    break;
                }
            }
            if (i > last)//顺序表中部存在与给定值相等的元素
            {
                return -1;
            }
            return i;
        }
        #endregion
        /// <summary>
        /// 由于顺序表空间的限制,需要判断顺序表是否为满
        /// </summary>
        /// <returns></returns>
        public bool IsFull()
        {
            if (last == maxsize - 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 将顺序表L倒置,顺序表有随机存储性质
        /// 算法思路:
        /// 把第一个元素与最后一个元素交换,把第二个元素与倒数第二个元素交换。
        /// 一般地,把第i个元素与第n-i个元素交换,i的取值范围是[0..n/2],n为顺序变的长度
        /// </summary>
        /// <param name="L"></param>
        public void ReversSeqList(SeqList<int> L)
        {
            int temp;
            int len = L.GetLength();
            for (int i = 0; i <= len / 2; ++i)
            {
                temp = L[i];
                L[i] = L[len - i];
                L[len - i] = temp;
            }
        }
        /// <summary>
        /// 将有序顺序表La和Lc合并成有序顺序表Lc
        /// </summary>
        /// <param name="La"></param>
        /// <param name="Lb"></param>
        /// <returns></returns>
        public SeqList<int> Merge(SeqList<int> La, SeqList<int> Lb)
        {
            SeqList<int> Lc = new SeqList<int>(La.maxsize + Lb.maxsize);
           
            int i = 0, j = 0;
           
            //2个表中都有数据元素
            while (i<=La.GetLength()-1 && j<=Lb.GetLength()-1)
            {
                if (La[i] < Lb[j])
                {
                    Lc.Append(La[i++]);
                }
                else
                {
                    Lc.Append(Lb[j++]);
                }
            }
            //a表中还有数据元素
            while (i <= La.GetLength() - 1)
            {
                Lc.Append(La[i++]);
            }
            //b表中还有数据元素
            while (j <= Lb.GetLength() - 1)
            {
                Lc.Append(Lb[j++]);
            }
            return Lc;
        }
    }

2.单链表

    /// <summary>
    /// 单链表节点
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Node<T>
    {
        private T data;        //数据域
        private Node<T> next;  //引用域 
        public Node(T val, Node<T> p)
        {
            data = val;
            next = p;
        }
        public Node(T val)
        {
            data = val;
        }
        public Node(Node<T> p)
        {
            next = p;
        }
        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; }
        }
    }
    /// <summary>
    /// 单链表类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class LinkList<T> : IListDS<T>
    {
        private Node<T> head;//单链表的头指针
        public Node<T> Head
        {
            get { return head; }
            set { head = value; }
        }
        public LinkList()
        {
            head = null;
        }
        #region IListDS<T> 成员
        public int GetLength()
        {
            Node<T> p = head;
            int len = 0;
            while (p != null)
            {
                ++len;
                p = p.Next;
            }
            return len;
        }
        public void Clear()
        {
            head = null;
        }
        public bool IsEmpty()
        {
            if (head == null) return true;
            else return false; 
        }
        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;
        }
        //第i个位置节点处插入一个值为item的新节点
        //前插操作
        public void Insert(T item, int i)
        {
            if (IsEmpty() || i < 1)
            {
                Console.WriteLine("List is empty or Position is error !");
                return;
            }
            if (i == 1)
            {
                Node<T> q = new Node<T>(item);
                q.Next = head;
                head = q;
                return;
            }
            //查找到第i个位置
            Node<T> p = head;
            Node<T> r = new Node<T>();//记录第i个位置前的节点
            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;
            }
            else
            {
                Console.WriteLine("Position is error !");
            }
            return;
        }
        //第i个位置节点处插入一个值为item的新节点
        //后插操作
        public void InsertPost(T item, int i)
        {
            if (IsEmpty() || i < 1)
            {
                Console.WriteLine("List is empty or Position is error !");
                return;
            }
            if (i == 1)
            {
                Node<T> q = new Node<T>(item);
                q.Next = head.Next ;
                head.Next = q;
                return;
            }
            //查找到第i个位置
            Node<T> p = head;
            int j = 1;
            while (p.Next != null && j < i)
            {
                p = p.Next;
                ++j;
            }
            if (j == i)
            {
                Node<T> q = new Node<T>(item);
                q.Next = p.Next ;
                p.Next = q;
            }
            else
            {
                Console.WriteLine("Position is error !");
            }
            return;
        }
        public T Delete(int i)
        {
            if (IsEmpty() || i < 0)
            {
                Console.WriteLine("List is empty or Position is error !");
                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)
            {
                q = p;//记录删除节点
                p = p.Next;
                ++j;
            }
            if (j == i)
            {
                q.Next = p.Next;
                return p.Data;
            }
            else
            {
                Console.WriteLine("The " + i + "th node is not exist !");
                return default(T);
            }
        }
        public T GetElem(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)
            {
                p = p.Next;
                ++j;
            }
            if (j == i)
            {
                return p.Data;
            }
            else
            {
                Console.WriteLine("The " + i + "th node is not exist !");
                return default(T);
            }
        }
        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;
            }
            return i;
        }
        #endregion
    }

4.栈

    #region 栈操作
    /// <summary>
    /// 栈操作接口
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IStack<T>
    {
        int GetLength();
        bool IsEmpty();
        void Clear();
        void Push(T item);//入栈
        T Pop();          //出栈
        T GetTop();       //取栈顶
    }
    /// <summary>
    /// 顺序栈
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SeqStack<T> : IStack<T>
    {
        private int maxsize;
        private T[] data;
        private int top;
        public T this[int index]
        {
            get { return data[index]; }
            set { data[index] = value; }
                
        }
        public int Maxsize
        {
            get { return maxsize; }
            set { maxsize = value; }
        }
        public int Top
        {
            get { return top; }
        }
        public SeqStack(int size)
        {
            data = new T[size];
            maxsize = size;
            top = -1;
        }
        #region IStack<T> 成员
        public int GetLength()
        {
            return top + 1;
        }
        public bool IsEmpty()
        {
            if (top == -1) return true;
            else return false;
        }
        public bool IsFull()
        {
            if (top == maxsize -1) return true;
            else return false;
        }
        public void Clear()
        {
            top = -1;
        }
        public void Push(T item)
        {
            if (IsFull())
            {
                Console.WriteLine("Stack is full!");
                return;
            }
            data[++top] = item;
        }
        public T Pop()
        {
            T temp = default(T); 
            if (IsEmpty())
            {
                Console.WriteLine("Stack is empty!");
                return temp;
            }
            temp = data[top];
            --top;
            return temp;
        }
        public T GetTop()
        {
            if (IsEmpty())
            {
                Console.WriteLine("Stack is empty!");
                return default(T);
            }
            return data[top];
        }
        #endregion
    }
    /// <summary>
    /// 链栈
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class LinkStack<T> : IStack<T>
    {
        private Node<T> top; //栈顶指示器
        private int num;     //栈中节点个数
        
        public Node<T> Top
        {
            get { return top; }
            set { top = value; }
        }
        public int Num
        {
            get { return num; }
            set { num = value; }
        }
        public LinkStack()
        {
            top = null;
            num = 0;
        }
        #region IStack<T> 成员
        public int GetLength()
        {
            return num;
        }
        public bool IsEmpty()
        {
            if (top == null && num == 0) return true;
            else return false;
        }
        public void Clear()
        {
            top = null;
            num = 0;
        }
        public void Push(T item)
        {
            Node<T> q = new Node<T>(item);
            if (top == null)
            {
                top = q;
            }
            else
            {
                q.Next = top;
                top = q;
            }
            ++num;
        }
        public T Pop()
        {
            T temp = default(T);
            if (IsEmpty())
            {
                Console.WriteLine("Stack is empty!");
                return temp;
            }
            Node<T> p = top;
            top = top.Next;
            --num;
            return p.Data;
        }
        public T GetTop()
        {
            T temp = default(T);
            if (IsEmpty())
            {
                Console.WriteLine("Stack is empty!");
                return temp;
            }
            return top.Data;
        }
        #endregion
    }
    #endregion

5.队列

    /// <summary>
    /// 队列操作接口
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IQueue<T>
    {
        int GetLength();
        bool IsEmpty();
        void Clear();
        void In(T item);//入队
        T Out();        //出队
        T GetFront();   //取队斗元素
    }
    /// <summary>
    /// 循环顺序队列
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class CSeqQueue<T> : IQueue<T>
    {
        private int maxsize;//容量
        private T[] data;   //数组,存储队列元素
        private int front;  //队头
        private int rear;   //队尾
        public int Maxsize
        {
            get { return maxsize; }
            set { maxsize = value; }
        }
        public int Front
        {
            get { return front; }
            set { front = value; }
        }
        public int Rear
        {
            get { return rear; }
            set { rear = value; }
        }
        public T this[int index]
        {
            get { return data[index]; }
            set { data[index] = value; } 
        }
        public CSeqQueue(int size)
        {
            data = new T[size];
            maxsize = size;
            front = rear = -1;
        }
        #region IQueue<T> 成员
        public int GetLength()
        {
            return (rear - front + maxsize) % maxsize;
        }
        public bool IsEmpty()
        {
            if (front == rear) return true;
            else return false;
        }
        //队满和队空时都有rear=front
        //解决方法是少用一个空间
        public bool IsFull()
        {
            if ((rear + 1) % maxsize == front) return true;
            else return false;
        }
        public void Clear()
        {
            front = rear = -1;
        }
        public void In(T item)
        {
            if (IsFull())
            {
                Console.WriteLine("Queue is full!");
                return;
            }
            data[++rear] = item;
        }
        public T Out()
        {
            T temp = default(T);
            if (IsEmpty())
            {
                Console.WriteLine("Queue is empty!");
                return temp ;
            }
            temp = data[ ++front];
            return temp;
        }
        public T GetFront()
        {
            T temp = default(T);
            if (IsEmpty())
            {
                Console.WriteLine("Queue is empty!");
                return temp;
            }
            temp = data[front+1];
            return temp;
        }
        #endregion
    }
    /// <summary>
    /// 链队列
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class LinkQueue<T> : IQueue<T>
    {
        private Node<T> front;  //队头
        private Node<T> rear;   //队尾
        private int num;//队列节点个数
        public int Num
        {
            get { return num; }
            set { num = value; }
        }
        public Node<T> Front
        {
            get { return front; }
            set { front = value; }
        }
        public Node<T> Rear
        {
            get { return rear; }
            set { rear = value; }
        }
        public LinkQueue()
        {
            front = rear = null;
            num = 0;
        }
        #region IQueue<T> 成员
        public int GetLength()
        {
            return num ;
        }
        public bool IsEmpty()
        {
            if (front == rear && num==0) return true;
            else return false;
        }
        public void Clear()
        {
            front = rear = null;
        }
        public void In(T item)
        {
            Node<T> q = new Node<T>(item);
            if (rear == null)
            {
                rear = q;
            }
            else
            {
                rear.Next = q;
                rear = q;
            }
            ++num;
        }
        public T Out()
        {
            if (IsEmpty())
            {
                Console.WriteLine("Queue is empty!");
                return default(T);
            }
            Node<T> p = front;//先取出链队列头节点
            front = front.Next;//指向链队列头节点的直接后继节点
            if (front == null)
            {
                rear = null;
            }
            --num;
           return p.Data;
        }
        public T GetFront()
        {
            if (IsEmpty())
            {
                Console.WriteLine("Queue is empty!");
                return default(T);
            }
            return front.Data;
        }
        #endregion
    }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值