- 栈的基本概念:
- 栈(
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
#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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
#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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
#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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
seq_stack = 0x55c8d992c260
11 22 88 55 77
此时栈满,进栈失败
77 55 88 22 11
此时栈空,出栈失败
seq_stack = (nil)