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

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
    评论
C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。在编写C程序时,需要注意变量的声明和定义、指针的使用、内存的分配与释放等问题。C语言中常用的数据结构包括: 1. 数组:一种存储同类型数据的结构,可以进行索引访问和修改。 2. 链表:一种存储不同类型数据的结构,每个节点包含数据和指向下一个节点的指针。 3. 栈:一种后进先出(LIFO)的数据结构,可以通过压入(push)和弹出(pop)操作进行数据的存储和取出。 4. 队列:一种先进先出(FIFO)的数据结构,可以通过入队(enqueue)和出队(dequeue)操作进行数据的存储和取出。 5. 树:一种存储具有父子关系的数据结构,可以通过中序遍历、前序遍历和后序遍历等方式进行数据的访问和修改。 6. 图:一种存储具有节点和边关系的数据结构,可以通过广度优先搜索、深度优先搜索等方式进行数据的访问和修改。 这些数据结构在C语言中都有相应的实现方式,可以应用于各种不同的场景。C语言中的各种数据结构都有其优缺点,下面列举一些常见的数据结构的优缺点: 数组: 优点:访问和修改元素的速度非常快,适用于需要频繁读取和修改数据的场合。 缺点:数组的长度是固定的,不适合存储大小不固定的动态数据,另外数组在内存中是连续分配的,当数组较大时可能会导致内存碎片化。 链表: 优点:可以方便地插入和删除元素,适用于需要频繁插入和删除数据的场合。 缺点:访问和修改元素的速度相对较慢,因为需要遍历链表找到指定的节点。 栈: 优点:后进先出(LIFO)的特性使得栈在处理递归和括号匹配等问题时非常方便。 缺点:栈的空间有限,当数据量较大时可能会导致栈溢出。 队列: 优点:先进先出(FIFO)的特性使得

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值