线性数据结构

概要内容

链表
数组

队列
STL的应用


题目列表
P1996 约瑟夫问题循环队列模拟,STL
P1115 最大字段和即时处理
P1739 表达式括号匹配栈,STL
P1160 队列安排静态链表,双链表
P1449 后缀表达式栈,队列,STL

具体代码戳这


题解
P1160 队列安排

学生标号即可作为数组下标,可少一层循环
双链表的指针为int型,指向下一个和上一个数组的下标

#include<bits/stdc++.h>
using namespace std;
//数组实现的静态双链表
//用数组标号代表学生标号
#define MAXN 100005
typedef struct node* linklist;
struct node{
    int prior;
    int next;
};
int main()
{
    linklist s = new struct node [MAXN];
    memset(s,0,sizeof(s));
    int n, m, k, p, first;
    scanf("%d",&n);
    (s+1)->prior = 1;
    (s+1)->next = 1;
    first = 1;
    for(int i = 2; i <= n; i++)
    {
        scanf("%d%d",&k,&p);
        if(p == 1)
        {
            s[i].next = s[k].next;
            s[s[k].next].prior = i;
            s[k].next = i;
            s[i].prior = k;
        }
        else if(p == 0)
        {
            s[s[k].prior].next = i;
            s[i].prior = s[k].prior;
            s[i].next = k;
            s[k].prior = i;
            if(k == first)
                first = i;
        }
    }
    int x = 0;
    scanf("%d",&m);
    for(int i = 0; i < m; i++)
    {
        scanf("%d",&x);
        if(s[x].next && s[x].prior)  记得使用后的指针置0,且不重复删除
        {
            s[s[x].prior].next = s[x].next;
            s[s[x].next].prior = s[x].prior;
            s[x].next = 0;
            s[x].prior = 0;
        }
        if(x == first)
        {
            first = s[x].next;
        }
    }
    for(int i = first, j = 0; ;j++)
    {
        if(s[i].next != first)
            printf("%d ",i);
        else
            printf("%d\n",i);
        i = s[i].next;
        if(i == first && j != 0)
        {
            break;
        }
    }
    return 0;
}
//用STL queue会TLE
/*
int main()
{
    queue<int> a;
    queue<int> b;
    int n, k, p;
    scanf("%d",&n);
    a.push(1);
    for(int i = 2; i <= n; i++)
    {
        scanf("%d%d",&k,&p);
        while(a.front() != k)
        {
            b.push(a.front());
            a.pop();
        }
        if(p == 1)
        {
            b.push(a.front());
            a.pop();
        }
        b.push(i);
        while(!a.empty())
        {
            b.push(a.front());
            a.pop();
        }
        while(!b.empty())
        {
            a.push(b.front());
            b.pop();
        }
    }
    int m;
    scanf("%d",&m);
    int x = 0,temp = 0;
    for(int i = 0 ;i < m && scanf("%d",&x) && x != temp; i++)
    {
        temp = x;
        while(!a.empty())
        {
            if(a.front() != x)
            {
                b.push(a.front());
            }
            a.pop();
        }
        while(!b.empty())
        {
            a.push(b.front());
            b.pop();
        }
    }
    while(!a.empty())
    {
       printf("%d ",a.front());
        a.pop();
    }
    return 0;
}
*/
//普通链表TLE
/*
typedef struct node *linklist;
struct node{
    int data;
    linklist next;
};
int main()
{
    int n, k, p, m;
    scanf("%d",&n);
    linklist l = (linklist)new struct node;
    linklist pl = (linklist)new struct node;
    pl->next = l->next = NULL;
    l->next = pl;
    pl->data = 1;
    linklist pre = l;
    for(int i = 2; i <= n; i++)
    {
        linklist temp = (linklist)new struct node;
        pl = l->next;
        scanf("%d%d",&k,&p);
        while(pl)
        {
            if(pl->data == k)
            {
                linklist temp = (linklist)new struct node;
                temp->data = i;
                temp->next = NULL;
                if(p == 0)
                {
                    temp->next = pl;
                    pre->next = temp;
                }
                else if(p == 1)
                {
                    temp->next = pl->next;
                    pl->next = temp;
                }
            }
            pre = pl;
            pl = pl->next;
        }
    }
    scanf("%d",&m);
    int x;
    pre = l;
    linklist tmp = pl;
    for(int i = 0; i < m; i++)
    {
        pl = l->next;
        scanf("%d",&x);
        while(pl)
        {
            if(pl->data == x)
            {
                tmp = pl;
                pre->next = pl->next;
                delete tmp;
            }
            pre = pl;
            pl = pl->next;
        }
    }
    pl = l->next;
    while(pl)
    {
        printf("%d ",pl->data);
        pl = pl->next;
    }
    return 0;
}
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值