数据结构-栈&链栈

 

 

栈接口实现:

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("");
        }

 

posted @ 2017-12-23 13:00  RONGWEIJUN  阅读(1050)  评论(0编辑  收藏  举报