数据结构-----栈

  interface IStack<T>
    {
        int GetLength(); //获取长度
        bool IsEmpty();//是否为空
        void Clear();//清空
        void Push(T item);//添加
        T Pop();//删除头数据
        T GetTop();//取出头数据
    }
 public  class Node<T>
    {
        private T data;
        private Node<T> next;
        public T Data
        {
            get { return data; }
            set { data = value; }
        }
        public Node<T> Next
        {
            get { return next; }
            set { next = value; }
        }
        public Node(T val,Node<T>p )
            {
            data = val;
            next = p;
            }
        public Node(T val)
        {
            data = val;
         //   next = null ;
        }
        public Node( Node<T> p)
        {
           // data = default(T);
            next = p;
        }
        public Node()
        {
            data = default (T );
            next = null;
        }
    }

//线栈

public   class SeqStack<T> : IStack<T>
    {
        private int matsize;
        private T[] data;
        private int top;

        public int Matsize { get => matsize; }
        public T[] Data { get => data; set => data = value; }
        public int Top { get => top;  }
      public SeqStack(int size)
        {
            matsize = size;
            data = new T[matsize];
            top = -1;
        }
        public T this[int i]
        {

            get
            {
                if (i <= top && i > -1)
                {
                    return data[i];
                }
                else
                {
                    Console.WriteLine("数据不存在");
                    return default(T);
                }
            }
        }
        public bool IsFull()
        {
            return top==matsize-1;
           
        }
        public void Clear()
        {
            top = -1;
            Array.Clear(data, 0, matsize);
        }

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

        public T GetTop()
        {
            if (IsEmpty())
            {
                Console.WriteLine("Stack is empty!");
                return default(T);
            }
            return data[top];
        }

        public bool IsEmpty()
        {
            return top == -1;
        }

        public T Pop()
        {
            T temp = default(T);
            if (IsEmpty ())
            {
                Console.WriteLine("Stack is full!");
                return temp ;
            }
            temp = data[top];
            --top;
            return temp;
        }

        public void Push(T item)
        {
            if (IsFull ())
            {
                Console.WriteLine("Stack is full!");return;
            }
            data[++top]=item;
        }
    }

//链栈

 public  class LinkStack<T> : IStack<T>
    {
       private  Node<T> top;
        private int num;
        public int Num { get => num;  }
        internal Node<T> Top { get => top; set => top = value; }
        public LinkStack()
        {
            top = null;
            num = 0;
        }
        public T this[int i]
        {
            get
            {
                if (i < 0 || i > num)
                 return default(T);                    
                Node<T> p = top;
                for (int j = 0; j <num -i; ++j)
                {
                    p = p.Next;
                }
                return p.Data;
            }
        }
        public void Clear()
        {
            top = null;
            num = 0;
        }

        public int GetLength()
        {
            return num;
        }

        public T GetTop()
        {
            if (IsEmpty())
            {
                Console.WriteLine("LinkStack is empty!");
                return default(T);
            }
            return top.Data;
        }

        public bool IsEmpty()
        {
            return num == 0 && top == null ? true:false ;
        }

        public T Pop()
        {
            if (IsEmpty ())
            {
                Console.WriteLine("LinkStack is empty!");
                return default(T);
            }
            Node<T> p = top;
            top = top.Next;
            --num;
           return p.Data;           
        }

        public void Push(T item)
        {
            Node<T> q = new Node<T>(item);
            if (top ==null)
            {
                top  = q;
            }
            else
            {
                q .Next=top;
                top = q;
            }
            ++num;
        }
    }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

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

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

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

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

打赏作者

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

抵扣说明:

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

余额充值