[C/C++]数据结构 LeetCode:用栈实现队列

题目描述:

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(pushpoppeekempty):

实现 MyQueue 类:

  • void push(int x) 将元素 x 推到队列的末尾
  • int pop() 从队列的开头移除并返回元素
  • int peek() 返回队列开头的元素
  • boolean empty() 如果队列为空,返回 true ;否则,返回 false

说明:

  • 你 只能 使用标准的栈操作 —— 也就是只有 push to toppeek/pop from topsize, 和 is empty 操作是合法的。
  • 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。

题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

功能思路: 

在实现本题前,需要先构建好栈的基本功能,可以参考:http://t.csdnimg.cn/Kha16

1.出队列 和 入队列

现在我们有两个栈,假设在第一个栈中依次入栈1 2 3 4 5,另一个为空栈

现在要出队列的话,应该把1出队,所以需要把除了栈底元素的其他元素出栈并且入到第二个栈里,这时就可以将1出队,但是将数据导入另一个栈时,数据会倒过来

如果要再次出队列的话,应该把2出队,就不需要把数据导一次了,直接栈顶出栈即可.

若此时再入队6 7的话,可以直接把6 7入到第一个栈中,因为出栈可以直接再第二个栈中操作

所以我们可以把将一个栈专门用来入数据,另一个栈专门用来出数据,每次出队之前先判断出数据的栈是否为空,如果出数据那个栈不为空的话,直接出数据即可,否则就将入数据的栈中的数据导入出数据的栈中,再出栈.

2.返回队列开头元素

如果出数据的栈不为空,栈顶元素即为队列开头元素,否则就需要将入数据的栈的元素导入出数据的栈中,在返回栈顶元素

3.判空

两个栈同时为空,说明队列为空

参考代码:


typedef int STDataType;
typedef struct stack
{
	int* a;
	int top;
	int capacity;
}ST;

void STInit(ST* ps)
{
	assert(ps);

	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;  //top初始化位0,top的值可以表示栈元素的个数
}

void STPush(ST* ps, STDataType x)
{
	assert(ps);
	//扩容
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* ret = (STDataType*)realloc(ps->a,sizeof(STDataType)*newcapacity);
		if (ret == NULL)
		{
			perror("realloc");
			return;
		}
		ps->a = ret;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

void STPop(ST* ps)
{
	assert(ps);
	assert(ps->top);

	ps->top--;
}

STDataType STTop(ST* ps)
{
	assert(ps);
	assert(ps->top);

	return ps->a[ps->top - 1];
}

bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

void STDestroy(ST* ps)
{
	assert(ps);

	free(ps->a);
	ps->a == NULL;
	ps->top = ps->capacity = 0;
}

typedef struct {
    ST Queuepush;
    ST Queuepop;
} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue *obj=(MyQueue*)malloc(sizeof(MyQueue));
    STInit(&obj->Queuepush);
    STInit(&obj->Queuepop);
    return obj;
}

void myQueuePush(MyQueue* obj, int x) {
    STPush(&obj->Queuepush,x);
}

int myQueuePeek(MyQueue* obj) {
    if(STEmpty(&obj->Queuepop))
    {
        while(!STEmpty(&obj->Queuepush))
        {
            STPush(&obj->Queuepop,STTop(&obj->Queuepush));
            STPop(&obj->Queuepush);
        }
    }
    return STTop(&obj->Queuepop);
}

int myQueuePop(MyQueue* obj) {
    int front = myQueuePeek(obj);
    STPop(&obj->Queuepop);
    return front;
}

bool myQueueEmpty(MyQueue* obj) {
    return STEmpty(&obj->Queuepop)&&STEmpty(&obj->Queuepush);
}

void myQueueFree(MyQueue* obj) {
    STDestroy(&obj->Queuepop);
    STDestroy(&obj->Queuepush);
    free(obj);
}

猜你喜欢

转载自blog.csdn.net/m0_74910646/article/details/134495056