C# data structure-chain stack

In the previous article, we implemented the stack structure (to be precise, the sequential storage structure of the stack) through the array structure, and now we use the chain (single chain) to store the stack, which is the chain stack.
Insert picture description here
Normally for a forward singly linked list, it starts from the head node and appends the node at the end of the chain, and the pointer of the previous node points to the additional node; for the implementation of the stack structure, inserts the node at the top of the stack (end of the chain), and the pointer points upward A node, so the chain that implements the stack structure can be said to be a reverse singly linked list.

public class LinkStackNode<T>
{
    
    
    /// <summary>
    /// 数据存储
    /// </summary>
    public T data {
    
     get; set; }
    /// <summary>
    /// 节点指针
    /// </summary>
    public LinkStackNode<T> next {
    
     get; set; }
    public LinkStackNode(T d)
    {
    
    
        data = d;
    }
}
	/// <summary>
    /// 链栈
    /// </summary>
    public class LinkStack<T>
    {
    
    
        //对于栈来说,不需要又链的头节点,所以此处我们忽略
        /// <summary>
        /// 栈顶
        /// </summary>
        public LinkStackNode<T> top {
    
     get; set; }
        /// <summary>
        /// 总长度
        /// </summary>
        public int count {
    
     get; set; }
        /// <summary>
        /// 入栈
        /// </summary>
        /// <param name="data"></param>
        public void push(T data)
        {
    
    
            LinkStackNode<T> linkedListNode = new LinkStackNode<T>(data);
            linkedListNode.next = top;
            top = linkedListNode;
            count++;
        }
        /// <summary>
        /// 取栈顶节点,但不删除该节点
        /// </summary>
        /// <returns></returns>
        public LinkStackNode<T> Peek()
        {
    
    
            if(count == 0)
                throw new InvalidOperationException("空栈");
            return top;
        }
        /// <summary>
        /// 出栈
        /// </summary>
        /// <returns></returns>
        public LinkStackNode<T> Pop()
        {
    
    
            if (count == 0)
                throw new InvalidOperationException("空栈");
            LinkStackNode<T> tp = top;
            top = top.next;
            count--;
            return tp;
        }
        /// <summary>
        /// 打印链栈所有元素
        /// </summary>
        public void showAll()
        {
    
    
            var node = top;
            for (int i = 0; i < count; i++)
            {
    
    
                Console.WriteLine($"{i},data:{node.data}");
                node = node.next;
            }
        }
    }
			LinkStack<string> link = new LinkStack<string>();
            link.push("第一");
            link.push("第二");
            link.push("第三");
            link.showAll();
            Console.WriteLine();
            Console.WriteLine($"Peek:{link.Peek().data}");
            Console.WriteLine();
            link.push("第四");
            link.push("第五");
            Console.WriteLine();
            link.showAll();
            link.Pop();
            Console.WriteLine();
            link.showAll();

Insert picture description here


Insert picture description here

Guess you like

Origin blog.csdn.net/xuetian0546/article/details/109100283