用2个栈实现队列

文章目录

题目

力扣链接

image-20211117212837493

思路

image-20211117222645256

代码

//注意点
//后面定义的函数某些都需要empty,因此定义empty时,一定要放到前。
//经过观察发现,2个栈是的功能是横定的,一个pushST,一个popST。
//当popST中为空后才将pushST中的元素pop到popST
typedef int SDateType;
typedef struct//对  匿名结构体  重命名。
              //匿名是一种特殊的结构体定义形式,
	          //但是最后不要定义2种同内容匿名结构体,因为编译器会把他们当成2种不同结构体类型 
{
    
    
	SDateType* arr;
	size_t sz;
	size_t capacity;
}stack;

void StackInit(stack* s)//初始化 
{
    
    

	assert(s);
	s->arr = NULL;
	s->sz = 0;
	s->capacity = 0;
}
bool StackEmpty(stack* s)//判断是否为空,因为sz的特殊性,当sz为0,代表栈空
{
    
    
	assert(s);
	return s->sz == 0;
}
void StackDestroy(stack* s)//销毁
{
    
    
	assert(s);
	free(s->arr);
	s->arr = NULL;
}
SDateType StackTop(stack* s)
{
    
    
	assert(s);
	assert(!StackEmpty(s));
	return s->arr[s->sz - 1];
}
void StackPush(stack* s, SDateType x)//入栈
//注意sz始终指向入完元素后的下一个位置
{
    
    

	assert(s);
	if (s->sz == s->capacity)//当前栈空间满了
	{
    
    
		s->capacity = s->capacity == 0 ? 4 : 2 * s->capacity;
		SDateType *a = (SDateType *)realloc(s->arr,sizeof(SDateType) * (s->capacity));
		//之所以通过中间变量进行重新分配。
		// 1.realocc返回NULL有2种:
		//   1. 开辟的空间为0。原始内存块,会被内部free。
		//   2. 堆满,返回NULL,,原始内存块,不会被free.
		     //如果这时直接赋值给arr,会导致内存泄露
		if (a == NULL)
		{
    
    
			exit(-1);
		}
		s->arr = a;
	}
	s->arr[s->sz] = x;
	s->sz++;
}

void StackPop(stack* s)//出栈
//只需要sz--就行
//1.数据可以被覆盖
//2.动态开辟的连续内存不允许在中间任何地方free
{
    
    
	assert(s);
	assert(!StackEmpty(s));//当sz为0,栈为空,就断言警告。
	s->sz--;
}
typedef struct {
    
    
stack pushST;
stack popST;

} MyQueue;


MyQueue* myQueueCreate() {
    
    

MyQueue* myqueue =(MyQueue*)malloc(sizeof(MyQueue));
if(myqueue==NULL)
{
    
    
perror("myqueue");
exit(-1);
}
StackInit(&(myqueue->pushST));
StackInit(&(myqueue->popST));
return myqueue; 
}
bool myQueueEmpty(MyQueue* obj) {
    
    
assert(obj);
return  StackEmpty(&(obj->pushST))&&StackEmpty(&(obj->popST));

}
void myQueuePush(MyQueue* obj, int x) {
    
    
assert(obj);
StackPush(&(obj->pushST),x);
}

int myQueuePop(MyQueue* obj) {
    
    
assert(obj);
assert(!myQueueEmpty(obj));
int ret=0;

if(!StackEmpty(&(obj->popST)))
{
    
    
ret=StackTop(&(obj->popST));
StackPop(&(obj->popST));
}
else
{
    
    
    
while(!StackEmpty(&(obj->pushST)))
{
    
    
StackPush(&(obj->popST),StackTop(&(obj->pushST)));
StackPop(&(obj->pushST));
}
ret=StackTop(&(obj->popST));
StackPop(&(obj->popST));
}
return ret;
}

int myQueuePeek(MyQueue* obj) {
    
    
assert(obj);
assert(!myQueueEmpty(obj));
int ret=0;

if(!StackEmpty(&(obj->popST)))
{
    
    

ret= StackTop(&(obj->popST));
}else
{
    
    
  while(!StackEmpty(&(obj->pushST)))
{
    
    
StackPush(&(obj->popST),StackTop(&(obj->pushST)));
StackPop(&(obj->pushST));
}
ret=StackTop(&(obj->popST));
}
return ret;
}

void myQueueFree(MyQueue* obj) {
    
    
assert(obj);
StackDestroy(&(obj->pushST));
StackDestroy(&(obj->popST));
}

/**
 * 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);
*/

猜你喜欢

转载自blog.csdn.net/qq_55439426/article/details/121389620