Study notes: analog stack, analog queue

AcWing 828. Simulation stack

Implement a stack, the stack is initially empty, and supports four operations:

(1) "push x"-insert a number x to the top of the stack;

(2) "pop"-pop a number from the top of the stack;

(3) "empty"-judge whether the stack is empty;

(4) "query"-Query the top element of the stack.

Now we need to perform M operations on the stack, each of which must output corresponding results for operation 3 and operation 4.

Input format The
first line contains the integer M, which represents the number of operations.

The next M lines, each line contains an operation command, the operation command is one of "push x", "pop", "empty", and "query".

Output format
For each "empty" and "query" operation, a query result must be output, and each result occupies a row.

Among them, the query result of the "empty" operation is "YES" or "NO", and the query result of the "query" operation is an integer, which represents the value of the top element on the stack.

Data range
1≤M≤100000,
1≤x≤1e9
All operations are guaranteed to be legal.

#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#define ll long long
#define ull unsigned long long
#define up_b upper_bound
#define low_b lower_bound
#define m_p make_pair
#define mem(a) memset(a,0,sizeof(a))
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#include<algorithm>
using namespace std;

inline ll read()
{
    
    
	ll x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9')	{
    
     if(ch=='-') f=-1; ch=getchar(); }
	while('0'<=ch&&ch<='9')	x=x*10+ch-'0', ch=getchar();
	return f*x;
}

const int N = 1e5+5;

int stk[N],top;

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

void pop()
{
    
    
	top--;
}

bool empty()
{
    
    
	return top==0;
}

int query() //获取栈顶元素
{
    
    
	return stk[top];
}

int main()
{
    
    
	int m; cin>>m;
	string op; int x;
	while(m--)
	{
    
    
		cin>>op;
		if(op=="push")
		{
    
    
			cin>>x;
			push(x);
		}
		else if(op=="pop")	pop();
		else if(op=="empty")
		{
    
    
			if(empty())	cout<<"YES"<<endl;
			else	cout<<"NO"<<endl;
		}
		else cout<<query()<<endl;
	}
	return 0;
}

AcWing 829. Simulation queue

Implement a queue, the queue is initially empty, and supports four operations:

(1) "push x"-insert a number x to the end of the line;

(2) "pop"-pop a number from the head of the line;

(3) "empty"-judge whether the queue is empty;

(4) "query"-Query the head of the team.

Now we have to perform M operations on the queue, each of which must output corresponding results for operation 3 and operation 4.

Input format The
first line contains the integer M, which represents the number of operations.

The next M lines, each line contains an operation command, the operation command is one of "push x", "pop", "empty", and "query".

Output format
For each "empty" and "query" operation, a query result must be output, and each result occupies a row.

Among them, the query result of the "empty" operation is "YES" or "NO", and the query result of the "query" operation is an integer, which represents the value of the head element.

The data range is
1≤M≤100000,
1≤x≤1e9, and
all operations are guaranteed to be legal.

#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#define ll long long
#define ull unsigned long long
#define up_b upper_bound
#define low_b lower_bound
#define m_p make_pair
#define mem(a) memset(a,0,sizeof(a))
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#include<algorithm>
using namespace std;

inline ll read()
{
    
    
	ll x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9')	{
    
     if(ch=='-') f=-1; ch=getchar(); }
	while('0'<=ch&&ch<='9')	x=x*10+ch-'0', ch=getchar();
	return f*x;
}

const int N = 1e5+5;

int q[N],head=0,tail=-1;

//队头弹出元素,队尾压入元素

void push(int x)
{
    
    
	q[++tail]=x;
}

void pop()
{
    
    
	head++;
}

bool empty()
{
    
    
	return head>tail; // 如果head<=tail说明队列中还有元素 
}

int query() //获取队头元素 
{
    
    
	return q[head];
}

int main()
{
    
    
	int m; cin>>m;
	string op; int x;
	while(m--)
	{
    
    
		cin>>op;
		if(op=="push")
		{
    
    
			cin>>x;
			push(x);
		}
		else if(op=="pop")	pop();
		else if(op=="empty")
		{
    
    
			if(empty())	cout<<"YES"<<endl;
			else	cout<<"NO"<<endl; 
		}
		else	cout<<query()<<endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_50815157/article/details/113486084