hdoj1702//栈和队列(头文件)

本文记录了一位程序员在解决ACboy被怪物困住的迷宫问题时,遇到的关于FIFO(先进先出)和FILO(先进后出)数据结构的题目。在初次尝试使用自定义栈和队列结构解决问题时,由于初始化错误导致WA,最终通过学习C++内置的栈和队列库函数解决了问题。作者分享了从错误中学习的经验,提醒读者注意初始化细节,并提供了简化后的代码实现。
摘要由CSDN通过智能技术生成

ACboy needs your help again!

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 21702    Accepted Submission(s): 10654


 

Problem Description
ACboy was kidnapped!!
he miss his mother very much and is very scare now.You can't image how dark the room he was put into is, so poor :(.
As a smart ACMer, you want to get ACboy out of the monster's labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can't solve my problems, you will die with ACboy."
The problems of the monster is shown on the wall:
Each problem's first line is a integer N(the number of commands), and a word "FIFO" or "FILO".(you are very happy because you know "FIFO" stands for "First In First Out", and "FILO" means "First In Last Out").
and the following N lines, each line is "IN M" or "OUT", (M represent a integer).
and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!
 

Input
The input contains multiple test cases.
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.
 

Output
For each command "OUT", you should output a integer depend on the word is "FIFO" or "FILO", or a word "None" if you don't have any integer.
 

Sample Input
4
4 FIFO
IN 1
IN 2
OUT
OUT
4 FILO
IN 1
IN 2
OUT
OUT
5 FIFO
IN 1
IN 2
OUT
OUT
OUT
5 FILO
IN 1
IN 2
OUT
IN 3
OUT
 

Sample Output
1
2
2
1
1
2
None
2
3

 这题..我好不容易写了一堆(六个?)函数 初始化栈出栈入栈 初始化队列出队入队 写出来之后一直wa 哭死 一直找不到错误 最后发现原来是初始化的问题

  !!!这题一定要注意初始化呀<敲黑板好叭>

还是就我一个人这么蠢吗

然后我又悲伤地发现原来c++自己是有关于栈和队列的库函数的

放下原来的写了六个函数的代码叭!(虽然应该没啥用没啥借鉴价值 但是!我写了这么久的不能就直接删掉呀wwww)

#include<iostream>
using namespace std;
typedef struct
{
    int *base;
    int *top;
}SqStack;

void Initstack(SqStack &S)//初始化栈
{//初始化
    S.base=new int[10000];
    S.top=S.base;
}

void Pushstack(SqStack &S,int e)//压栈
{
    *S.top=e;
    S.top++;
}

int Popstack(SqStack &S,int &e)//出栈
{
    if(S.top==S.base)
        return 0;
    e=*--S.top;
    return 1;
}
typedef struct queuestack
{
    int data;
    struct queuestack *next;
}Qnode,*Qnodeptr;

typedef struct queue
{
    Qnodeptr front;
    Qnodeptr rear;
}Queue;

void Initqueue (Queue &Q)//初始化队列
{
    Q.front=Q.rear=new Qnode;
    Q.rear->next=NULL;
}

void Enqueue(Queue &Q,int e)//入队
{
    Qnodeptr q=new Qnode;
    q->data=e;
    q->next=NULL;
    Q.rear->next=q;
    Q.rear=q;
}

int Dequeue(Queue &Q,int &e)//出队
{
    if(Q.rear==Q.front)
        return 0;
    Qnodeptr q=new Qnode;
    q=Q.front->next;
    e=q->data;
    Q.front->next=q->next;
    if(Q.rear==q)
        Q.rear=Q.front;
    delete q;
    return 1;
}

int main()
{
    int M,N,e,n;
    char a[4];
    char b[5];
    SqStack L;
    Queue Q;
    cin>>M;
    while(M--)
    {
        scanf("%d%s",&N,a);
        if(a[2]=='F')
        {
            Initqueue(Q);
            while(N--)
            {
                scanf("%s",b);
                if(b[0]=='I')
                {
                    cin>>n;
                    Enqueue(Q,n);
                }
                else if(b[0]=='O')
                {
                    if(Dequeue(Q,e)==1)
                        cout<<e<<endl;
                    else
                        cout<<"None"<<endl;
                }
            }
        }
        else if(a[2]=='L')
        {
            Initstack(L);
            while(N--)
            {
                scanf("%s",b);
                if(b[0]=='I')
                {
                    cin>>n;
                    Pushstack(L,n);
                }
                else if(b[0]=='O')
                {
                    if(Popstack(L,e)==1)
                        cout<<e<<endl;
                    else
                        cout<<"None"<<endl;
                }
            }
        }
    }
    return 0;
}

后来去学习了下栈和队列的头文件..

STACK 栈

1.使用stack函数必须要加上<stack>头文件。
2.定义stack 对象的示例代码如下:
stack<int> s1;
stack<string> s2;
3.stack 的基本操作有:
入栈,如例:s.push(x)。
出栈,如例:s.pop()。注意!出栈操作只是删除栈顶元素,并不返回该元素
访问栈顶,如例:s.top()
判断栈空,如例:s.empty(),当栈空时,返回true。
访问栈中的元素个数,如例:s.size()。

QUEUE 队列 

1.使用queue函数必须要加上<queue>头文件。
2.定义queue对象的示例代码如下:
queue<int> q1;
queue<string> q2;
3.queue 的基本操作有:
入队,如例:q.push(x); 将x 接到队列的末端。
出队,如例:q.pop(); 弹出队列的第一个元素。注意!并不会返回被弹出元素的值。
访问队首元素,如例:q.front(),即最早被压入队列的元素。
访问队尾元素,如例:q.back(),即最后被压入队列的元素。
判断队列空,如例:q.empty(),当队列空时,返回true。
访问队列中的元素个数,如例:q.size()

pair


下面是在洛谷题解上看到的一个东西

queue<pair<int,int> > q;//定义

q.push(make_pair(x,y));//入队
//取队首
xx=q.front().first;//第一个值
yy=q.front().second;//第二个值

q.pop();//出队

这里的定义 > >中间必须要加一个空格 我在自己编译器上没有问题 但是在洛谷上交会编译错误 要注意呀!

以后就不用傻傻地写两个结构体六个函数啦! 

放下我最后的代码叭!

#include<iostream>
#include<stack>
#include<queue>
int main()
{
    using namespace std;
    int M,N,e,n;
    char a[4];
    char b[5];
    cin>>M;
    while(M--)
    {
        queue<int>Q;
        scanf("%d%s",&N,a);
        if(a[2]=='F')
        {
            while(N--)
            {
                scanf("%s",b);
                if(b[0]=='I')
                {
                    cin>>n;
                    Q.push(n);
                }
                else if(b[0]=='O')
                {
                    if(Q.empty()!=true)
                    {
                        cout<<Q.front()<<endl;
                        Q.pop();
                    }

                    else
                        cout<<"None"<<endl;
                }
            }
        }
        else if(a[2]=='L')
        {
            stack<int>S;
            while(N--)
            {
                scanf("%s",b);
                if(b[0]=='I')
                {
                    cin>>n;
                    S.push(n);
                }
                else if(b[0]=='O')
                {
                    if(S.empty()!=true)
                    {
                        cout<<S.top()<<endl;
                        S.pop();
                    }
                    else
                        cout<<"None"<<endl;
                }
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值