zoukankan      html  css  js  c++  java
  • 数据结构-栈&链栈

    栈接口实现:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _002_栈
    {
        /// <summary>
        /// 栈接口
        /// </summary>
        /// <typeparam name="T"></typeparam>
        interface IStackDS<T>
        {
            int Count { get; }
            int GetLength();
            bool isEmpty();
            void Clear();
            void Push(T item);
            T Pop();
            T Peek();
        }
    }

    顺序栈实现:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _002_栈
    {
        /// <summary>
        /// 顺序栈
        /// </summary>
        /// <typeparam name="T"></typeparam>
        class SeqStack<T> : IStackDS<T>
        {
            private T[] data; //数组
            private int top; //栈顶
    
            public SeqStack(int size)
            {
                data = new T[size];
                top = -1;
            }
            public SeqStack():this(10)
            {
    
            }
    
            public int Count //栈内数量
            {
              get
                {
                    return top + 1;
                }           
            }
    
            /// <summary>
            /// 清空
            /// </summary>
            public void Clear()
            {
                top = -1;
            }
    
            /// <summary>
            /// 获得长度
            /// </summary>
            /// <returns></returns>
            public int GetLength()
            {
                return Count;
            }
    
            /// <summary>
            /// 是否为空
            /// </summary>
            /// <returns></returns>
            public bool isEmpty()
            {
                return Count == 0;
            }
    
            /// <summary> 
            /// 取得栈顶数据,但不删除
            /// </summary>
            /// <returns></returns>
            public T Peek()
            {
                if (isEmpty())
                {
                    Console.WriteLine("栈为空,无法弹栈");
                    return default(T);
                }
                return data[top];
            }
            /// <summary>
            /// 出栈,并且删除数据
            /// </summary>
            /// <returns></returns>
            public T Pop()
            {
                if (isEmpty())
                {
                    Console.WriteLine("栈为空,无法弹栈");
                    return default(T);
                }
                else
                {
                    T temp = data[top];
                    top--;
                    return temp;
                }
            }
            /// <summary>
            /// 入栈
            /// </summary>
            /// <param name="item"></param>
            public void Push(T item)
            {
                if (IsFull())
                {
                    Console.WriteLine("栈已满,无法入栈");
                }
                else
                {
                    data[top + 1] = item;
                    top++;
                }
     
            }
            public bool IsFull()
            {
                if (top == data.Length)
                { 
                    return true;
                }
                return false;
            }
        }
    }

    链栈

     链节点:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _002_栈
    {
        /// <summary>
        /// 链栈节点
        /// </summary>
        /// <typeparam name="T"></typeparam>
        class Node<T>
        {
            private T data;
            private Node<T> next;
    
            public Node()
            {
                data = default(T);
                next = null;
            }
            public  Node(T data)
            {
                this.data = data;
                next = null;
            }
            public Node(Node<T> next)
            {
                this.next = next;
                data = default(T);
            }
            public Node(T data,Node<T> next)
            {
                this.data = data;
                this.next = next;
            } 
            public T Data
            {
                get
                {
                    return data;
                }
                set
                {
                    data = value;
                }
            }
            public Node<T> Next
            {
                get
                {
                    return next;
                }
                set
                {
                    next = value;
                }
            }
        }
    }

    链栈实现:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _002_栈
    {
        /// <summary>
        /// 链栈
        /// </summary>
        /// <typeparam name="T"></typeparam>
        class LinkStack<T> : IStackDS<T>
        {
            private Node<T> top;//栈顶元素节点
            private int count = 0;//栈中元素的个数
    
    
            public LinkStack()
            {
                top = null;
                count = 0;
            }
    
            /// <summary>
            /// 取得栈中元素的个数
            /// </summary>
            public int Count {  
             get {
                    return count;
                 }
            }
            /// <summary>
            /// 清空栈中所有数据
            /// </summary>
            public void Clear()
            {
                count = 0;
                top = null;
            }
    
            /// <summary>
            /// 取得栈中元素的个数
            /// </summary>
            public int GetLength()
            {
                return count;
            }
    
            /// <summary>
            /// 是否为空栈
            /// </summary>
            /// <returns></returns>
            public bool isEmpty()
            {
                return count == 0;
            }
    
            /// <summary>
            /// 取得栈顶元素
            /// </summary>
            /// <returns></returns>
            public T Peek()
            {
                if (isEmpty())
                {
                    return default(T);
                }
                return top.Data;
            }
            /// <summary>
            /// 出栈 取得栈顶元素,然后删除
            /// </summary>
            /// <returns></returns>
            public T Pop()
            {
                if (isEmpty())
                {
                    return default(T);
                }
                else
                {
                    //先进后出原则,直接把top弄出去
                    T data = top.Data;
                    top = top.Next;
                    count--;
                    return data;
                }
            }
    
            /// <summary>
            /// 入栈
            /// </summary>
            /// <param name="item"></param>
            public void Push(T item)
            {
                //把新添加的元素作为栈顶元素结点(栈顶)
                Node<T> newNode = new Node<T>(item);
                if (top == null)
                {
                    top = newNode;
                }
                else
                {
                    newNode.Next = top; 
                    top = newNode;
                }
                count++;
            }
        }
    }

     栈应用

    /// <summary>
            /// 十进制转八进制
            /// </summary>
            /// <param name="n"></param>
            public static void TenConversionEight(int n)
            {
                LinkStack<int> stack = new LinkStack<int>();
                while (n>0)
                {
                    stack.Push(n % 8);
                     n = n/8;
                }
                while (!stack.isEmpty())
                {
                    Console.Write("{0}", stack.Pop());
                }
                Console.WriteLine("");
            }
  • 相关阅读:
    消息中间件(MQ)
    java Lambda表达式
    【测试】性能测试及性能测试工具JMeter
    【Mysql】mysql集群方案之读写分离
    linux下mysql开启远程访问权限及防火墙开放3306端口
    MySQL事务提交与回滚
    MySQL索引
    MySQL视图
    MySQL事务
    MySQL参数化有效防止SQL注入
  • 原文地址:https://www.cnblogs.com/rongweijun/p/8093161.html
Copyright © 2011-2022 走看看