數據結構和算法系列3 棧

上一篇總結完了線性表之鏈表,這一篇文章咱們要總結的是棧,我想從如下幾個方面來進行總結。數組

1,什麼是棧?
2,棧的存儲結構?
3,棧的常見操做及代碼實現?spa

1,什麼是棧

首先棧是一種特殊的線性表。那它的特殊性表如今哪裏呢?棧是限定在表的一端進行插入和刪除運算的線性表,所以,棧也稱爲後進先出(LIFO)的線性表。3d

它有不少應用場景,好比食堂中的一疊盤子,咱們只能從頂端一個一個地取。指針

2,棧的存儲結構

ds13

實現代碼:code

/// <summary>
    /// 封裝順序棧
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SeqStack<T>
    {
        //使用數組來存放結點
        public T[] data;

        //棧頂指針
        public int top = -1;

        public SeqStack(int length)
        { 
            data=new T[length];
        }
    }

3,棧的常見操做及實現代碼

1,初始化blog

實現思路:用指定大小的length實例化一個SeqStack<T>,而後使top指針指向-1。get

2,進棧string

實現思路:將top指針加1,而後將新結點插入到top指針指向的位置。it

3,出棧io

實現思路:消滅top指向的結點,並使top指針減1。

4,獲取棧頂元素

實現思路:與出棧類似,只是不改變棧的狀態而已。

5,判斷是否棧空

實現思路:若是top指針是否等於-1,若是是則返爲空棧。

6,判斷是否棧滿

實現思路:檢查top指針的值是否等於數組的長度,若是是則爲棧滿。

 

C#版代碼:

namespace DS.Model
{
    /// <summary>
    /// 學生實體
    /// </summary>
    public class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

namespace DS.BLL
{
    public class SeqStackBLL
    {
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static SeqStack<T> Init<T>(int length)
        {
            SeqStack<T> seqStack = new SeqStack<T>(length);
            seqStack.top = -1;
            return seqStack;
        }

        /// <summary>
        /// 進棧
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="seqStack"></param>
        /// <param name="data"></param>
        public static void Push<T>(SeqStack<T> seqStack, T data)
        { 
            //檢查是否棧滿
            if (IsFull(seqStack)) return;
            seqStack.data[++seqStack.top] = data;
        }

        /// <summary>
        /// 出棧
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="seqStack"></param>
        public static T Pop<T>(SeqStack<T> seqStack)
        { 
            //檢查是否棧空
            if (IsEmpty(seqStack)) return default(T);

            seqStack.data[seqStack.top]=default(T);
            return seqStack.data[--seqStack.top];
        }

        /// <summary>
        /// 獲取棧頂元素
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="seqStack"></param>
        /// <returns></returns>
        public static T GetTop<T>(SeqStack<T> seqStack)
        {
            //檢查是否棧空
            if (IsEmpty(seqStack)) return default(T);

            return seqStack.data[seqStack.top];
        }

        /// <summary>
        /// 獲取棧中元素個數
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="seqStack"></param>
        /// <returns></returns>
        public static int GetLength<T>(SeqStack<T> seqStack)
        {
            return seqStack.top + 1;
        }

        /// <summary>
        /// 判斷是否棧滿
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="seqStack"></param>
        /// <returns></returns>
        private static bool IsFull<T>(SeqStack<T> seqStack)
        {
            return seqStack.top == seqStack.data.Length;
        }

        /// <summary>
        /// 判斷是否棧空
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="seqStack"></param>
        /// <returns></returns>
        private static bool IsEmpty<T>(SeqStack<T> seqStack)
        {
            return seqStack.top == -1;
        }
    }

    /// <summary>
    /// 封裝順序棧
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SeqStack<T>
    {
        //使用數組來存放結點
        public T[] data;

        //棧頂指針
        public int top = -1;

        public SeqStack(int length)
        { 
            data=new T[length];
        }
    }
}

namespace SeqStack.CSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            SeqStack<Student> seqStack = null;
            Console.WriteLine("***********************初始化*****************************\n");
            seqStack = SeqStackBLL.Init<Student>(10);
            if (seqStack != null && seqStack.data.Length > 0) Console.WriteLine("初始化成功\n");
            else Console.WriteLine("初始化失敗\n");
            Console.WriteLine("當前棧中有{0}個元素", SeqStackBLL.GetLength(seqStack));

            Console.WriteLine("\n***********************進棧*****************************\n");
            Console.WriteLine("壓入3條數據\n");
            SeqStackBLL.Push<Student>(seqStack, new Student { ID = 1, Name = "a", Age = 10 });
            SeqStackBLL.Push<Student>(seqStack, new Student { ID = 2, Name = "b", Age = 11 });
            SeqStackBLL.Push<Student>(seqStack, new Student { ID = 3, Name = "c", Age = 12 });
            Display(seqStack);

