火车站中的驶出的火车
#include <iostream> using namespace std; template<typename DataType> struct Node { DataType data;//数据域 Node<DataType>*next;//指针域 }; template<typename DataType> class LinkStack { public: LinkStack();//构造函数,初始化一个空链栈 ~LinkStack();//析构函数,释放链栈各结点的存储空间 void Push(DataType x);//入栈操作,将元素x入栈 DataType Pop();//出栈操作,将栈顶元素出栈 DataType GetTop();//取栈顶元素(并不删除) int Empty();//判空操作,判断链栈是否为空栈 private: Node<DataType>*top;//栈顶指针即链栈的头指针 }; template<typename DataType> LinkStack<DataType>::LinkStack() { top = new Node<DataType>;//生成头结点 top->next = nullptr;//头结点的指针域置空 } template<typename DataType> LinkStack<DataType>::~LinkStack() { Node<DataType>*q = nullptr; while(top!=nullptr)//释放链栈的每一个结点的存储空间 { q = top;//暂存被释放结点 top = top->next;// top指向被释放结点的下一个结点 delete q; } } template<typename DataType> DataType LinkStack<DataType>::GetTop() { if(top==nullptr) throw "下溢异常"; else return top->data; } template<typename DataType> void LinkStack<DataType>::Push(DataType x) { Node<DataType>*s = nullptr; s = new Node<DataType>; s->data = x;//申请结点s数据域为x s->next = top; top = s;//将结点s插在栈顶 } template<typename DataType> DataType LinkStack<DataType>::Pop() { Node<DataType>*p = nullptr; DataType x; if(top==nullptr) throw "下溢"; x = top->data; p = top;//暂存栈顶元素 top = top->next;//将栈顶结点摘链 delete p; return x; } template<typename DataType> int LinkStack<DataType>::Empty() { if(top==nullptr) throw "下溢异常"; else return 0; } int main() { LinkStack<int>S{ }; int m,n; cin>>m>>n; int v; int j = 1; while(cin>>v) { if(v==1) { S.Push(j); j++; } if(v==-1) cout<<S.Pop()<<" "; } return 0; }
总时间限制:
1000ms
内存限制:
65535kB
描述
有编号从1到N的N辆火车等待进入车站,而车站的容量是有限的,需要对火车进行调度。
只有两种调度指令,进站和出站。
现在车站给出了M个指令,请你输出在完成这些调度指令后,车站中驶出火车编号。
输入
两行
第一行两个整数,N和M
第二行为M个为-1或者为1的整数,-1代表出站,1代表入站输出
一行整数,为驶出车站的火车编号
样例输入
3 3 1 -1 1样例输出
1