目录
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
注意:
你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/implement-stack-using-queues


-
- #include
- #include
- #include
- #include
-
- //能用单链表解决问题就可以用单链表 -- 没必要使用双向循环链表,这属于有点小题大做了
-
- typedef int QDataType;
- typedef struct QueueNode
- {
- struct QueueNode* next;
- QDataType data;
- }QNode;
-
- //再定义一个节点来存放两个指针,方便后面操作
- typedef struct Queue
- {
- //尾指针
- QNode* tail;
- //头指针
- QNode* head;
- int size;
- }Queue;
-
- //初始化 -- 到现在有三种不用二级指针来初始化结构体了 返回值、哨兵位的头节点和这里的创立一个新的结构体放俩这种
- void QueueInit(Queue* pq);
-
- //销毁
- void QueueDestroy(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 = pq->tail = NULL;
- pq->size = 0;
- }
-
- //销毁
- void QueueDestroy(Queue* pq)
- {
- assert(pq);
- QNode* cur = pq->head;
- while (cur)
- {
- QNode* del = cur;
- cur = cur->next;
- free(del);
- }
-
- pq->head = pq->tail = NULL; //要改变什么就需要什么的指针,这里置空就可以了
- }
-
- //入队列 -- 根据队列的性质 只尾插
- void QueuePush(Queue* pq, QDataType x)
- {
- assert(pq);
- QNode* newnode = (QNode*)malloc(sizeof(QNode));
- if (newnode == NULL)
- {
- perror("malloc fail");
- exit(-1);
- }
- else
- {
- newnode->data = x;
- newnode->next = NULL; //这里置空,下面就不需要置空了
- }
-
- if (pq->tail == NULL)
- {
- pq->head = pq->tail = newnode;
- }
- else
- {
- pq->tail->next = newnode;
- pq->tail = newnode;
- }
-
- pq->size++;
- }
-
- //出队列 -- 根据队列的性质 只头删
- void QueuePop(Queue* pq)
- {
- assert(pq);
- assert(!QueueEmpty(pq));
-
- if (pq->head->next == NULL) //防止只剩下一个节点时候再删,就会导致野指针
- {
- free(pq->head);
- pq->tail = pq->head = NULL;
- }
- else
- {
- QNode* del = pq->head;
- pq->head = pq->head->next;
-
- free(del);
- del = NULL;
- }
-
- pq->size--;
- }
-
- //查看头
- QDataType QueueFront(Queue* pq)
- {
- assert(pq);
- assert(!QueueEmpty(pq));
-
- return pq->head->data;
- }
-
- //查看尾
- QDataType QueueBack(Queue* pq)
- {
- assert(pq);
- 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);
-
- //这样写就变为O(N)级别了 为此需要稍作修改
- //QNode* cur = pq->head;
- //int n = 0;
- //while (cur)
- //{
- // ++n;
- // cur = cur->next;
- //}
-
- //return n;
-
- 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)) //如果q1不为空就继续放数据
- {
- QueuePush(&obj->q1 , x);
- }
- else
- {
- QueuePush(&obj->q2 , x);
- }
-
- }
-
- int myStackPop(MyStack* obj) {
- Queue* Empty = &obj->q1;
- Queue* nonEmpty = &obj->q2;
- if(!QueueEmpty(&obj->q1)) //假设一个为空,再后面判断赋值,使得对应的指针为空
- {
- Empty = &obj->q2;
- nonEmpty = &obj->q1;
- }
-
- //把非空的队列的数据倒到空的队列中去 -- 只要倒n-1个就行了,最后一个弹出来
- while(QueueSize(nonEmpty) > 1)
- {
- QueuePush(Empty ,QueueFront(nonEmpty));
- QueuePop(nonEmpty);
- }
-
- int top = QueueFront(nonEmpty);
- QueuePop(nonEmpty); //top已经保留了数据,弹出去不影响
- return top;
- }
-
- int myStackTop(MyStack* obj) {
-
- if(!QueueEmpty(&obj->q1)) //如果队列里面不为就出返回队列尾部的数据
- {
- return QueueBack(&obj->q1);
- }
- else
- {
- return QueueBack(&obj->q2);
- }
-
-
- }
-
- bool myStackEmpty(MyStack* obj) {
- //两个为空才为空,因为这里会报持一个队列为空
- return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
- }
-
- void myStackFree(MyStack* obj) {
- //这里要单独把释放,因为它们开辟的空间仅仅释放obj是释放不掉的
- QueueDestroy(&obj->q1);
- QueueDestroy(&obj->q2);
-
- free(obj);
- }
-
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false
说明:你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
示例 1:
输入:
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
提示:
1 <= x <= 9
最多调用 100 次 push、pop、peek 和 empty
假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)
进阶:
你能否实现每个操作均摊时间复杂度为 O(1) 的队列?换句话说,执行 n 个操作的总时间复杂度为 O(n) ,即使其中一个操作可能花费较长时间。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/implement-queue-using-stacks