            Console.WriteLine("\n***********************出棧*****************************\n");
            Console.WriteLine("彈出棧頂元素\n");
            Student student= SeqStackBLL.Pop<Student>(seqStack);
            Console.WriteLine("當前棧頂元素爲:ID={0},Name={1},Age={2}",student.ID,student.Name,student.Age);

            Console.WriteLine("\n***********************獲取棧頂元素*****************************\n");
            Student student1= SeqStackBLL.GetTop(seqStack);
            Console.WriteLine("當前棧頂元素爲:ID={0},Name={1},Age={2}", student1.ID, student1.Name, student1.Age);

            Console.WriteLine("\n***********************獲取棧中元素個數*****************************\n");
            Console.WriteLine("當前棧中有{0}個元素\n", SeqStackBLL.GetLength(seqStack));


            Console.ReadKey();
        }

        private static void Display(SeqStack<Student> seqStack)
        {
            Console.WriteLine("****展現數據****\n");
            for (int i = SeqStackBLL.GetLength(seqStack)-1; i>=0; i--)
            {
                Console.WriteLine("ID={0},Name={1},Age={2}",seqStack.data[i].ID,seqStack.data[i].Name,seqStack.data[i].Age);
            }
            Console.WriteLine("****展現完畢****\n");
        }
    }
}

程序運行結果:

ds15

 

C語言版:

#include "stdio.h"    
#include "stdlib.h"   
#include "io.h"  
#include "math.h"  
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 10

typedef int Status; 
typedef int ElemType;

/* 順序棧結構 */
typedef struct
{
    /*使用數組來存放結點*/
    ElemType data[MAXSIZE];

    /* 棧頂指針 */
    int top;
}SeqStack;


/*初始化*/
/************************************************************************/
/* 思路:使top指針指向-1                                                 */
/************************************************************************/
Status Init(SeqStack *seqStack)
{
    seqStack->top=-1;
    return OK;
}

/*進棧*/
/************************************************************************/
/*思路:top指針加1,而後將新結點從棧頂壓入                                 */
/************************************************************************/
Status Push(SeqStack *seqStack,ElemType e)
{
    //檢查是否棧滿
    if (IsFull(seqStack)) return ERROR;

    seqStack->data[++seqStack->top]=e;
}

/*出棧*/
/************************************************************************/
/*思路:消滅top指向的結點,並使top指針減1                                 */
/************************************************************************/
Status Pop(SeqStack *seqStack,ElemType *e)
{
    //檢查是否棧空
    if (IsEmpty(seqStack)) return ERROR;

    *e=seqStack->data[seqStack->top];
    seqStack->top--;
    return OK;
}

/*獲取棧頂元素*/
/************************************************************************/
/*思路:用e返回top指針指向的元素的值                                      */
/************************************************************************/
Status GetTop(SeqStack *seqStack,ElemType *e)
{
    //檢查是否棧空
    if (IsEmpty(seqStack)) return ERROR;
    
    *e=seqStack->data[seqStack->top];
}

/*獲取棧中元素個數*/
/************************************************************************/
/*思路:取棧的長度便可                                                   */
/************************************************************************/
int GetLength(SeqStack *seqStack)
{
    return seqStack->top+1;
}

/*判斷是否棧空*/
Status IsEmpty(SeqStack *seqStack)
{
    return seqStack->top==-1;
}

/*判斷是否棧滿*/
Status IsFull(SeqStack *seqStack)
{
    return seqStack->top==MAXSIZE-1;
}

/*展現棧中數據*/
void Display(SeqStack *seqStack)
{
    int i;
    printf("**********開始展現數據**********\n");

    for (i=seqStack->top;i>=0;i--)
    {
        printf("%d ",seqStack->data[i]);
    }
    printf("\n**********展現結束**********\n");
}


void main()
{
    SeqStack seqStack;
    int j;
    ElemType e;

    printf("********************初始化********************\n");
    if (Init(&seqStack)) printf("初始化成功!\n");
    else printf("初始化失敗!\n");

    printf("\n********************進棧********************\n");
    printf("依次插入10個元素\n");
    for (j=1;j<=10;j++)
    {
        Push(&seqStack,j);
    }
    Display(&seqStack);

    printf("\n********************出棧********************\n");
    printf("彈出棧頂元素\n");
    Pop(&seqStack,&e);
    printf("被彈出的棧頂元素是:%d\n",e);

    printf("\n****************獲取棧頂元素****************\n");
    GetTop(&seqStack,&e);
    printf("當前棧頂元素是:%d\n",e);

    printf("\n****************獲取棧中元素個數****************\n");
    printf("當前棧中共有%d個元素\n",GetLength(&seqStack));

    getchar();
}

程序運行結果:

ds14

相關文章
相關標籤/搜索