LeetCode-225. 用队列实现栈&232. 用栈实现队列

1.用队列实现栈

LeetCode225.用队列实现栈:

https://leetcode-cn.com/problems/implement-stack-using-queues/225.用队列实现栈
使用队列实现栈的下列操作:
push(x) – 元素 x 入栈
pop() – 移除栈顶元素
top() – 获取栈顶元素
empty() – 返回栈是否为空

1.题目分析
在这里插入图片描述

如上图所示,栈是先进后出,而队列是先进先出,故用两个队列去实现栈,一个队列用于存储数据,另一个用于存储出数据时要出数据之前的所有数据,在出掉该数据,以实现先进后出的原则。

2.C语言代码实现:

typedef struct {
    
    
    int Front;
    int Tail;
    int size;
    int *array;
} MyStack;
/** Initialize your data structure here. */
MyStack* myStackCreate() {
    
    
    MyStack*a=(MyStack*)malloc(sizeof(MyStack));
    a->array=(int*)malloc(sizeof(int)*1024);
    a->Front=0;
    a->Tail=0;
    a->size=0;
    return a;
}

/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) {
    
    
    obj->size++;
    obj->Tail++;
    obj->array[obj->Tail]=x;
}
/** Removes the element on top of the stack and returns that element. */
int myStackPop(MyStack* obj) {
    
    
    int last=obj->Tail;
    int sum=0;
    sum=obj->array[last];
    obj->Tail=last-1;
    obj->size--;
    return sum;
}
/** Get the top element. */
int myStackTop(MyStack* obj) {
    
    
    return obj->array[obj->Tail];
}
/** Returns whether the stack is empty. */
bool myStackEmpty(MyStack* obj) {
    
    
    if(obj->size==0)
    return true;
    else
    return false;
}
void myStackFree(MyStack* obj) {
    
    
    free(obj);
}

2.用栈实现队列

LeetCode232.用栈实现队列

https://leetcode-cn.com/problems/implement-queue-using-stacks/232.用栈实现队列
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列的支持的所有操作(push、pop、peek、empty):
void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false

1.题目分析:
在这里插入图片描述

如上图所示,用两个栈_pushST和_popST去实现数据的入和出,首先将数据入到_pushST中,然后将_pushST中的数据转移到_popST中,特别的是,将数据转移过去以后,在出数据时,_popST中的数据已经按照先进先出的顺序进行排列的,从栈顶出即可,直到_popST中的数据为空。

2.C语言代码实现:

//动态栈(数组)(可动态增容)
typedef int STDataType;
typedef struct Stack
{
    
    
	STDataType* a;
	int  top;//栈顶下标
	int  capacity;
}Stack;

// 初始化栈 
void StackInit(Stack* ps);
// 入栈 
void StackPush(Stack* ps, STDataType data);
// 出栈 
void StackPop(Stack* ps);
// 获取栈顶元素 
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数 
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps);
// 销毁栈 
void StackDestroy(Stack* ps);
//栈的初始化
void StackInit(Stack* ps)
{
    
    
	ps->a = malloc(sizeof(STDataType) * 4);
	ps->top = 0;
	ps->capacity = 4;
}
//入栈
void StackPush(Stack* ps, STDataType data)
{
    
    
	assert(ps);
	ps->a[ps->top] = data;
	ps->top++;
	if (ps->top == ps->capacity) {
    
    
		ps->capacity *= 2;
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * ps->capacity);
		if (tmp == NULL) {
    
    
			printf("Apply error!");
			exit(-1);
		}
		else
			ps->a = tmp;
	}
}
// 出栈 
void StackPop(Stack* ps)
{
    
    
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}
//获取栈中个数
int StackSize(Stack* ps)
{
    
    
	assert(ps);
	return ps->top;
}

// 销毁栈 
void StackDestroy(Stack* ps)
{
    
    
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps)
{
    
    
	assert(ps);
	if (ps->top == 0)
		return 1;
	else
		return 0;
}

// 获取栈顶元素 
STDataType StackTop(Stack* ps)
{
    
    
	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top - 1];
}

typedef struct {
    
    
    Stack _pushST;
    Stack _popST;
} MyQueue;

/** Initialize your data structure here. */

MyQueue* myQueueCreate() {
    
    
    MyQueue*q=(MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&q->_pushST);
    StackInit(&q->_popST);
    return q;
}

/** 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) {
    
    
    int front=myQueuePeek(obj);
    StackPop(&obj->_popST);
    return front;
}

/** Get the front element. */
int myQueuePeek(MyQueue* obj) {
    
     
    if(!StackEmpty(&obj->_popST))
    return StackTop(&obj->_popST);
    else
    {
    
    
        while(!StackEmpty(&obj->_pushST)){
    
    
            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)&&StackEmpty(&obj->_popST);
}

void myQueueFree(MyQueue* obj) {
    
    
    StackDestroy(&obj->_pushST);
    StackDestroy(&obj->_popST);
    free(obj);
}

猜你喜欢

转载自blog.csdn.net/weixin_45313447/article/details/112393038