栈和队列OJ

文章目录

  • 1.用队列实现栈
  • 2.用栈实现队列
  • 3.设计循环队列
  • 4.循环队列经典题

1.用队列实现栈

栈和队列OJ_第1张图片
栈和队列OJ_第2张图片
栈和队列OJ_第3张图片

typedef int QDataType;
typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType data;
}QNode;

typedef struct Queue
{
	QNode* head;
	QNode* tail;
}Queue;
typedef struct MyStack
{
	Queue q1;
	Queue q2;
}MyStack;

//创建
MyStack* myStackCreate()
{
	MyStack* obj = (MyStack*)malloc(sizeof(MyStack));
	QueueInit(&obj->q1);
	QueueInit(&obj->q2);
	return obj; 
}
//销毁
void myStackFree(MyStack* obj)
{
	QueueDestroy(&obj->q1);
	QueueDestroy(&obj->q2);
	free(obj);
}
//压栈
void myStackPush(MyStack* obj, int x)
{
	//q1不为空 新数据放到q1
	if (!QueueEmpty(&obj->q1))
		QueuePush(&obj->q1, x);
	//q1为空 新数据就放到q2
	else
		QueuePush(&obj->q2, x);
}
//出栈
int myStackPop(MyStack* obj)
{
	//假设q1为空 q2非空
	Queue* emptyQ = &obj->q1;
	Queue* nonEmptyQ = &obj->q2;
	//若q1不为空 交换指针指向
	//保证empty指向空 nonemptyQ指向非空
	if (!QueueEmpty(&obj->q1))
	{
		emptyQ = &obj->q2;
		nonEmptyQ = &obj->q1;
	}

	while (QueueSize(nonEmptyQ) > 1)
	{
		QueuePush(emptyQ, QueueFront(nonEmptyQ));
		QueuePop(nonEmptyQ);
	}
	int top = QueueFront(nonEmptyQ);
	QueuePop(nonEmptyQ);
	return top;
}
//取栈顶
int myStackTop(MyStack* obj) 
{
	//q1不为空 q1取队尾
	if (!QueueEmpty(&obj->q1))
	{
		return QueueBack(&obj->q1);
	}
	//q1为空 q2不为空 q2取队尾
	else
	{
		return QueueBack(&obj->q2);
	}
}
//判空
bool myStackEmpty(MyStack* obj) 
{
	return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
}

2.用栈实现队列

栈和队列OJ_第4张图片
栈和队列OJ_第5张图片

typedef int STDataType;
typedef struct Stack
{
	int top;      
	int capacity;
	STDataType* a;
}ST;
typedef struct Queue
{
	ST st_push;
	ST st_pop;
} MyQueue;
//创建
MyQueue* myQueueCreate()
{
	MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
	StackInit(&obj->st_push);
	StackInit(&obj->st_pop);
	return obj;
}
//销毁
void myQueueFree(MyQueue* obj)
{
	StackDestroy(&obj->st_push);
	StackDestroy(&obj->st_pop);
	free(obj);
}
//判空
bool myQueueEmpty(MyQueue* obj)
{
	return StackEmpty(&obj->st_pop) && StackEmpty(&obj->st_push);
}
//入队
void myQueuePush(MyQueue* obj, int x)
{
	StackPush(&obj->st_push, x);
}
//出队
int myQueuePop(MyQueue* obj) 
{
	if (StackEmpty(&obj->st_pop))
	{	
		while (!StackEmpty(&obj->st_push))
		{
			StackPush(&obj->st_pop, StackTop(&obj->st_push));
			StackPop(&obj->st_push);
		}
	}
	int front = StackTop(&obj->st_pop);
	StackPop(&obj->st_pop);
	return front;
}
//取队头
int myQueuePeek(MyQueue* obj) 
{
	if (StackEmpty(&obj->st_pop))
	{
		while (!StackEmpty(&obj->st_push))
		{
			StackPush(&obj->st_pop, StackTop(&obj->st_push));
			StackPop(&obj->st_push);
		}
	}
	return StackTop(&obj->st_pop);
}

3.设计循环队列

栈和队列OJ_第6张图片
栈和队列OJ_第7张图片

typedef struct MyCircularQueue
{
	int* a;
	int k;
	int head;
	int tail;
} MyCQ;

//创建
MyCQ* myCircularQueueCreate(int k)
{
	MyCQ* obj = (MyCQ*)malloc(sizeof(MyCQ));
	obj->a = malloc(sizeof(int) * (k + 1));
	obj->head = obj->tail = 0;
	obj->k = k;
	return obj;
}
//销毁
void myCircularQueueFree(MyCQ* obj)
{
	free(obj->a);
	free(obj);
}
//判空
bool myCircularQueueIsEmpty(MyCQ* obj)
{
	return obj->head == obj->tail;
}
//判满
bool myCircularQueueIsFull(MyCQ* obj)
{
	//一般情况tail+1 == head 就满了 
	//当tail+1越界 tail要成为0与head比较
	int t = obj->tail + 1;
	if (t == obj->k + 1)
		t = 0;
	return t == obj->head;
}
//入队
/*
* bool myCircularQueueEnQueue(MyCQ* obj, int value)
{
	if (myCircularQueueIsFull(obj))
		return false;
	obj->a[obj->tail] = value;
	obj->tail++;

	obj->tail %= obj->k + 1;

	return true;
}
*/
bool myCircularQueueEnQueue(MyCQ* obj, int value)
{
	if (myCircularQueueIsFull(obj))
		return false;
	obj->a[obj->tail] = value;
	obj->tail++;
	if (obj->tail == obj->k + 1)
		obj->tail = 0;
	return true;
}
//出队
bool myCircularQueueDeQueue(MyCQ* obj)
{
	if (myCircularQueueIsEmpty(obj))
		return false;
	++obj->head;
	//删完后head越界
	if (obj->head == obj->k + 1)
		obj->head = 0;
	return true;
}
//取头
int myCircularQueueFront(MyCQ* obj)
{
	if (myCircularQueueIsEmpty(obj))
		return -1;
	return obj->a[obj->head];
}
//取尾
/*
* int myCircularQueueRear(MyCQ* obj)
{
	if (myCircularQueueIsEmpty(obj))
		return -1;

	int index = obj->tail + obj->k;
	index %= obj->k + 1;

	//return obj-> a[(obj->tail + obj->k) % (k + 1)]

	return obj->a[index];
}
*/
int myCircularQueueRear(MyCQ* obj)
{
	if (myCircularQueueIsEmpty(obj))
		return -1;
	//一般情况下 队尾的下标是 tail-1
	int index = obj->tail - 1;
	//若tail == 0 队尾下标是k
	if (obj->tail == 0)
		index = obj->k;
	return obj->a[index];
}

4.循环队列经典题

栈和队列OJ_第8张图片
B

你可能感兴趣的:(深度学习数据结构,数据结构,算法,链表)