zoukankan      html  css  js  c++  java
  • 链堆栈的实现

    代码
     
        
    //链堆栈的接口定义如下所示。
        public interface IStack<T>
        {
            
    int GetLength(); //求栈的长度
            bool IsEmpty(); //判断栈是否为空
            void Clear(); //清空操作
            void Push(T item); //入栈操作
            T Pop(); //出栈操作
            T GetTop(); //取栈顶元素
        }
       
    //链堆栈方法的实现
       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;
            }
            
    //求链栈的长度
            public int GetLength()
            {
                
    return num;
            }
            
    //清空链栈
            public void Clear()
            {
                top 
    = null;
                num 
    = 0;
            }
            
    //判断链栈是否为空
            public bool IsEmpty()
            {
                
    if ((top == null&& (num == 0))
                {
                    
    return true;
                }
                
    else
                {
                    
    return false;
                }
            }
            
    //入栈
            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()
            {
                
    if (IsEmpty())
                {
                    Console.WriteLine(
    "Stack is empty!");
                    
    return default(T);
                }
                Node
    <T> p = top;
                top 
    = top.Next;
                
    --num;
                
    return p.Data;
            }
            
    //获取栈顶结点的值
            public T GetTop()
            {
                
    if (IsEmpty())
                {
                    Console.WriteLine(
    "Stack is empty!");
                    
    return default(T);
                }
                
    return top.Data;
            }
        }
  • 相关阅读:
    LeetCode 40. 组合总和 II(Combination Sum II)
    LeetCode 129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)
    LeetCode 60. 第k个排列(Permutation Sequence)
    LeetCode 47. 全排列 II(Permutations II)
    LeetCode 46. 全排列(Permutations)
    LeetCode 93. 复原IP地址(Restore IP Addresses)
    LeetCode 98. 验证二叉搜索树(Validate Binary Search Tree)
    LeetCode 59. 螺旋矩阵 II(Spiral Matrix II)
    一重指针和二重指针
    指针的意义
  • 原文地址:https://www.cnblogs.com/hubcarl/p/1706371.html
Copyright © 2011-2022 走看看