1.题目:
2.思路:
此题可以用两个栈实现,一个栈进行入队操作,另一个栈进行出队操作 |
出队操作: 当出队的栈不为空是,直接进行出栈操作,如果为空,需要把入队的栈元素全部导入到出队的栈,然后再进行出栈操作 |
3.我的代码:
C语言:
typedef int STDataType; //重定义数据类型
typedef struct Stack
{
STDataType* data;
int top; //栈顶
int capacity; //容量
}Stack;
extern void StackInit(Stack* ps); // 1.初始化栈
extern void StackPush(Stack* ps, STDataType x); // 2.入栈
extern void StackPop(Stack* ps); // 3.出栈
extern STDataType StackTop(Stack* ps); // 4.获取栈顶元素
extern int StackSize(Stack* ps); // 5.获取栈中有效元素个数
extern bool StackEmpty(Stack* ps); // 6.检测栈是否为空
extern void StackDestroy(Stack* ps); // 7.销毁栈
void StackInit(Stack* ps) // 1.初始化栈
{
assert(ps);
ps->data = NULL;
ps->capacity = 0;
ps->top = 0; //初始值是-1或0都可以,但是初始值不同,后期的操作不同
}
void StackPush(Stack* ps, STDataType x) // 2.入栈
{
assert(ps);
if (ps->capacity == ps->top) //扩容
{
int newcapacity = ps->capacity > 0 ? 2 * ps->capacity : 4; //考虑容量为0的情况
STDataType* tmp = (STDataType*)realloc(ps->data, sizeof(STDataType) * newcapacity);
if (tmp == NULL) //考虑扩容失败
{
perror("realloc:");
return;
}
ps->data = tmp;
ps->capacity = newcapacity;
}
ps->data[ps->top] = x;
ps->top++;
}
void StackPop(Stack* ps) // 3.出栈
{
assert(ps);
assert(!StackEmpty(ps)); //考虑栈空
ps->top--;
}
STDataType StackTop(Stack* ps) // 4.获取栈顶元素
{
assert(ps);
assert(!StackEmpty(ps)); //考虑栈空
return ps->data[ps->top - 1];
}
int StackSize(Stack* ps) // 5.获取栈中有效元素个数
{
assert(ps);
return ps->top;
}
bool StackEmpty(Stack* ps) // 6.检测栈是否为空
{
assert(ps);
return ps->top == 0;
}
void StackDestroy(Stack* ps) // 7.销毁栈
{
assert(ps);
free(ps->data);
ps->data = NULL;
ps->capacity = 0;
ps->top = 0;
}
typedef struct
{
Stack stpush; //使用两个栈模拟实现队列
Stack stpop; //一个栈入数据,另一个出数据
} MyQueue;
MyQueue* myQueueCreate()
{
MyQueue* myQueue = (MyQueue*)malloc(sizeof(MyQueue));
StackInit(&myQueue->stpush);
StackInit(&myQueue->stpop);
return myQueue;
}
void myQueuePush(MyQueue* obj, int x)
{
StackPush(&obj->stpush, x);
}
int myQueuePop(MyQueue* obj)
{
if (StackEmpty(&obj->stpop)) //判断出数据栈是否为空
{
while (!StackEmpty(&obj->stpush)) //将入数据栈的数据压入出数据栈
{
StackPush(&obj->stpop, StackTop(&obj->stpush));
StackPop(&obj->stpush);
}
}
int ret = StackTop(&obj->stpop);
StackPop(&obj->stpop); //出数据
return ret;
}
int myQueuePeek(MyQueue* obj)
{
if (StackEmpty(&obj->stpop)) //判断出数据栈是否为空
{
while (!StackEmpty(&obj->stpush)) //将入数据栈的数据压入出数据栈
{
StackPush(&obj->stpop, StackTop(&obj->stpush));
StackPop(&obj->stpush);
}
}
return StackTop(&obj->stpop);
}
bool myQueueEmpty(MyQueue* obj)
{
return StackEmpty(&obj->stpush) && StackEmpty(&obj->stpop);
}
void myQueueFree(MyQueue* obj)
{
StackDestroy(&obj->stpush);
StackDestroy(&obj->stpop);
free(obj);
}
/**
* Your MyQueue struct will be instantiated and called as such:
* MyQueue* obj = myQueueCreate();
* myQueuePush(obj, x);
* int param_2 = myQueuePop(obj);
* int param_3 = myQueuePeek(obj);
* bool param_4 = myQueueEmpty(obj);
* myQueueFree(obj);
*/
Java:
class MyQueue {
private Stack<Integer> pushStack;
private Stack<Integer> popStack;
public MyQueue() {
pushStack = new Stack<>();
popStack = new Stack<>();
}
public void push(int x) {
pushStack.push(x);
}
public int pop() {
if (empty()) {
return -1;
}
if (popStack.isEmpty()) {
int size = pushStack.size();
for (int i = 0; i < size; ++i) {
int tmp = pushStack.pop();
popStack.push(tmp);
}
}
return popStack.pop();
}
public int peek() {
if (empty()) {
return -1;
}
if (popStack.isEmpty()) {
int size = pushStack.size();
for (int i = 0; i < size; ++i) {
int tmp = pushStack.pop();
popStack.push(tmp);
}
}
return popStack.peek();
}
public boolean empty() {
return popStack.isEmpty() && pushStack.isEmpty();
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
4.答案代码:
C语言:
typedef int STDataType; //重定义数据类型
typedef struct Stack
{
STDataType* data;
int top; //栈顶
int capacity; //容量
}Stack;
extern void StackInit(Stack* ps); // 1.初始化栈
extern void StackPush(Stack* ps, STDataType x); // 2.入栈
extern void StackPop(Stack* ps); // 3.出栈
extern STDataType StackTop(Stack* ps); // 4.获取栈顶元素
extern int StackSize(Stack* ps); // 5.获取栈中有效元素个数
extern bool StackEmpty(Stack* ps); // 6.检测栈是否为空
extern void StackDestroy(Stack* ps); // 7.销毁栈
void StackInit(Stack* ps) // 1.初始化栈
{
assert(ps);
ps->data = NULL;
ps->capacity = 0;
ps->top = 0; //初始值是-1或0都可以,但是初始值不同,后期的操作不同
}
void StackPush(Stack* ps, STDataType x) // 2.入栈
{
assert(ps);
if (ps->capacity == ps->top) //扩容
{
int newcapacity = ps->capacity > 0 ? 2 * ps->capacity : 4; //考虑容量为0的情况
STDataType* tmp = (STDataType*)realloc(ps->data, sizeof(STDataType) * newcapacity);
if (tmp == NULL) //考虑扩容失败
{
perror("realloc:");
return;
}
ps->data = tmp;
ps->capacity = newcapacity;
}
ps->data[ps->top] = x;
ps->top++;
}
void StackPop(Stack* ps) // 3.出栈
{
assert(ps);
assert(!StackEmpty(ps)); //考虑栈空
ps->top--;
}
STDataType StackTop(Stack* ps) // 4.获取栈顶元素
{
assert(ps);
assert(!StackEmpty(ps)); //考虑栈空
return ps->data[ps->top - 1];
}
int StackSize(Stack* ps) // 5.获取栈中有效元素个数
{
assert(ps);
return ps->top;
}
bool StackEmpty(Stack* ps) // 6.检测栈是否为空
{
assert(ps);
return ps->top == 0;
}
void StackDestroy(Stack* ps) // 7.销毁栈
{
assert(ps);
free(ps->data);
ps->data = NULL;
ps->capacity = 0;
ps->top = 0;
}
typedef struct {
//入队栈
Stack pushST;
//出队栈
Stack popST;
} MyQueue;
/** Initialize your data structure here. */
MyQueue* myQueueCreate(int maxSize) {
MyQueue* pqueue = (MyQueue*)malloc(sizeof(MyQueue));
StackInit(&pqueue->pushST, maxSize);
StackInit(&pqueue->popST, maxSize);
return pqueue;
}
/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, int x) {
//入队栈进行入栈操作
StackPush(&obj->pushST, x);
}
/** Removes the element from in front of queue and returns that element. */
int myQueuePop(MyQueue* obj) {
//如果出队栈为空,导入入队栈的元素
if(StackEmpty(&obj->popST) == 0)
{
while(StackEmpty(&obj->pushST) != 0)
{
StackPush(&obj->popST, StackTop(&obj->pushST));
StackPop(&obj->pushST);
}
}
int front = StackTop(&obj->popST);
//出队栈进行出队操作
StackPop(&obj->popST);
return front;
}
/** Get the front element. */
int myQueuePeek(MyQueue* obj) {
//类似于出队操作
if(StackEmpty(&obj->popST) == 0)
{
while(StackEmpty(&obj->pushST) != 0)
{
StackPush(&obj->popST, StackTop(&obj->pushST));
StackPop(&obj->pushST);
}
}
return StackTop(&obj->popST);
}
/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {
return StackEmpty(&obj->pushST) == 0
&& StackEmpty(&obj->popST) == 0;
}
void myQueueFree(MyQueue* obj) {
StackDestroy(&obj->pushST);
StackDestroy(&obj->popST);
free(obj);
}
Java:
/*
解题思路:
利用两个栈模拟实现队列
入队列:s1模拟入队列,所有入队列的元素都放入到s1中
出队列:s2模拟出队列,当s2为空时,将s1中所有元素导入到s2中
s2中pop一个元素
获取队头元素:如果s2是空,将s1中所有元素导入s2中,然后peek()一个元素
*/
class MyQueue {
private Stack<Integer> s1; // 模拟入队列,所有元素都放入到s1中
private Stack<Integer> s2;
public MyQueue() {
s1 = new Stack<>();
s2 = new Stack<>();
}
// 入队类时,直接找s1
public void push(int x) {
s1.push(x);
}
// 出队列:如果s2是空的,需要将s1中的所有元素导入到s2中
// 此时s1先压入的元素就后进入到s2中,出的时候就先出了
public int pop() {
if(s2.empty()){
while(!s1.empty()){
s2.push(s1.pop());
}
}
return s2.pop();
}
public int peek() {
if(s2.empty()){
while(!s1.empty()){
s2.push(s1.pop());
}
}
return s2.peek();
}
public boolean empty() {
return s1.empty() && s2.empty();
}
}