[Data structure] Implementation of static sequential stack (with detailed comments)

Preface

  1. "Data Structure Series Home Page" is the home page of the data structure series of articles, which will be gradually updated Interested players can take a look at the implementation of various data structures.
  2. The homepage not only contains the implementation of various data structures, but also the basic knowledge necessary for learning data structures. If any contestants feel that they The foundation is not very solid, you can master the basic knowledge first, and then conquer the data structure part.
  3. Since I have just started learning the course of data structure, soIf you find any errors in the article, I hope you can point them out directly. Willmodify immediately.
  4. For more implementations of data structures, please see "Data Structure Series Articles". I will continue to update after learning new data structures. content therein.

opening remarks

The stack is a special linear list, which is limited to insertion and deletion operations only at the end of the list. The end that allows insertion and deletion is called the top of the stack (top), and the other end is called the bottom of the stack (bottom). The stack is also It is called a linear table called First In First Out (Last In First Out), or LIFO structure for short. This article uses a static one-dimensional array to implement the sequential storage of the stack and some operations on the sequential stack. The entire sequence stack is actually a sequence table with limited operations.

Implementation

head File
#include <stdio.h>	// printf、scanf函数所在的头文件

// 按照严蔚敏老师书中的编程习惯,定义如下4个宏
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0

typedef int Status;		// Status是函数返回值的类型,其返回的是函数执行结果的状态
typedef int ElemType;	// ElemType是元素的数据类型,想要整体改变元素的数据类型只需改变ElemType前的数据类型即可

// 定义静态顺序栈的存储结构
#define MAXSIZE 20 // 定义栈的存储空间的最大容量
typedef struct StaticStack
{
   
    
    
	ElemType data[MAXSIZE]; // 用于存储数据
	int top; // 用于存储栈顶元素的下标
}STACK; // 等同于 struct StaticStack
function declaration
void init_stack(STACK&);
void clear_stack(STACK&);
Status is_empty(STACK&);
Status is_full(STACK&);

Guess you like

Origin blog.csdn.net/weixin_65334260/article/details/125815962