用队列实现栈,用栈实现队列,设计循环队列(C语言)

作者在学习了栈与链表有关的知识之后,做了几道与栈和队列有关的题目

225. 用队列实现栈https://leetcode.cn/problems/implement-stack-using-queues/

1、

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。

实现 MyStack 类:

  • void push(int x) 将元素 x 压入栈顶。
  • int pop() 移除并返回栈顶元素。
  • int top() 返回栈顶元素。
  • boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。

模拟入栈操作时,我们随机往某个空队列输入数据,保证另一个队列为空;当我们需要使用出栈操作时,可以发现,将非空的队列中的数据出队列到空的那一个队列,当非空队列只剩下一个数据时,那就是要出栈的数据,对其进行出栈操作,这样我么那就又得到了一个空队列与一个非空队列;返回栈定的元素与出栈操作相类似;当两队列都为空时,该栈即为空。

//自己定义的栈

//假定栈中存储的数据类型为int
typedef int STDataType;

//支持动态增长的栈
typedef struct Stack
{
	STDataType* _data;
	//栈的容量
	int _capacity;
	//栈顶的下标
	int _top;
}Stack;

void StackInit(Stack* ps)
{
    assert(ps);
	STDataType* tmp = (STDataType*)malloc(sizeof(STDataType) * 4);
	if (tmp == NULL)
	{
		perror("malloc failed");
		exit(-1);
	}
	ps->_data = tmp;
	ps->_top = 0;
	ps->_capacity = 4;
}

void StackPush(Stack* ps, STDataType x)
{
	assert(ps);
	if (ps->_top == ps->_capacity)
	{
		STDataType* tmp = (STDataType*)realloc(ps->_data, ps->_capacity*2*sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc failed");
			exit(-1);
		}
		ps->_data = tmp;
		ps->_capacity *= 2;
	}
	ps->_data[ps->_top] = x;
	ps->_top++;
}

bool StackEmpty(Stack* ps)
{
    assert(ps);
	return ps->_top == 0;
}

void StackPop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	ps->_top--; 
}

STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->_data[ps->_top-1];
}

int StackSize(Stack* ps)
{
	assert(ps);
	return ps->_top;
}



void StackDestroy(Stack* ps)
{
    assert(ps);
	free(ps->_data);
	ps->_data = NULL;
	ps->_capacity = 0;
	ps->_top = 0;
}


//用自定义的栈来实现队列
typedef struct {
    Stack pushstack;
    Stack popstack;
} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&obj->pushstack);
    StackInit(&obj->popstack);

    return obj;
}

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

int myQueuePop(MyQueue* obj) {
    int out = myQueuePeek(obj);
    StackPop(&obj->popstack);
    return out;
}

int myQueuePeek(MyQueue* obj) {
    if (!StackEmpty(&obj->popstack))
    {
        return StackTop(&obj->popstack);
    }
    while (!StackEmpty(&obj->pushstack))
    {
        StackPush(&obj->popstack, StackTop(&obj->pushstack));
        StackPop(&obj->pushstack);
    }
        return StackTop(&obj->popstack);
}

bool myQueueEmpty(MyQueue* obj) {
    return StackEmpty(&obj->pushstack) && StackEmpty(&obj->popstack);
}

void myQueueFree(MyQueue* obj) {
    StackDestroy(&obj->pushstack);
    StackDestroy(&obj->popstack);
    free(obj);
}

232. 用栈实现队列https://leetcode.cn/problems/implement-queue-using-stacks/

2、

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

实现 MyQueue 类:

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

第二个问题与第一个相类似,但是又有些不同。与之前相同在随意的一个空栈中存入数据,不同的是当需要模拟出队列的操作时,当把数据导入到空的栈时,数据自然而然的变成了,想要的出队列的形式。这就说明,这两个栈是可以分开工作的,我们将其中的一个定义为专门用来模拟入队列的操作,另一个栈popstack当其为空时从pushstack中导入数据,然后进行出队列的操作;若非空就可以直接进行出队列的操作,直至为空,再从另一个中导入数据。

 

