# Introduction to stack (c language implementation)

Posted by jimmyo on Tue, 08 Mar 2022 22:24:00 +0100

# preface

When you use the browser to surf the Internet, almost all browsers have a back button. After you click it, you can return to the previously loaded web pages in reverse order of access order. There are many similar operations in life. Using word undo, their principle is a data structure we will introduce next - stack.

# 1, What is a stack?

Definition of stack: a stack is a linear table that is restricted to insert and delete operations only at the end of the table.
We call the end that allows insertion and deletion as the top of the stack, the other end as the bottom of the stack, and the stack without any data elements as an empty stack. Stack is also called * * Last In First Out * * linear table, or LIFO structure for short.

# 2, Characteristics and operation of stack

## 1. Characteristics of stack

As mentioned in the above article, the characteristic of the stack is that it can only operate at the end of the table, and the stack is always operated at the top of the stack, which makes the bottom of the stack fixed and the first data can only be entered at the bottom of the stack. However, the first element in the stack is not necessarily the last one out of the stack.
For example: now let's put 1, 2 and 3 shaping elements on the stack in turn. What will be the stacking order?
The first one: 1, 2, 3 in, 3, 2, 1 out, which is the easiest one we can think of.
The second type: 1 in, 1 out, 2 in, 2 out, 3 in, 3 out. That is, one out and one in. The first one in the stack goes out first.
There are several other schemes, which are not listed here.
Let's introduce the basic operation of stack.

## 2. Basic operation of stack:

There are two basic operations of stack: entering stack and entering stack. We can also understand it as the pressing and ejecting of bullets.

### First, we define the stack (sequential storage stack).

The code is as follows (example):

```#include<stdio.h>
#define MAXSIZE 100;
typedef struct {
int data[MAXSIZE];	//Use the array as the storage space of the stack
int top;	//Top as stack top pointer
}stack;
```

Here is the virtual stack with array.

### Stack operation - push.

The operation of entering the stack is called inserting, which is also called pressing the stack. Even if new elements are added to the top of the stack, we should first judge whether the stack is full. If not, we can insert. The condition to judge whether the stack is full is not top == maxsize - 1.
The code is as follows (example):

```int push(stack *s, int val) { //val is stack data
if (s->top == MAXSIZE - 1) { //At this time, the stack is full and cannot be put into the stack.
return 0;
}
s->top++;	//The stack top pointer points to the new stack top.
s->data[s->top] = val;	//Stack new elements.
return 1;
}
```

### Stack out operation - pop.

Stack out operation, that is, delete, also known as pop-up stack. When we perform stack out operation, we should judge whether there are elements in the stack. If there are no elements, the top pointer of the stack points to the bottom of the stack, that is, top == -1;
The code is as follows (example):

```int pop(stack *s, int *val) {	//Assign the pop-up stack top element to val.
if (s->top == -1) {		    //At this time, the stack is empty.
return 0;
}
*val = s->data[s->top];
s->top;						//Stack top pointer minus one
return 1;
}
}
```

### On the application of sequential stack -- effective parentheses

Title Valid parentheses
Given a string s that only includes' (',') ',' {','} ',' [','] ', judge whether the string is valid.
Valid strings must meet:
1. The left bracket must be closed with the same type of right bracket.
2. The left parentheses must be closed in the correct order.

Example 1:
Input: s = "()" output: true
Example 2:
Input: s = "() [] {}" output: true
Example 3:
Input: s = "(]" output: false
Example 4:
Input: s = "([)]" output: false
Example 5:
Input: s = "{[]}" output: true

Tips:
1 <= s.length <= 104
Consists of '{]}' only
The code is as follows (example):

```#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char pairs(char a) {
if (a == ')') return '(';
if (a == '}') return '{';
if (a == ']') return '[';
return 0;
}
int isValid(char * s) {
int length , i;
length = strlen(s);
if (length % 2 == 1) {
return 0;
}
int stack[length + 1] , top = 0;
for(i = 0;i < length; i++){
char ch = pairs(s[i]);
if(ch) {
if(top == 0 || stack[top - 1] != ch) {
return 0;
}
top--;
} else{
stack[top++] = s[i];
}
}
if (top == 0) {
return 1;
}
if (top != 0) {
return 0;
}
}
int main(void) {
char str1[100];
scanf("%s",str1);
int a = isValid(str1);
if (a == 0) {
printf("false");
} else {
printf("true");
}

}

```

# summary

There may be many flaws in writing a blog for the first time.
Although most of us use virtual stack, stack is an idea. The introduction of stack simplifies the problem of programming and divides different levels of attention, which makes us focus more on the core of the problem.

Topics: C stack