## 1. Definition of stack

### 1.1 definition

**Stack is a linear table that only inserts and deletes at the end of the table.****Top of stack**The end that allows insertion and deletion is called the top of the stack.**Stack bottom**: the other end corresponding to the top of the stack is called the bottom of the stack.**Empty stack**: a stack without any data elements is called an empty stack.**Characteristics of stack**First in, last out, also called last in, first out.

Stack is a kind of special linear table. The elements in the stack have linear relationship, that is, the relationship between precursor and successor. In the definition, only insert and delete at the end of the table, that is, insert and delete at the top of the stack, not at the bottom of the stack.

### 1.2 concept of insertion and deletion

**The insertion operation of stack is called stack entry, also called stack pressing and stack entry**As shown in the figure below:**Stack delete operation, called outbound, also known as bomb stack**As shown in the figure below:

## 2. Sequential storage structure of stack

### 2.1 definition of stack sequential storage structure

Stack is a special linear table. If we use sequential storage structure, we call it sequential stack. We use array to realize sequential storage.

In the sequential storage structure, if the insertion and deletion are carried out at the head of the array, the subsequent element shift will be involved, so it will be more convenient for us to insert and delete at the end of the array. Therefore, we call the head of the array (that is, the position with subscript 0) as the bottom of the stack, and the tail of the array as the top of the stack.

We define the top pointer to record the position of the top element in the array. If the length of the stack is maxsize, then the top position must be less than maxsize. When there is an element in the stack, top is equal to 0. If it is empty, top is equal to – 1.

**Structure definition of sequential stack**：

```
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#Define maxsize 20 / * initial storage allocation*/
typedef int Status;
typedef int SElemType; /* The selemtype type depends on the actual situation. Here, it is assumed to be int*/
/*Sequential stack structure*/
typedef struct
{
SElemType data[MAXSIZE];
int top; /* Used to point to the top of the stack*/
}SqStack;
```

### 2.2 sequence stack initialization

The structure of the sequential stack is relatively simple. We use a storage unit with continuous addresses to store data, so when we create the stack object, we apply for storage space, so we only need to set the initial value for initialization.

**code implementation**：

```
//Building an empty stack s
Status InitStack(SqStack *S){
//If the stack does not exist, error is returned.
if (S == NULL) {
return ERROR;
}
//Set top to - 1 and the stack is empty.
S->top = -1;
return OK;
}
```

### 2.3 sequential stack operation — insert element

Entering the stack means inserting elements at the top of the stack.**thinking**

- Judge whether the stack is full. If it is full, the data cannot be inserted and error is returned.
- Set the top pointer to top + +.
- Assign the newly inserted element to the top of stack space.
- Return to OK.

**code implementation**

```
//Insert element E as the new top of stack element
Status PushData(SqStack *S, SElemType e){
//The stack is full
if (S->top == MAXSIZE -1) {
return ERROR;
}
//Top of stack pointer + 1;
S->top ++;
//Assign the newly inserted element to the top of stack space
S->data[S->top] = e;
return OK;
}
```

### 2.4 sequential stack out operation — deleting elements

**thinking**

- Judge whether the stack is empty. If it is empty, the operation cannot be performed and error is returned.
- Assign the deleted element to e and bring it back to the calling function.
- Set the top of stack pointer to top –.
- Return to OK.

**code implementation**

```
//Delete the top element of s stack and bring it back with E
Status Pop(SqStack *S,SElemType *e){
//If the stack is empty, error is returned;
if (S->top == -1) {
return ERROR;
}
//Assign the top of stack element to E
*e = S->data[S->top];
//Top of stack pointer --;
S->top--;
return OK;
}
```

### 2.5 sequence stack empty and judgment

**Empty**

To empty the stack, we don’t need to empty all the elements of the sequential stack, we just need to modify the top pointer.

**code implementation**

```
//Empty stack
Status ClearStack(SqStack *S){
S->top = -1;
return OK;
}
```

**judge**

As mentioned above, when the top pointer of the stack is – 1, the stack is empty.

**code implementation**

```
//Determine whether the sequence stack is empty;
Status StackEmpty(SqStack S){
if (S.top == -1)
return TRUE;
else
return FALSE;
}
```

### 2.6 getting the top elements of the sequential stack

To get the top element of the sequential stack is to get the last element of the array.

**code implementation**

```
//Get top of stack
Status GetTop(SqStack S,SElemType *e){
if (S.top == -1)
return ERROR;
else
*e = S.data[S.top];
return OK;
}
```

### 2.7 traversing sequential stack elements

Traversing sequential stack elements means traversing each element of the array.

**code implementation**

```
//Each element in the stack is printed from the bottom to the top
Status StackTraverse(SqStack S){
int i = 0;
Printf ("all elements in this stack");
while (i<=S.top) {
printf("%d ",S.data[i++]);
}
printf("\n");
return OK;
}
```