//自己定义的队列

//数据类型
typedef int QDataType;

//队列的结点
typedef struct QueueNode
{
	QDataType _data;
	struct QueueNode* _next;
}QNode;

//队列的处于入的指针
typedef struct Queue
{
	QNode* head;
	QNode* tail;
	int size;
}Queue;

//队列初始化
void QueueInit(Queue* pq);

//队列销毁
void QueueDestory(Queue* pq);

//入队列
void QueuePush(Queue* pq, QDataType x);

//出队列
void QueuePop(Queue* pq);

//队列首元素
QDataType QueueFront(Queue* pq);

//队列尾元素
QDataType QueueBack(Queue* pq);

//队列是否为空
bool QueueEmpty(Queue* pq);

//队列元素个数
int QueueSize(Queue* pq);

//队列初始化
void QueueInit(Queue* pq)
{
	assert(pq);

	pq->head = NULL;
	pq->tail = NULL;
	pq->size = 0;
}

//队列销毁
void QueueDestory(Queue* pq)
{
	assert(pq);

	while (pq->head != NULL)
	{
		QNode* del = pq->head;
		pq->head = pq->head->_next;
		free(del);
	}

	pq->head = NULL;
	pq->tail = NULL;
	pq->size = 0;
}

//入队列
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);

	if (pq->head == NULL)
	{
		QNode* newnode = (QNode*)malloc(sizeof(QNode));
		if (newnode == NULL)
		{
			perror("malloc failed");
			exit(-1);
		}
		newnode->_data = x;
		pq->head = newnode; 
		pq->tail = newnode;
		pq->tail->_next = NULL;
	}
	else
	{
		QNode* newnode = (QNode*)malloc(sizeof(QNode));
		if (newnode == NULL)
		{
			perror("malloc failed");
			exit(-1);
		}
		newnode->_data = x;
		pq->tail->_next = newnode;
		pq->tail = pq->tail->_next;
		pq->tail->_next = NULL;
	}

	pq->size++;
}

//出队列
void QueuePop(Queue* pq)
{
	assert(pq);
	//assert(pq->head != NULL);
	assert(!QueueEmpty(pq));

	Queue* del = pq->head;
	pq->head = pq->head->_next;
	free(del);

	if (pq->head == NULL)
	{
		pq->tail = NULL;
	}

	pq->size--;
}

//队列首元素
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	//assert(pq->head != NULL);
	assert(!QueueEmpty(pq));

	return pq->head->_data;
}

//队列尾元素
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	//assert(pq->head != NULL);
	assert(!QueueEmpty(pq));

	return pq->tail->_data;
}

//队列是否为空
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	
	return pq->head == NULL && pq->tail == NULL;
}

//队列元素个数
int QueueSize(Queue* pq)
{
	assert(pq);

	return pq->size;
}


//使用自定义的队列实现栈
typedef struct {
    Queue q1;
    Queue q2;
} MyStack;


MyStack* myStackCreate() {
    MyStack* obj = (MyStack*)malloc(sizeof(MyStack));
    QueueInit(&obj->q1);
    QueueInit(&obj->q2);
    return obj;
}

void myStackPush(MyStack* obj, int x) {
    if (QueueEmpty(&(obj->q1)))
    {
        QueuePush(&(obj->q1), x);
    }
    else
    {
        QueuePush(&(obj->q2), x);
    }
}

int myStackPop(MyStack* obj) {
    int top = 0;
    Queue* EmptyQ = &(obj->q1);
    Queue* UNEmptyQ = &(obj->q2);
    if (QueueEmpty(&(obj->q2)))
    {
        EmptyQ = &(obj->q2);
        UNEmptyQ = &(obj->q1);
    }
    while (QueueSize(UNEmptyQ) > 1)
    {
        top = QueueFront(UNEmptyQ);
        QueuePop(UNEmptyQ);
        QueuePush(EmptyQ, top);
    }
    top = QueueFront(UNEmptyQ);
    QueuePop(UNEmptyQ);
    return top;
}

