[] Data structure - stacks and stacks of applications (Reverse Polish Notation)

Defined stack

Stack operation is a restricted linear table, the latter is advanced out of the data structure is defined only insertions and deletions at one end, called the top of the stack to allow an end of the operation, known as bottom of the stack operation is not allowed.

Sequential storage structure:
1, the storage space occupied by the elements must be continuous (consecutive herein refers to a logical continuous, rather than physically continuous)
2, the position of the element is located in the storage space in a logical order

Here Insert Picture Description
From the above it can be seen concise structure stack.

Simple implementation of the stack

#include <iostream>
#include <stdlib.h>
using namespace std;

#define Max_size 100

typedef struct sta
{
	int top;   //栈顶元素
	int data[Max_size]; 
	int size;  //栈的大小
}stack;

stack InitStack()   //栈的初始化
{
	stack s;
	s.size = Max_size;
	s.top = -1;
	return s;
}

//判断栈是否为空
bool StackEmpty(stack s)
{
	if (s.top == -1)
	{
		return true;
	}
	else
		return false;
}
//判断栈是否已满
bool StackFull(stack s)
{
	if (s.top == Max_size - 1)
	{
		return true;
	}
	else
	{
		return false;
	}

}
void push(stack &s, int data)    //入栈
{
	if (StackFull(s) == true)
	{
		cout << "栈已满,不可入栈" << endl;
	}
	s.top++;
	s.data[s.top] = data;
}
void pop(stack& s)  //出栈
{
	if (StackEmpty(s) == true)
	{
		cout << "空栈!!!无法出栈" << endl;
	}
	else
	{
		int x = s.data[s.top];
		cout << "出栈元素为:" << x << endl;
		s.top--;
	}
}

//遍历栈中的元素
void display(stack s)
{
	while (s.top != -1)
	{
		cout << s.data[s.top] << " ";
		s.top--;
	}
	cout << endl;
}

int main()
{
	stack p;
	p = InitStack();
	cout << "入栈元素为:" << endl;
	push(p, 1);
	push(p, 2);
	push(p, 3);
	push(p, 4);
	display(p);

	pop(p);
	display(p);

	system("pause");
	return 0;
}

Stack of applications

Stack has many applications: for example, matching brackets, reverse Polish notation
here mainly speak a: Reverse Polish Notation

  • The basic idea:
    If the string of space-time, direct return 0.
    If not empty, all the numbers stack, the stack will not encounter characters operators, two elements directly out of the top of the stack, calculates, and then stores the result in the stack, continue to get the character, and so on.
  • code show as below:
class Solution 
{
public:
    int evalRPN(vector<string>& tokens) 
    {
            if(tokens.size()==0)
                return 0;
            stack<int> tmp;
            for(int i=0;i<tokens.size();i++)
            {
                if(tokens[i].size()>1||(tokens[i].size()==1
                &&tokens[i]>="0"&&tokens[i]<="9"))
                {
                    tmp.push(atoi(tokens[i].c_str()));
                }
                else
                {
                    int a=tmp.top();
                    tmp.pop();

                    int b=tmp.top();
                    tmp.pop();
                    int res;

                    if(tokens[i]=="+")
                            res=a+b;
                    else if(tokens[i]=="-")
                            res=b-a;
                    else if(tokens[i]=="*")
                            res=a*b;
                    else if(tokens[i]=="/")
                            res=b/a;
                    tmp.push(res);
                }
            }
            return tmp.top();
    }
};
Published 33 original articles · won praise 13 · views 1051

Guess you like

Origin blog.csdn.net/Vicky_Cr/article/details/104467455