## 3. Chain storage structure of stack

### 3.1 definition of chain storage structure of stack

As we know about the sequential storage structure of the stack, if the stack is stored in the linked list structure, that is, the chain storage structure of the stack, we become the chain stack, or chain stack.

In a chained stack, we store each element of the stack in the form of a single linked list. How do we define the top and bottom of the stack? If the tail of a single linked list is regarded as the top of the stack, we need to traverse to find the tail element every time we insert or delete it. This is very annoying. Each linked list has its own head pointer. Therefore, the head of the linked list is regarded as the top of the stack, We just need to modify the context of linked list elements.

In the chain stack, we only operate in the head of the list, so the meaning of the head node of the list is gone, so the first element of the list is the head node.

In the sequential stack, we need to consider the stack overflow, because the space we apply for each time is fixed, but in the chain stack, we don’t need to consider the overflow, unless the computer’s memory is not enough.

In a chained stack, when the top pointer is null, it is considered to be an empty chained stack.

**Structure definition of chain stack**：

```
/*Chain stack node structure*/
typedef struct StackNode
{
SElemType data;
struct StackNode *next;
}StackNode,*LinkStackPtr;
/*Chain stack structure*/
typedef struct
{
LinkStackPtr top;
int count;
}LinkStack;
```

**Schematic diagram of chain stack**

### 3.2 chain stack initialization

There is no head node in the chain stack, so the initialization only needs to set the initial value, and there is no need to open up space.

**code implementation**

```
/*Construct an empty stack s*/
Status InitStack(LinkStack *S)
{
S->top=NULL;
S->count=0;
return OK;
}
```

### 3.3 chain stack operation — insert element

**thinking**

- Create a new node temp and assign a value to the new node.
- Point the successor of the new node temp to the current top node s – > top.
- Modify the stack top pointer s – > top to temp.
- Add 1 to the number of stack elements.
- Return to OK.

**code implementation**

```
/*Insert element E into chain stack s (become a new element at the top of the stack)*/
Status Push(LinkStack *S, SElemType e){
//Create new node temp
LinkStackPtr temp = (LinkStackPtr)malloc(sizeof(StackNode));
//Assignment
temp->data = e;
//Assign the current top of stack element to the direct successor of the new node. Refer to step 1 in the figure above;
temp->next = S->top;
//Assign the new node temp to the top of stack pointer. Refer to step 2 of the legend;
S->top = temp;
S->count++;
return OK;
}
```

### 3.4 chain stack out operation — delete element

**thinking**

- If there is no element in the stack, the element cannot be deleted and error is returned.
- Assign the value of the top of the stack element to e and bring it back to the calling function.
- Create a temporary variable p to point to the top of the stack element.
- Move the top pointer down one bit.
- Release the original top element P.
- Reduce the number of stack elements by 1
- Return to OK.

**code implementation**

```
/*If the stack is not empty, delete the top element of S, return its value with E, and return OK, otherwise return error*/
Status Pop(LinkStack *S,SElemType *e){
LinkStackPtr p;
if ((*S).count == 0) {
return ERROR;
}
//Assign the top of stack element to * e
*e = S->top->data;
//Assign the top node of stack to P, refer to legend ①
p = S->top;
//Make the top of stack pointer move down one bit to point to the next node
S->top= S->top->next;
//Release p
free(p);
//Number--
S->count--;
return OK;
}
```

### 3.5 empty chain stack and judgment

**Empty**

- Create node pointers P, Q, and point P to the top of the stack.
- Loop through the list, assign p to Q, P to the next node, release Q.
- When traversing to the end, that is, all nodes in the list have been released, set the number of stack elements to 0
- Return to OK.

**code implementation**

```
/*Set chain stack s to empty stack*/
Status ClearStack(LinkStack *S){
LinkStackPtr p,q;
p = S->top;
while (p) {
q = p;
p = p->next;
free(q);
}
S->count = 0;
return OK;
}
```

**judge**

To determine whether the stack is empty, you only need to determine whether count is 0.

**code implementation**

```
/*If stack s is empty, return true, otherwise return false*/
Status StackEmpty(LinkStack S){
if (S.count == 0)
return TRUE;
else
return FALSE;
}
```

### 3.6 chain stack to get top elements

To get the top element of chain stack is to get the first node element of chain list.

**code implementation**

```
/*If the chain stack s is not empty, return the top element of the stack with E and return OK, otherwise return error*/
Status GetTop(LinkStack S,SElemType *e){
if(S.top == NULL)
return ERROR;
else
*e = S.top->data;
return OK;
}
```

### 3.7 traversing chain stack elements

Traversing chain stack elements means traversing every element of the chain list.**code implementation**

```
/*Traversal chain stack*/
Status StackTraverse(LinkStack S){
LinkStackPtr p;
p = S.top;
while (p) {
printf("%d ",p->data);
p = p->next;
}
printf("\n");
return OK;
}
```