用两个栈来实现一个队列,使用n个元素来完成 n 次在队列尾部插入整数(push)和n次在队列头部删除整数(pop)的功能。 队列中的元素为int类型。保证操作合法,即保证pop操作时队列内已有元素。
数据范围:
要求:存储n个元素的空间复杂度为
,插入与删除的时间复杂度都是
["PSH1","PSH2","POP","POP"]
1,2
"PSH1":代表将1插入队列尾部 "PSH2":代表将2插入队列尾部 "POP“:代表删除一个元素,先进先出=>返回1 "POP“:代表删除一个元素,先进先出=>返回2
["PSH2","POP","PSH1","POP"]
2,1
static int stk1[1000],stk2[1000];
static int top1=-1,top2=-1;
void push(int node ) {
if(top1==999){
return;
}
stk1[++top1]=node;
}
int pop() {
while(top2>-1){
return stk2[--top2];
}
while(top1>-1){
stk2[top2++]=stk1[top1--];
}
return stk2[--top2];
} #include <stdbool.h>
#include <stdio.h>
int queue[1000]={0}, *topQueue=NULL, *bottomQueue=NULL;
int top() {
if(topQueue==NULL)
return 0;
return *topQueue;
}
int bottom() {
if(bottomQueue==NULL)
return 0;
return *bottomQueue;
}
int len() {
if(bottomQueue==NULL)
return 0;
if(topQueue <= bottomQueue)
return bottomQueue-topQueue+1;
else
return sizeof(queue)/sizeof(int)-(topQueue-bottomQueue)+1;
}
void push(int node ) {
if(bottomQueue==NULL) {
bottomQueue = queue;
topQueue = queue;
}
else {
if(topQueue-bottomQueue==1 || (topQueue==queue && bottomQueue==(queue+sizeof(queue)/sizeof(int)-1)))
return;
if(bottomQueue == queue+sizeof(queue)/sizeof(int)-1)
bottomQueue = queue;
else
bottomQueue++;
}
*bottomQueue = node;
}
int pop() {
int node=0;
if(topQueue == NULL)
return 0;
node = *topQueue;
*topQueue = 0;
if(bottomQueue == topQueue) {
bottomQueue = NULL;
topQueue = NULL;
}
else {
if(topQueue == queue+sizeof(queue)/sizeof(int)-1)
topQueue = queue;
else
topQueue++;
}
return node;
} int stack1[1000];
int stack2[1000];
int top1=0;
int top2=0;
void push(int node ) {
// write code here
stack1[top1++]=node;
}
int pop() {
return stack1[top2++];
}
int stack1[1000];
int stack2[1000];
int top1=0;
int top2=0;
void push(int node ) {
// write code here
if(top1>=1000)
{
return;
}
stack1[top1++]=node;
}
int pop() {
if(top2>=1000)
{
return -1;
}
if(top2==0)
{
while(top1>0)
{
stack2[top2++]=stack1[--top1];
}
}
printf("t2[%d]=%d\n",top2-1,stack2[top2-1]);
return stack2[--top2];
} static int stack[1000];
int end = 0;
int head = 0;
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param node int整型
* @return 无
*/
void push(int node ) {
// write code here
if(end > 1023)
{
end = 0;
}
stack[end++] = node;
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param 无
* @return int整型
*/
int pop() {
// write code here
if(head > 1023)
{
head = 0;
}
return stack[head++];
} #include<stdio.h>
/*
1.压栈放到stk1中
2.出栈时,如果stk2不为空,则出stk2;否则先将stk1中的元素依次弹出放入stk2,再出stk2
*/
static int stk1[1000],stk2[1000];
static int top1=0,top2=0;
void push(int node ) {
if(top1==1000)//栈满
return;
stk1[top1++]=node;
}
int pop() {
int x;
if(top2!=0){//stk2中还有,则从stk2出,否则stk1先放到stk2中,再stk2出
x=stk2[--top2];
return x;
}
while(top1>0){
stk2[top2++]=stk1[--top1];
}
x=stk2[--top2];
return x;
} 单纯为了过这道题而,不考虑pop之后的空间浪费,设置头尾指针即可
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param node int整型
* @return 无
*/
static int list[1000] ={0};
static int head=1000-1, tail=1000-1;
void push(int node ) {
// write code here
if(tail>=0) {
list[tail] = node;
tail--;
}
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param 无
* @return int整型
*/
int pop() {
// write code here
if(head>=0 && head>=tail) {
head--;
return list[head+1];
}
else return -1;
}
int arr1[1000] = {0}; // 0 为 底,
int arr2[1000] = {0}; // 0 为 底。
int index1 = 0;
int index2 = -1;
void push(int node ) {
// write code here
arr1[index1++] = node;
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param 无
* @return int整型
*/
int pop() {
// write code here
if (index2 == -1)
{
do
{
arr2[++index2] = arr1[--index1];
}
while(index1>0);
}
return arr2[index2--];
} /**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param node int整型
* @return 无
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
static int instack[1000];
static int outstack[1000];
static int intop = 0;
static int outtop = 0;
void push(int node ) {
// write code here
*(instack + (intop ++)) = node;
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param 无
* @return int整型
*/
int pop() {
// write code here
if(outtop == 0)
{
while(intop)
{
*(outstack + (outtop ++)) = *(instack + (--intop));
}
}
return *(outstack + (--outtop));
} /**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param node int整型
* @return 无
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
int s1[1000];
int s2[1000];
int k=0;
void push(int node) {
// write code here
s1[k++]=node;
}
//k=2;
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param 无
* @return int整型
*/
int j=0;
int pop() {
// write code here
if(j==0){
k--;
for(;k>=0;k--,j++)
s2[j]=s1[k];
k++;
}
j--;
return s2[j];
}
class Solution { public: void push(int node) { stack1.push(node); }int pop() { int a; if(stack2.empty()){ while(!stack1.empty()){ a=stack1.top(); stack2.push(a); stack1.pop(); } } a=stack2.top(); stack2.pop(); return a; }private: stack<int> stack1; stack<int> stack2; };用两个栈实现一个队列的功能?要求给出算法和思路!
<分析>:
入队:将元素进栈A
出队:判断栈B是否为空,如果为空,则将栈A中所有元素pop,并push进栈B,栈B出栈;
如果不为空,栈B直接出栈。
用两个队列实现一个栈的功能?要求给出算法和思路!
<分析>:
入栈:将元素进队列A
出栈:判断队列A中元素的个数是否为1,如果等于1,则出队列,否则将队列A中的元素 以此出队列并放入队列B,直到队列A中的元素留下一个,然后队列A出队列,再把 队列B中的元素出队列以此放入队列A中。