栈和队列都属于特殊的线性表,最多只能在两端操作。
栈是 FILO , 先进后出 ; 队列是 FIFO , 先进先出。
和 DFS 和 BFS 的关系十分类似 。
基础题目,模拟栈和队列
题目描述:
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!
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!
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.
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
1 2 2 1 1 2 None 2 3
这道题目,我一开始用的是数组模拟的栈和队列,一直 WA , 开了 1000000 的空间也不够。
所以,用链表来模拟,链表可以无限拓展。
我用一条链表来表示栈或者队列,如果是栈,入栈就插入头,出栈就删除第一个元素 ;如果是队列,入队就插入尾,出队就删除第一个元素。相同点是出栈和出队都可以通过删除第一个元素来实现。
我写的栈是倒过来看的,栈顶在链表的头。
#include <bits/stdc++.h>
using namespace std ;
enum Pattern {
FIFO , FILO // enum 链表类型
};
struct Node { // 一个节点的定义
int data ;
Node *next ;
Node ( int _data ) {
data = _data ;
next = NULL ;
}
} ;
class List {
private:
Node *head , *tail ; // 头指针,尾指针
public:
List () {
head = new Node ( -1 ) ;
tail = head ;
}
~List () { // 析构函数,释放内存
Node *target = NULL ;
while ( head ) {
target = head ;
head = head->next ; // 头结点后移
delete target ;
target = NULL ;
}
head = tail = NULL ;
}
int front () { // 返回 栈顶 或者 队首 的数据
return head->next->data ;
}
void push_back ( int data ) { // 新元素入队
Node *One = new Node ( data ) ;
tail->next = One ;
tail = One ;
}
void push_front ( int data ) { // 新元素压栈
Node *One = new Node ( data ) ;
One->next = head->next ;
head->next = One ;
}
int pop_front () { // 栈顶出栈;或者第一个元素出队
Node *target = head->next ;
head->next = target->next ;
int data = target->data ;
delete target ;
target = NULL ;
return data ;
}
bool empty () { // 判断栈为空,队列为空
return head->next == NULL ;
}
} ;
int main () {
int cases , Q , data ; // cases 组数据,Q 次操作
char how[10] , com[10] ; // how 决定是栈还是队列, com 决定是 IN 还是 OUT
scanf ( "%d" , &cases ) ;
while ( cases-- ) {
scanf ( "%d%s" , &Q , how ) ;
List One ; // 声明一个 List 对象
while ( Q-- ) {
Pattern pattern = how[2] == 'L' ? FILO : FIFO ; // 决定数据结构是栈还是队列
scanf ( "%s" , com ) ;
int is_in = com[0] == 'I' ; // 决定 IN ,OUT
if ( is_in )
scanf ( "%d" , &data ) ;
if ( is_in )
if ( pattern == FILO )
One.push_front( data ) ;
else
One.push_back( data ) ;
else
if ( One.empty() )
printf ( "None\n" ) ;
else
printf ( "%d\n" , One.front() ) , One.pop_front() ;
}
}
return 0 ;
}
直接用 STL 的 stack 和 queue 很简便。
但是使用不灵活,因为是封装好的,受到很多的限制(我个人觉得)
#include <bits/stdc++.h>
using namespace std ;
int main(){
int cases , Q , num ;
char how[10] , com[10] ;
scanf( "%d" , &cases ) ;
while( cases-- ){
stack<int> Stack ;
queue<int> Queue ;
scanf( "%d%s" , &Q , how ) ;
while( Q-- ){
int pattern = how[2] == 'L' ;
scanf( "%s" , com ) ;
int is_in = com[0] == 'I' ;
if( is_in )
scanf( "%d" , &num ) ;
if( pattern ) // 栈
if( is_in )
Stack.push( num ) ;
else
if( Stack.empty() )
printf( "None\n" ) ;
else
printf( "%d\n" , Stack.top() ) , Stack.pop() ;
else
if( is_in ) // 队列
Queue.push( num ) ;
else
if( Queue.empty() )
printf( "None\n" ) ;
else
printf( "%d\n" , Queue.front() ) , Queue.pop() ;
}
}
return 0 ;
}
我用数组会 WA , 不知道是内存不够的问题还是我模拟的问题,如有错误,敬请指出:
#include <bits/stdc++.h>
using namespace std ;
int package[1000005] , l , r ;
int main(){
int cases , Q , num ;
char how[10] , com[10] ;
scanf( "%d" , &cases ) ;
while( cases-- ){
l = r = 0 ;
scanf( "%d%s" , &Q , how ) ;
int pattern = how[2] == 'L' ;
while( Q-- ){
scanf( "%s" , com ) ;
int is_in = com[0] == 'I' ;
if( is_in )
scanf( "%d" , &num ) ,
package[++r] = num ;
else
if( pattern == 0 )
printf( l == r ? "None\n" : "%d\n" , package[l+1] ) , ++l ;
else
printf( r == 0 ? "None\n" : "%d\n" , package[r] ) , --r ;
}
}
return 0 ;
}