今天学习c#当中实现栈,学过C#的都知道,c#本身已经写好 了栈和队列,我们可以直接用,这里自己实现以下,就是为了更深刻的理解。

首先说明线性表,栈、队列他们的数据元素以及数据元素之间的逻辑关系实际上都是相同的,不同的是线性表的操作不受限制,而栈和队列则受限制,栈的操作只能在一端进行,队列的扎入在一端进行,别的操作在另一端进行。

我们通常把表尾看做是栈顶,另一端是固定的叫栈底,栈中没有数据时我们称为空栈。

c#栈的实现

实现栈的代码

 public class SeqStack<T>
    {
        private int maxSize;// 顺序栈的容量

        public int MaxSize
        {
            get { return maxSize; }
            set { maxSize = value; }
        }
        public T[] data; // 数组,用于存储顺序栈中的数据元素
        private int top; // 指示顺序栈的栈顶

        public int Top
        {
            get { return top; }
        }
        //构造器
        public SeqStack(int size)
        {
            data = new T[size];
            maxSize = size;
            top = -1;
        } 
        // 定义索引器
        public T this[int index]
        {
            get { return data[index]; }
            set { data[index] = value; }
        }
        // 由于数组是 0 基数组,即数组的最小索引为 0,所以,顺序栈的长度就是数组中最后一个元素的索引 top 加 1
        public int GetLength()
        {
            return top + 1;
        }

        public bool IsEmpty()
        {
            if ( -1 == top )
            {
                return true;
            }
            return false;
        }
        public bool IsFull()
        {
            if ( top == maxSize - 1 )
            {
                return true;
            }
            return false;
        }
        public void Push( T item)
        {
            if ( IsFull() )
            {
                Console.WriteLine("栈已满,无法压栈");
                return;
            }
            data[++top] = item;
        }

        public T Pop()
        {
            if ( IsEmpty() )
            {
                Console.WriteLine("栈为空,无法弹栈");
                return default(T);
            }
            return data[top--];
        }

        public T Peek()
        {
            if ( IsEmpty() )
            {
                Console.WriteLine("栈为空,无法弹栈");
                return default(T);
            }
            return data[top];
        }
    }
    // 链表实现栈
    public class LinkStack<T>
    {
        private Node<T> top; // 栈顶指示器

        internal Node<T> Top 
        {
            get { return top; }
            set { top = value; }
        }
        private int nCount; //栈中结点的个数

        public int NCount
        {
            get { return nCount; }
            set { nCount = value; }
        }

        public LinkStack()
        {
            top = null;
            nCount = 0;
        }

        public int GetLength()
        {
            return nCount;
        }
        public bool IsEmpty()
        {
            if ( (top == null) && (0 == nCount))
            {
                return true;
            }
            return false;
        }
        public void Push(T item)
        {
            Node<T> p = new Node<T>(item);
            if ( top == null )
            {
                top = p;
            }
            else
            {
                p.Next = top;
                top = p;
            }
            nCount++;
        }

        public T Pop()
        {
            if ( IsEmpty() )
            {
                return default(T);
            }
            Node<T> p = top;
            top = top.Next;
            --nCount;
            return p.Data;
            
        }

        public T Peek()
        {
            if ( IsEmpty() )
            {
                return default(T);
            }
            return top.Data;
        }
    }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-29
  • 2022-01-12
  • 2022-01-10
  • 2021-09-20
猜你喜欢
  • 2021-05-15
  • 2022-12-23
  • 2022-12-23
  • 2021-10-14
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案