上一篇总结完了线性表之链表,这一篇文章我们要总结的是栈,我想从以下几个方面来进行总结。

1,什么是栈?
2,栈的存储结构?
3,栈的常见操作及代码实现?

1,什么是栈

首先栈是一种特殊的线性表。那它的特殊性表现在哪里呢?栈是限定在表的一端进行插入和删除运算的线性表,因此,栈也称为后进先出(LIFO)的线性表。

它有很多应用场景,比如食堂中的一叠盘子,我们只能从顶端一个一个地取。

2,栈的存储结构

ds13

实现代码:

/// <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,初始化

实现思路:用指定大小的length实例化一个SeqStack<T>,然后使top指针指向-1。

2,进栈

实现思路:将top指针加1,然后将新结点插入到top指针指向的位置。

3,出栈

实现思路:消灭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

posted on 2013-07-24 22:18  永远的麦子  阅读(1537)  评论(0编辑  收藏  举报