# Data structure Chapter 3 basic operation of stack

## Introduction to stack:

Stack is a linear list or linked list that can only be inserted or deleted at one end. The end of the list that allows insertion and deletion is called the top of the stack, and the other end of the table is called the bottom of the stack
When there is no element in the stack, it is called an empty stack. The insertion of the stack is usually called entering the stack or entering the stack, and the deletion of the stack is usually called leaving the stack or leaving the stack.

## Stack features:

The stack is characterized by last in first out (LIFO), that is, last in first out, and the later elements come out of the stack first

## Type of stack

The stack with linear storage structure is called sequential stack, and the stack with chain storage structure is called chain stack

## Basic operation of stack:

The chain stack is used here, and the sequence stack will be explained in detail in the following articles

```//Initialization stack
//Destroy stack
//Air judgment
//Enter the stack
//Out of stack
//Get stack top element
//Point the pointer to the last element
//Find the length of the stack
//Stack traversal
```

## Implementation of basic operation of stack:

state invariant
It is convenient for subsequent changes after the header definition. Status is the alias of int, which is also convenient for subsequent changes

```#include <stdlib.h>
#include <stdio.h>
#define OK  1
#define ERROR 0
#define OVERFLOW –2
#define TRUE 1
#define FALSE 0
typedef int Status;
```

Structure definition of stack
ElemType defines the type of the stack node. Alias is used for subsequent changes

```typedef int ElemType;
ElemType data;
```

Initialization stack
The initialization stack is too simple and will not be described in detail here

```Status InitStack(LinkSqStack *Stack){
if (!Stack) {
return FALSE;
}
Stack->next = NULL;
return OK;
}
```

Destroy stack
Destroy the stack, because the nodes are created one by one when they are created, so they should be destroyed one by one when they are destroyed. Two auxiliary pointers are needed. One pointer points to the first element node and the other pointer points to the next node of the first element node. Then, the stack can be destroyed by using the two pointers to cycle in turn, and the head node will be released later

```Status DestoryStack(LinkSqStack *Stack){
LinkSqStack *p = Stack,*q = Stack->next;
while (q) {
p = q;
q = q->next;
free(p);
}
free(q);
printf("Stack destroyed\n");
return OK;
}
```

Empty judgment of stack
If the following of the header node is empty, the stack is empty

```Status EmptyStack(LinkSqStack *Stack){
if (Stack->next == NULL) {
printf("Stack empty\n");
return FALSE;
}else{
printf("Stack is not empty\n");
return OK;
}
}
```

Stack or stack operation
Generally, Push is used to represent the stack operation, create a new node, transfer the data to be stacked to this node, and then insert this node into the top of the stack (here, the top of the stack is in the front, and the initial node is the top of the stack, which is to insert and delete at the front of the linked list)

```Status push(LinkSqStack *Stack,ElemType e){
p->data = e;
p->next = Stack->next;
Stack->next = p;
return OK;
}
```

Out of stack or out of stack operation
First, judge whether the stack is empty, then move an auxiliary pointer to the first element node (Note: the value must be taken out before deleting the node), let the pointer field of the head node record the pointer field of the first element node, and then release the first element node

```Status Pop(LinkSqStack *Stack,ElemType *e){
if (Stack->next == NULL) {
printf("Stack empty\n");
return FALSE;
}
p = Stack->next;
*e = p->data;
Stack->next = p->next;
free(p);
return OK;
}
```

Get stack top element
First, judge whether the stack is empty, and then take the value directly without deleting the node

```Status GetPop(LinkSqStack *Stack,ElemType *e){
if (Stack->next == NULL) {
printf("Stack empty\n");
return FALSE;
}
*e = Stack->next->data;
return OK;
}
```

Find the length of the stack
The operation is too simple to explain here

```Status LengthStack(LinkSqStack *Stack){
int count = 0;
while (Stack) {
Stack = Stack->next;
count++;
}
printf("The capacity of the stack is%d",count);
return OK;
}
```

Stack traversal
First, move the pointer to the first element node because the head node does not maintain the data field, and then use the while loop to traverse the stack until the pointer points to the bottom element of the stack, and the pointer field of the bottom element of the stack is empty

```Status TraverseStack(LinkSqStack *Stack){
Stack = Stack->next;
if (Stack->next == NULL) {
printf("Stack empty\n");
return FALSE;
}
while (Stack) {
printf("%d\t",Stack->data);
Stack =Stack->next;
}
return OK;
}
```

The following is the stack test code

```int main(void){
ElemType e;
InitStack(&Stack);
for (int i = 0; i < 9; i++) {
push(&Stack, i);
}
EmptyStack(&Stack);
LengthStack(&Stack);
printf("\n");
TraverseStack(&Stack);
printf("\n");
GetPop(&Stack, &e);
printf("Stack top element%d",e);
printf("\n");
printf("Element out of stack (the first three elements out of stack)\n");
for (int j = 0; j < 3; j++) {
Pop(&Stack, &e);
printf("%d\t",e);
}
printf("\n");
DestoryStack(&Stack);
}

```

test result Tags: data structure

Posted on Sun, 24 Oct 2021 05:55:10 -0400 by mysterio2099