-
- #include
- #include
- #include
- #include
-
-
- typedef int STDataType;
- typedef struct Stack
- {
- STDataType* a;
- int top; //数据
- int capacity; //空间容量
- }ST;
-
- //初始化
- void StackInit(ST* ps);
-
- //销毁
- void StackDestroy(ST* ps);
-
- //入栈
- void StackPush(ST* ps , STDataType x); //根据栈的概念 栈只有一个地方可以插入
-
- //出栈
- void StackPop(ST* ps);
-
- //访问Top位置
- STDataType StackTop(ST* ps);
-
- //检查是否为空
- bool StackEmpty(ST* ps);
-
- //查看数据数量
- int StackSize(ST* ps); //调用函数来实现 -- 数据结构建议不要直接访问结构数据,一定要通过函数接口访问
- //解耦 -- 高内聚,低耦合
-
-
- //初始化
- void StackInit(ST* ps)
- {
- assert(ps);
- ps->a = NULL;
- ps->capacity = ps->top = 0;
- }
-
- //销毁
- void StackDestroy(ST* ps)
- {
- assert(ps);
-
- free(ps->a);
- ps->a = NULL;
- ps->capacity = ps->top = 0;
- }
-
- //入栈
- void StackPush(ST* ps, STDataType x)
- {
- assert(ps);
- //扩容
- if (ps->top == ps->capacity) //根据初始化的内容来判断
- {
- int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
- STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newCapacity);
- if (tmp == NULL)
- {
- perror("realloc fail");
- exit(-1);
- }
- ps->a = tmp;
- ps->capacity = newCapacity;
- }
-
-
- ps->a[ps->top] = x;
- ps->top++;
-
- }
-
- //出栈
- void StackPop(ST* ps)
- {
- assert(ps);
- assert(!StackEmpty(ps));
-
- --ps->top;
- }
-
- //访问Top位置
- STDataType StackTop(ST* ps)
- {
- assert(ps);
- assert(!StackEmpty(ps));
-
- return ps->a[ps->top - 1];
- }
-
- //检查是否为空
- bool StackEmpty(ST* ps)
- {
- assert(ps);
- return ps->top == 0;
- }
-
- //查看数据数量
- int StackSize(ST* ps)
- {
- assert(ps);
-
- return ps->top; //这里的top刚好是数据的下一个位置,自然就是数据个数
- }
-
-
-
- typedef struct {
- ST pushST;
- ST popST;
- } MyQueue;
-
-
- MyQueue* myQueueCreate() {
- MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
- StackInit(&obj->pushST);
- StackInit(&obj->popST);
-
- return obj;
- }
-
- void myQueuePush(MyQueue* obj, int x) {
-
- StackPush(&obj->pushST,x); //把数据放到pushST里面,专门放数据
-
- }
-
- //额外写的一个接口函数,专门用来倒数据的
- void PushTToPopST(MyQueue* obj)
- {
- if(StackEmpty(&obj->popST))
- {
- while(!StackEmpty(&obj->pushST))
- {
- StackPush(&obj->popST, StackTop(&obj->pushST));
- StackPop(&obj->pushST);
- }
-
- }
-
- }
-
- int myQueuePop(MyQueue* obj) {
- PushTToPopST(obj);
-
- int front = StackTop(&obj->popST);
- StackPop(&obj->popST);
-
- return front;
- }
-
- int myQueuePeek(MyQueue* obj) {
-
- PushTToPopST(obj);
-
- return StackTop(&obj->popST);
-
- }
-
- bool myQueueEmpty(MyQueue* obj) {
-
- return StackEmpty(&obj->pushST)
- && StackEmpty(&obj->popST);
- }
-
- void myQueueFree(MyQueue* obj) {
- StackDestroy(&obj->popST);
- StackDestroy(&obj->pushST);
-
- free(obj);
- }
设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。
循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/design-circular-queue



- typedef struct {
- int* a;
- int front; //头部
- int back; //尾部
- int N; //存储空间
- } MyCircularQueue;
-
-
- MyCircularQueue* myCircularQueueCreate(int k) {
- MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
- obj->a = (int*)malloc(sizeof(int)*(k+1)); //多开一个数据空间
- obj->front = obj->back = 0;
- obj->N = k+1; //实际空间长度
-
- return obj;
- }
-
- //这两个接口从下面移到了上面
- bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
-
- return obj->front == obj->back; //当两个相等的时候就为空,因为back指的是下一个位置
- }
-
- bool myCircularQueueIsFull(MyCircularQueue* obj) {
-
- return (obj->back+1)% obj->N == obj->front;
- }
-
- bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
- if(myCircularQueueIsFull(obj))
- return false;
-
- obj->a[obj->back] = value;
- obj->back++;
- obj->back %= obj->N; //控制back位置,到尾的时候就回到0的位置
-
- return true;
- }
-
- bool myCircularQueueDeQueue(MyCircularQueue* obj) {
-
- if(myCircularQueueIsEmpty(obj))
- return false;
-
- obj->front++;
- //控制front位置,假如到了空间尾以后,back回到0的位置
- obj->front %= obj->N;
-
- return true;
- }
-
- //返回队头的数据
- int myCircularQueueFront(MyCircularQueue* obj) {
- if(myCircularQueueIsEmpty(obj))
- return -1; //题目要求失败返回-1
- else
- return obj->a[obj->front];
-
- }
-
- //返回队尾的数据
- int myCircularQueueRear(MyCircularQueue* obj) {
- if(myCircularQueueIsEmpty(obj))
- return -1;
- else
- return obj->a[(obj->back -1 +obj->N)% obj->N] ; //返回前一个位置的数据,因为这里实现的back指向尾的后一个,但是要注意端点的情况
- }
-
-
-
- void myCircularQueueFree(MyCircularQueue* obj) {
- free(obj->a);
- free(obj);
- }
下面有一道题目是与 int myCircularQueueRear(MyCircularQueue* obj) 这个接口相关的


选择查看答案
3.D4.B5.B
世事一场大梦,人生几度秋凉?
苏轼《西江月》