Table of contents
01 Implementation principle of the stack
02 Practical application of the stack
02.2 Infix expression to suffix expression
02.4 Convert decimal to binary
foreword
The source of this study note is Embedded Technology Open Course Data Structure Tutorial
This study note is continuously updated... welcome to communicate.
00 Data Structure Beginning
data structure + algorithm = program
Algorithm: the steps a computer takes to solve a problem
Data structure: the arrangement of the data as the object of processing
Data is the center of the program
Data structure is the most effective way to organize data
Senior programmer: data structure + algorithm + mathematical thinking + process control + logical thinking
data structure:
1. Basic data structures: stack, queue, tree, linked list
2, figure
3. Sort
4. Find/Search
5. Advanced data structures
algorithm:
Greedy algorithm, distribution system, dynamic programming, backtracking method
01 Implementation principle of the stack
array + loop == stack queue
01.1 What is a stack ?
...similar to magazines ...going into an elevator...books in boxes
last in first out first in first out
The content in the stack can be any data type ( the content stored in the array )
01.2 Operations on the stack
Push and pop to determine whether the stack is empty
The pointer always points to the top of the stack
code example
#include <stdio.h>
char stack[512];
int top = 0;
void push(char c);
char pop(void);
int is_empty(void);
int main(void)
{
push('a');
push('b');
push('c');
while(!is_empty())
{
putchar(pop());
}
printf("\n");
return 0;
}
void push(char c)
{
stack[top++] = c;
}
char pop(void)
{
return stack[--top];
}
int is_empty(void)
{
/*
if(top == 0)
return 1;
else
return 0;
*/
return top == 0;
}
02 Practical application of the stack
02.1 Reverse Polish notation
Think about the computer to achieve the following operations?
5*(((8+9)*(4*6))+7)
The start of the calculation is uncertain
So how can we turn this uncertainty into decisiveness!
If there are (1+2)*(3-4)
Reverse Polish representation 12+34-*
Encountered numbers from left to right are pushed onto the stack, encountered operators are popped out of the stack twice in a row, and the calculated results are pushed onto the stack
What did you end up with? That's right, it is the result of following the four arithmetic operations
Calculate 89+46**7+5* to see if the result is the same as you think!
The specific implementation code is given below
#include <stdio.h>
#include <string.h>
int stack[512];
int top = 0;
void push(int c);
int pop(void);
int is_empty(void);
int main(void)
{
char a[100];
int n;
int i;
int n1, n2;
printf("Please enter a reverse polish expression:\n");
gets(a);
n = strlen(a);
for(i = 0; i < n; i++)
{
if((a[i] >= '0') && (a[i] <= '9'))
push(a[i] - '0');
else
{
n2 = pop();
n1 = pop();
switch(a[i])
{
case '+':
push(n1 + n2);
break;
case '-':
push(n1 - n2);
break;
case '*':
push(n1 * n2);
break;
}
}
}
printf("result = %d\n", pop());
return 0;
}
void push(int c)
{
stack[top++] = c;
}
int pop(void)
{
return stack[--top];
}
int is_empty(void)
{
/*
if(top == 0)
return 1;
else
return 0;
*/
return top == 0;
}
02.2 Infix expression to suffix expression
In 02.1 we said that (1+2)*(3-4) is an infix expression and after processing, 12+ 34-* is a suffix expression, so how to convert the infix expression into Suffix expression?
Give the algorithm idea
((1+2)*(3-4))
- Process data from left to right
- When an opening parenthesis is encountered, it is ignored
- When a value is encountered, output it directly
- Push operators onto the stack when they are encountered
- When a closing parenthesis is encountered, the operator at the top of the stack is popped
code implementation
#include <stdio.h>
#include <string.h>
char stack[512];
int top = 0;
void push(char c);
char pop(void);
int is_empty(void);
int main(void)
{
char str[100];
int i, len;
printf("Please enter a calcuate expression:");
gets(str);
len = strlen(str);
for(i = 0; i < len; i++)
{
if(str[i] == '(')
continue;
else if((str[i] >= '0') && (str[i] <= '9'))
printf("%c", str[i]);
else if((str[i] == '+') || (str[i] == '-') || (str[i] == '*'))
push(str[i]);
else if(str[i] == ')')
printf("%c", pop());
}
printf("\n");
return 0;
}
void push(char c)
{
stack[top++] = c;
}
char pop(void)
{
return stack[--top];
}
int is_empty(void)
{
/*
if(top == 0)
return 1;
else
return 0;
*/
return top == 0;
}
02.3 Bracket matching
Introduction to the problem - can you accurately find the right parenthesis corresponding to the left parenthesis in the four arithmetic operations!
Example: a[6]=(())()
1-2
0-3
4-5
Algorithm idea: Search from left to right (ignore left brackets first) When encountering right brackets, start to find the nearest unmatched left
bracket
How to enter the number?
Push the number instead of the left parenthesis onto the stack
programming
#include <stdio.h>
#include <string.h>
int stack[512];
int top = 0;
void push(int c);
int pop(void);
int is_empty(void);
int main(void)
{
char str[100];
int i, len;
printf("Please enter a calculate expression:");
gets(str);
len = strlen(str);
for(i = 0; i < len; i++)
{
if(str[i] == '(')
push(i);
else if(str[i] == ')')
printf("%d %d\n", pop(), i);
}
return 0;
}
void push(int c)
{
stack[top++] = c;
}
int pop(void)
{
return stack[--top];
}
int is_empty(void)
{
/*
if(top == 0)
return 1;
else
return 0;
*/
return top == 0;
}
02.4 Convert decimal to binary
Problem introduction - how to convert decimal 13 to binary?
Algorithm idea :
1. Judging whether n is 0 (n!=0), n%2 updates the value of n (n=n/2)
2. Repeat the operation of the first step until the operation is n=0
13
13%2 = 1 13/2 = 6
6%2 = 0 6/2 = 3
3%2=1 3/2=1
1%2 =1 1/2 = 0
3. As long as the stack is not empty, the content on the top of the stack will be popped up, and the printed display is the result of our conversion
programming
#include <stdio.h>
int stack[512];
int top = 0;
void push(int c);
int pop(void);
int is_empty(void);
int main(void)
{
int num;
printf("Please enter an integer in decimal:");
scanf("%d", &num);
while(num)
{
push(num % 2);
num /= 2;
}
while(!is_empty())
printf("%d", pop());
printf("\n");
return 0;
}
void push(int c)
{
stack[top++] = c;
}
int pop(void)
{
return stack[--top];
}
int is_empty(void)
{
/*
if(top == 0)
return 1;
else
return 0;
*/
return top == 0;
}
02.5 Palindrome judgment
What are palindromic numbers?
It is a line of characters that are written upside down and written backwards. For example abccba 12344321
algorithm thinking
1. Find the length of the string, and push the first half of the characters onto the stack in turn.
2. If the stack is not empty, pop the top element of the stack and compare it with the second half of the string. If the length of the string is odd, skip the element at the center point and compare the elements behind the center point.
3. If the compared elements are equal, compare until the stack is empty. At this time, the return is a palindrome. If the comparison is not equal, the comparison is stopped immediately and a non-palindrome is returned.
programming
#include <stdio.h>
#include <string.h>
char stack[512];
int top = 0;
void push(char c);
char pop(void);
int is_empty(void);
int is_palindrom(char *pt);
int main(void)
{
char str[100];
printf("Please enter a string:");
gets(str);
if(is_palindrom(str))
printf("str is a palindrom.\n");
else
printf("str is not a palindrom.\n");
return 0;
}
void push(char c)
{
stack[top++] = c;
}
char pop(void)
{
return stack[--top];
}
int is_empty(void)
{
/*
if(top == 0)
return 1;
else
return 0;
*/
return top == 0;
}
int is_palindrom(char *pt)
{
int len, i;
len = strlen(pt);
for(i = 0; i < len/2; i++)
push(pt[i]);
if(len % 2 == 1)
i++;
while(!is_empty())
{
if(pop() == pt[i])
i++;
else
return 0;
}
return 1;
}