数组实现链表,栈,队列

数组实现链表,栈,队列

#include<bits/stdc++.h>
using namespace std;
​
const int N = 2e5 + 9;
int e[N], p[N], head, tail;
​
void del(int x){
    if (e[x] == -1){
        e[p[x]] = -1;
    }
    else {
        e[p[x]] = e[x], p[e[x]] = p[x];
    }
}
void insert_front(int y, int x){
    e[x] = y, p[x] = p[y], p[e[x]] = x, e[p[x]] = x;
}
void insert_back(int y, int x){
    if (e[y] == -1){
        e[y] = x;
        p[x] = y;
        e[x] = -1;
    }
    else{
        e[x] = e[y];
        p[x] = y;
        e[y] = x;
        p[e[x]] = x;
    }
}
int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++){
        e[i] = i + 1;
        p[i] = i - 1;
    }
    e[0] = 1, e[n] = -1;
​
    head = 0, tail = n;
    e[head] = 0, tail = n;
    e[head] = 1, e[tail] = -1;
    for (int i = 1; i <= m; i++){
        int x, y, z; cin >> x >> y >> z;
        if (z)del(x), insert_front(y, x);
        else del(x), insert_back(y, x);
    }
    for (int i = e[0]; i != -1; i = e[i])cout << i << " ";
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N = 105;
char stk[N];
int top;
char s[N];
int main()
{
    int n; cin >> n;
    cin >> s + 1;
    for (int i = 1; i <= n; i++){
        if (s[i] == ')'){
            if (top&&stk[top] == ')'){
                top--;
                continue;
            }
        }
        stk[++top] = s[i];
    }
    if (top)cout << "No" << '\n';
    else cout << "Yes" << '\n';
    return 0;
}

队列

入队:q[++h]=x;

出队:hh++;

​
#include<bits/stdc++.h>
using namespace std;
​
const int N = 2e3 + 9;
int q[N], hh = 1, tt = 0;
​
int main()
{
    int m, n,ans=0; cin >> m >> n;
    for (int i = 1; i <= n; ++i){
        int x; cin >> x;
        bool tag = false;
        for (int j = hh; j <= tt; ++j)if (q[j] == x)tag = true;
        if (tag)continue;
        if (tt - hh + 1 == m)hh++;
        q[++tt] = x;
        ans++;
    }
    cout << ans << "\n";
    return 0;
}
  • 23
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值