Data structure stack implementation

Implementation of the data structure stack - using arrays

Ps: It is better to implement the stack with classes, which can manage multiple data structures at the same time. But for me who just got in touch, just look at the numbers and follow the process in the book. I will try my best to explain the characteristics of the stack clearly, and I will add it later when I have a deeper understanding.

Citation

Reverse Polish notation is a method of describing a program (computation) by writing the operator after the operand. For example, the formula (1+2)*(5+4) we usually describe in infix notation is 1 2 + 5 4 - * after being changed to reverse Polish notation. Compared with infix notation, reverse Polish notation has the advantage that no parentheses are required.
Please enter the calculation result of the formula entered in reverse polish notation.

enter Enter 1 calculation in one line. Adjacent symbols (operands or operators) are separated by a space character
output Output calculation results in one line
limit 2 ≤ The total number of operands in the formula ≤ 100 2\leq The total number of operands in the formula\leq1002The total number of operands in the expression1 0 0 ,1 ≤ The total number of operators in the formula ≤ 99 1\leqThe total number of operators in the formula\leq991total number of operators in the expression9 9 operators only contain "+" "-" "∗ * ”, the operand is1 0 6 10^610A positive integer below 6 . − 1 ∗ 1 0 9 ≤ value during calculation ≤ 1 0 9 -1*10^9\leqvalue during calculation\leq10^91109value during calculation _ _109
input example 1 2 + 3 4 - *
output example -3

Implemented in C++

#include<iostream>
#include<string>
#include<stdlib.h>
#define MAX 1000

using namespace std;

int a[MAX], top;


int pop();
void push(int x);

int main()
{
    
    
	int a, b;
	top = 0;
	char s[100];
	while (cin >> s)
	{
    
    
		if (s[0] == '+')
		{
    
    
			a = pop();
			b = pop();
			push(a + b);
		}
		else if (s[0] == '-')
		{
    
    
			b = pop();
			a = pop();
			push(a - b);
		}
		else if (s[0] == '*')
		{
    
    
			a = pop();
			b = pop();
			push(a * b);
		}
		else
		{
    
    
			push(atoi(s));
			//atoi函数将字符串形式的数字转化为整形数字
		}
	}
	printf("%d\n", pop());
	return 0;
}

void push(int x)
{
    
    
	S[++top] = x;
}

int pop()
{
    
    
	top--;
	return S[top + 1];
}

problem analysis

The program implements the stack with an array, and the data is stored in the character array s. Each cycle pushes the data into the "stack" S, and top is used as the top pointer of the stack, indicating where the last added element is stored in S. In each cycle, if a character type number is encountered, it will be converted into an integer type number, and top+1 will be shifted backward and pushed the number onto the stack. If an operator is encountered, top-1 will return the number behind it. Take two elements from the stack to perform the operation. The number remaining in the final stack is the result.
The element sent by push(x) is pushed into the stack after top+1, and pop() returns the element pointed to by top and then subtracts 1.

This is just a simple implementation of a stack with an array. A relatively complete stack implemented with an array should include the following functions.

//使用数组实现栈的伪代码
	initialize()   //initialize函数用来清空栈
		top = 0;
	isEmpty()     //isEmpty函数用来检查top是否为0,判断栈中是否有元素
		return top == 0;
	isFull()      //isFull函数用判断栈中是否已满
		return top >= MAX - 1;
	push(x)       
	/*push函数将top+1后将top所指位置加入元素,
	  并且每次判断栈是否已满*/
		if(isFull())
			错误(上溢)
		top++;
		S[top] = x;

	pop()
	/*pop函数返回top所指的元素,再将top-1
	  并且每次判断栈是否为空*/
		if(isEmpty())
			错误(下溢)
		top--;
		return S[top+1]

In general, data structures are mostly implemented in the form of structures or classes, and implementing in the form of classes can manage multiple data structures at the same time, which is convenient for programs to call data.

Guess you like

Origin blog.csdn.net/qq_32577169/article/details/90085438