数据结构(六)栈的顺序存储结构

  一、栈的定义(类似弹夹中的子弹,先进后出,后进先出)

  1.栈(stack)是限定仅在表尾进行插入和删除操作的线性表。

  2.把允许插入和删除的一端称为栈顶(top),另一端称为栈底(bottom),不含任何元素的栈称为空栈。栈又称为后进先出(Last In First Out)的线性表,简称LIFO结构。

  3.栈的插入操作,叫做进栈(push),也称压栈、入栈;栈的删除操作,叫做出栈(pop),也称弹栈。

  4.栈的顺序存储结构:既然栈是线性表的特例,那么栈的顺序存储其实也是线性表顺序存储的简化。数组下标为0的一端作为栈底,定义一个top变量来指示栈顶元素在数组中的位置。

  5.栈的顺序存储结构的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 20 /* 存储空间初始分配量 */

typedef int Status; 
typedef int SElemType; /* SElemType类型根据实际情况而定,这里假设为int */

/* 顺序栈结构 */
typedef struct
{
        SElemType data[MAXSIZE];
        int top; /* 用于栈顶指针 */
}SqStack;

Status visit(SElemType c)
{
        printf("%d ",c);
        return OK;
}

/*  构造一个空栈S */
Status InitStack(SqStack *S)
{ 
        /* S.data=(SElemType *)malloc(MAXSIZE*sizeof(SElemType)); */
        S->top=-1;
        return OK;
}

/* 把S置为空栈 */
Status ClearStack(SqStack *S)
{ 
        S->top=-1;
        return OK;
}

/* 若栈S为空栈,则返回TRUE,否则返回FALSE */
Status StackEmpty(SqStack S)
{ 
        if (S.top==-1)
                return TRUE;
        else
                return FALSE;
}

/* 返回S的元素个数,即栈的长度 */
int StackLength(SqStack S)
{ 
        return S.top+1;
}

/* 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR */
Status GetTop(SqStack S,SElemType *e)
{
        if (S.top==-1)
                return ERROR;
        else
                *e=S.data[S.top];
        return OK;
}

/* 插入元素e为新的栈顶元素 */
Status Push(SqStack *S,SElemType e)
{
        if(S->top == MAXSIZE -1) /* 栈满 */
        {
                return ERROR;
        }
        S->top++;                /* 栈顶指针增加一 */
        S->data[S->top]=e;      /* 将新插入元素赋值给栈顶空间 */
        return OK;
}

/* 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR */
Status Pop(SqStack *S,SElemType *e)
{ 
        if(S->top==-1)
                return ERROR;
        *e=S->data[S->top];        /* 将要删除的栈顶元素赋值给e */
        S->top--;                /* 栈顶指针减一 */
        return OK;
}

/* 从栈底到栈顶依次对栈中每个元素显示 */
Status StackTraverse(SqStack S)
{
        int i;
        i=0;
        while(i<=S.top)
        {
                visit(S.data[i++]);
        }
        printf("
");
        return OK;
}

int main()
{
        int j;
        SqStack s;
        int e;
        if(InitStack(&s)==OK)
                for(j=1;j<=10;j++)
                        Push(&s,j);
        printf("栈中元素依次为:");
        StackTraverse(s);
        Pop(&s,&e);
        printf("弹出的栈顶元素 e=%d
",e);
        printf("栈空否:%d(1:空 0:否)
",StackEmpty(s));
        GetTop(s,&e);
        printf("栈顶元素 e=%d 栈的长度为%d
",e,StackLength(s));
        ClearStack(&s);
        printf("清空栈后,栈空否:%d(1:空 0:否)
",StackEmpty(s));
        
        return 0;
}


输出为:
栈中元素依次为:1 2 3 4 5 6 7 8 9 10 
弹出的栈顶元素 e=10
栈空否:0(1:空 0:否)
栈顶元素 e=9 栈的长度为9
清空栈后,栈空否:1(1:空 0:否)

  6.栈的顺序存储结构的Java语言代码实现:

  • 接口类:
package bigjun.iplab.sequenceStack;

public interface StackINF {
    // 判断顺序栈是否为空
    public boolean isStackEmpty();
    // 将一个已经存在的顺序栈置成空表
    public void stackClear();
    // 求顺序栈的长度
    public int stackLength();
    // 读取顺序栈的栈顶元素
    public int getTopElem() throws Exception;
    // 在顺序栈中插入元素e
    public void stackPush(int e) throws Exception;
    // 删除顺序栈中的栈顶元素
    public void stackPop() throws Exception ;
    // 输出顺序栈中的所有元素
    public void stackTraverse();
}
  • 实现类:
package bigjun.iplab.sequenceStack;

public class SequenceStack implements StackINF{
    
    private final static int MAXSIZE = 20;
    private int[] stackElem;
    private int top;             // 将top设置为指向栈顶元素的存储位置即数组下标,则空栈时,top==-1
    
    public SequenceStack() {
        top = -1;
        stackElem = new int[MAXSIZE];
    }

    public boolean isStackEmpty() {
        if (top == -1) 
            return true;
        else
            return false;
    }

    public void stackClear() {
        top = -1;
    }

    public int stackLength() {
        return top + 1 ;
    }

    public int getTopElem() throws Exception{
        if (top == -1) 
            throw new Exception("栈为空,无法获取栈顶元素");
        else {
            return stackElem[top];
        }
    }

    public void stackPush(int e) throws Exception {
        if (top == MAXSIZE -1) 
            throw new Exception("栈为满,无法在栈顶插入元素e");
        else 
            top++;
            stackElem[top] = e;
    }

    public void stackPop() throws Exception {
        if (top == -1) 
            throw new Exception("栈为空,无法删除栈顶元素");
        else {
            top--;
        }
    }

    public void stackTraverse() {
        System.out.print("此时,栈中的元素为: ");
        int i = 0;
        while (i <= top) {
            System.out.print(stackElem[i++] + " ");
        }
        System.out.println();
    }
    
    public static void main(String[] args) throws Exception {
        SequenceStack seqStack = new SequenceStack();
        
        for (int j = 1; j <= 10; j++) 
            seqStack.stackPush(j);
        seqStack.stackTraverse();
        
        seqStack.stackPop();
        seqStack.stackTraverse();
        System.out.println("栈顶元素为: " + seqStack.getTopElem());
        System.out.println("栈的长度为: " + seqStack.stackLength());
        
        System.out.println("栈是否为空: " + seqStack.isStackEmpty());
        seqStack.stackClear();
        System.out.println("栈是否为空: " + seqStack.isStackEmpty());
    }

}
  • 输出:
此时,栈中的元素为: 1 2 3 4 5 6 7 8 9 10 
此时,栈中的元素为: 1 2 3 4 5 6 7 8 9 
栈顶元素为: 9
栈的长度为: 9
栈是否为空: false
栈是否为空: true
原文地址:https://www.cnblogs.com/BigJunOba/p/9183276.html