数据结构学习系列之顺序栈

  • 栈的基本概念:
  • 栈(Stack):一种后进先出(LIFO)的线性表
  • 栈顶(top):栈允许插入删除的一端;
  • 进栈(压栈、入栈):栈的插入操作;
  • 出栈:栈的删除操作;
  • 顺序栈:
  • 即:栈的顺序存储结构;
  • 进栈(push):
int push_seq_stack(stack_t *seq_stack,int data)
{
    if(NULL == seq_stack)
    {
        printf("入参合理性检查\n");
        return -1;
    }

    if(stack_is_full(seq_stack))
    {
        printf("此时栈满,进栈失败\n");
        return -1;
    }

    seq_stack->a[seq_stack->top] = data;
    seq_stack->top++;
    return 0;

}
  • 出栈(pop):
int pop_seq_stack(stack_t *seq_stack , int *data)
{
    if(NULL == seq_stack || NULL == data)
    {
        printf("入参合理性检查\n");
        return -1;
    }

    if(stack_is_empty(seq_stack))
    {
        printf("此时栈空,出栈失败\n");
        return -1;
    }

    seq_stack->top--;
    *data = seq_stack->a[seq_stack->top];

    return 0;
}
  • 示例代码:
  • seq_stack.h:
#ifndef __SEQ_STACK_H__
#define __SEQ_STACK_H__


#include 
#include 
#include 
#define N 5


typedef struct Seq_stack
{

    int a[N];
    int top;  //栈顶指针
   
}stack_t;

int create_seq_stack(stack_t **P);
int stack_is_full(stack_t *seq_stack);
int push_seq_stack(stack_t *seq_stack,int data);
int stack_is_empty(stack_t *seq_stack);
int pop_seq_stack(stack_t *seq_stack,int *data);
int clean_seq_stack(stack_t *seq_stack);
int destroy_seq_stack(stack_t **seq_stack);
int print_seq_stack(stack_t *seq_stack);



#endif
  • seq_stack.c:

#include "seq_stack.h"


//创建顺序栈
int create_seq_stack(stack_t **p)
{
    if(NULL == p)
    {
        printf("入参合理性检查");
        return -1;
       
    }

    *p = (stack_t *)malloc(sizeof(stack_t));

    if(NULL == *p)
    {

        printf("内存分配失败\n");
        return -1;
            
    }

    memset(*p,0,sizeof(stack_t));

    return 0;

}

//判断栈是否为满
int stack_is_full(stack_t *seq_stack)
{
    if(NULL == seq_stack)
    {
        printf("入参合理性检查\n");
        return -1;
    }

    return seq_stack->top == N ? 1 : 0;

}

//进栈
int push_seq_stack(stack_t *seq_stack,int data)
{
    if(NULL == seq_stack)
    {
        printf("入参合理性检查\n");
        return -1;
    }

    if(stack_is_full(seq_stack))
    {
        printf("此时栈满,进栈失败\n");
        return -1;
    }

    seq_stack->a[seq_stack->top] = data;
    seq_stack->top++;
    return 0;

}
//判断栈是否为空
int stack_is_empty(stack_t *seq_stack)
{
    if(NULL == seq_stack)
    {
        printf("入参合理性检查\n");
        return -1;
    }

    return seq_stack->top == 0 ? 1 : 0;

}
//出栈
int pop_seq_stack(stack_t *seq_stack , int *data)
{
    if(NULL == seq_stack || NULL == data)
    {
        printf("入参合理性检查\n");
        return -1;
    }

    if(stack_is_empty(seq_stack))
    {
        printf("此时栈空,出栈失败\n");
        return -1;
    }

    seq_stack->top--;
    *data = seq_stack->a[seq_stack->top];

    return 0;
}

//清空栈
int clean_seq_stack(stack_t *seq_stack)
{
    if(NULL == seq_stack)
    {
        printf("入参合理性检查\n");
        return -1;
    }
    seq_stack->top = 0;
    return 0;


}
//销毁栈
int destroy_seq_stack(stack_t **seq_stack)
{
    if(NULL == seq_stack || NULL == *seq_stack)
    {
        printf("入参合理性检查\n");
        return -1;
    }
    free(*seq_stack);
    *seq_stack = NULL;
    return 0;

}
//遍历栈
int print_seq_stack(stack_t *seq_stack)
{

    if(NULL == seq_stack)
    {
        printf("入参合理性检查\n");
        return -1;
    }
    int i = 0;
    while(i < seq_stack->top)
    {
        printf("%d ",seq_stack->a[i]);
        i++;
    }
    puts("");
    return 0;

}

  • main.c:
#include "seq_stack.h"

int main(int argc, char const *argv[])
{
    stack_t *seq_stack = NULL;
    create_seq_stack(&seq_stack);
    printf("seq_stack = %p\n",seq_stack);

    //进栈
    push_seq_stack(seq_stack,11);
    push_seq_stack(seq_stack,22);
    push_seq_stack(seq_stack,88);
    push_seq_stack(seq_stack,55);
    push_seq_stack(seq_stack,77);


    print_seq_stack(seq_stack);

    push_seq_stack(seq_stack,100);


    //出栈
    int data;
    pop_seq_stack(seq_stack,&data);
    printf("%d ",data);
    pop_seq_stack(seq_stack,&data);
    printf("%d ",data);
    pop_seq_stack(seq_stack,&data);
    printf("%d ",data);
    pop_seq_stack(seq_stack,&data);
    printf("%d ",data);
    pop_seq_stack(seq_stack,&data);
    printf("%d ",data);

    puts("");

    pop_seq_stack(seq_stack,&data);
 

    //清空
    clean_seq_stack(seq_stack);
    print_seq_stack(seq_stack);

    //销毁
    destroy_seq_stack(&seq_stack);
    printf("seq_stack = %p\n",seq_stack);

    return 0;
}

  • 运行结果:
seq_stack = 0x55c8d992c260
11 22 88 55 77 
此时栈满,进栈失败
77 55 88 22 11 
此时栈空,出栈失败

seq_stack = (nil)
  • 本示例代码,仅供参考;

你可能感兴趣的:(数据结构学习系列,数据结构,学习,算法,c语言,栈,顺序栈)