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

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()

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

放下我最后的代码叭!

#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;
}

 

上一篇:将两个多项式相加


下一篇:刷题-力扣-509