数据结构和算法基础之栈的链式存储

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

namespace ConsoleApplication1
{
    public class StackNode<T>
    {
        public T Data;
        public StackNode<T> Next;

        public StackNode()
        {
            Data = default(T);
            Next = null;
        }

        public StackNode(T datat)
        {
            Data = datat;
            Next = null;
        }
    }
    /// <summary>
    /// 栈的链式存贮
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class LinkStack<T>
    {
        /// <summary>
        /// 栈顶的节点
        /// </summary>
        public StackNode<T> Top;

        public int Count;

        public LinkStack()
        {
            Top = null;
            Count = 0;
        }

        /// <summary>
        /// 压栈
        /// </summary>
        public void Push(T data)
        {
            StackNode<T> newNode = new StackNode<T>(data);
            Count++;
            if (Top==null)
            {
                Top = newNode;
                return;
            }
            newNode.Next = Top;
            //Top节点重新赋值
            Top = newNode;
        }

        /// <summary>
        /// 弹栈
        /// </summary>
        /// <returns></returns>
        public T Pop()
        {
            if (Count == 0) return default(T);

            T data= Top.Data;
            Top = Top.Next;
            Count--;
            return data;
        }

    }
}

 

上一篇:gfortran + msmpi 在windows上编译并行程序


下一篇:链式栈