int myStackTop(MyStack* obj) {
    int top = 0;
    Queue* EmptyQ = &(obj->q1);
    Queue* UNEmptyQ = &(obj->q2);
    if (QueueEmpty(&(obj->q2)))
    {
        EmptyQ = &(obj->q2);
        UNEmptyQ = &(obj->q1);
    }
    while (QueueSize(UNEmptyQ) >= 1)
    {
        top = QueueFront(UNEmptyQ);
        QueuePop(UNEmptyQ);
        QueuePush(EmptyQ, top);
    }
    return top;
}

bool myStackEmpty(MyStack* obj) {
    return (QueueEmpty(&(obj->q1)) && QueueEmpty(&(obj->q2)));
}

void myStackFree(MyStack* obj) {
    QueueDestory(&(obj->q1));
    QueueDestory(&(obj->q2));
    free(obj);
}

622. 设计循环队列https://leetcode.cn/problems/design-circular-queue/

3、

设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。

循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。

你的实现应该支持如下操作:

  • MyCircularQueue(k): 构造器,设置队列长度为 k 。
  • Front: 从队首获取元素。如果队列为空,返回 -1 。
  • Rear: 获取队尾元素。如果队列为空,返回 -1 。
  • enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。
  • deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。
  • isEmpty(): 检查循环队列是否为空。
  • isFull(): 检查循环队列是否已满。

循环队列可以使用两种形式来实现,分别是链表与数组。如果使用链表存在着一些问题,在本题中使用的是数组,假设我们开的空间与数据的个数一致,这样会遇到一个问题:当循环队列满时head结点等于tail结点,这种情况会与队列为空的情况冲突,产生错误,针对这个问题有两种解决方案,1、采用size记录队列的长度;2、在创建时多创建一个空余的空间存储tail指针。使用链表还有一个问题就是如果需要访问队尾的数据非常的麻烦。因此在这里使用的数组,并且多创建了一个空余结点,同时需要记录存储空间的个数(k)。

使用数组实现循环队列时,也会遇到一些一个问题,若是在队列未满情况下,tail可以正常++,若是当循环队列数据已满,并且tail指向数组的最后一个元素时,如果此时进行出队列再入队列的操作,tail++无法正常找到循环队列的下一个存储位,这时就需要我们来进行一些处理:可以是if的判断条件,也可以是obj->tail = (obj->tail+1) % (obj->k+1),通过取余的方式将tail中的数据重新置0,回到数组的首元素,并且这个操作不会影响队列未满时正常的操作。

 头结点也会遇到这个问题,可以采取同样的处理。

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

bool myCircularQueueIsEmpty(MyCircularQueue* obj);

bool myCircularQueueIsFull(MyCircularQueue* obj);


MyCircularQueue* myCircularQueueCreate(int k) {
    MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    if (obj == NULL)
    {
        perror("malloc failed");
        exit(-1);
    }
    obj->a = (int*)malloc(sizeof(int) * (k + 1)); 
    obj->head = 0;
    obj->tail = 0;
    obj->k = k;
    return obj;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if (myCircularQueueIsFull(obj))
    {
        return false;
    }

    obj->a[obj->tail] = value;
    obj->tail = (obj->tail+1) % (obj->k+1);
    return true;

}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
    {
        return false;
    }

    obj->head = (obj->head+1) % (obj->k+1);
    return true;        
}

int myCircularQueueFront(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
    {
        return -1;
    }

    return obj->a[obj->head];
}

int myCircularQueueRear(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    
    int rear = (obj->tail-1+obj->k+1) % (obj->k+1);

    return obj->a[rear];

}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    if (obj->head == obj->tail)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
    if (((obj->tail+1) % (obj->k+1)) == obj->head)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void myCircularQueueFree(MyCircularQueue* obj) {
    free(obj->a);
    free(obj);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值