基本数据结构实现—用 栈 来实现 队列 的功能
主要思路:
用两个栈来实现,一个栈负责输入数据的管理,另一个栈负责输出数据的管理,同时两者之间需要严格的数据传输,以防输出顺序错误;
/* * @lc app=leetcode.cn id=232 lang=c * * [232] 用栈实现队列 * * https://leetcode-cn.com/problems/implement-queue-using-stacks/description/ * * algorithms * Easy (61.94%) * Likes: 119 * Dislikes: 0 * Total Accepted: 28.4K * Total Submissions: 45.8K * Testcase Example: '["MyQueue","push","push","peek","pop","empty"]\n[[],[1],[2],[],[],[]]' * * 使用栈实现队列的下列操作: * * * push(x) -- 将一个元素放入队列的尾部。 * pop() -- 从队列首部移除元素。 * peek() -- 返回队列首部的元素。 * empty() -- 返回队列是否为空。 * * * 示例: * * MyQueue queue = new MyQueue(); * * queue.push(1); * queue.push(2); * queue.peek(); // 返回 1 * queue.pop(); // 返回 1 * queue.empty(); // 返回 false * * 说明: * * * 你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty * 操作是合法的。 * 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。 * 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。 * * */ #include <iostream> using namespace std; // @lc code=start #define MAXSIZE 100 // 栈实现 typedef struct { int data[MAXSIZE]; int topIdx; } Stack; Stack *InitStack() { Stack *p = NULL; p = (Stack *)malloc(sizeof(Stack)); if (p == NULL) { return NULL; } p->topIdx = 0; return p; } void FreeStack(Stack *s) { if (s != NULL) { free(s); s = NULL; } } bool IsStackEmpty(Stack *s) { if (s->topIdx == 0) { return true; } return false; } bool IsStackFull(Stack *s) { if (s->topIdx == MAXSIZE) { return true; } return false; } bool PushStack(Stack *s, int data) { if (IsStackFull(s)) { return false; } s->data[s->topIdx++] = data; return true; } bool PopStack(Stack *s, int *data) { if (IsStackEmpty(s)) { return false; } *data = s->data[--s->topIdx]; return true; } bool TopStack(Stack *s, int *data) { if (IsStackEmpty(s)) { return false; } *data = s->data[s->topIdx - 1]; return true; } // queue typedef struct { Stack *s1; // in Stack *s2; // out } MyQueue; /** Initialize your data structure here. */ MyQueue* myQueueCreate() { MyQueue* q = NULL; q = (MyQueue*)malloc(sizeof(MyQueue)); if (q == NULL) { return NULL; } q->s1 = InitStack(); q->s2 = InitStack(); if (q->s1 == NULL || q->s2 == NULL) { FreeStack(q->s1); FreeStack(q->s2); free(q); return NULL; } return q; } void myQueueFree(MyQueue* obj) { if (obj != NULL) { FreeStack(obj->s1); FreeStack(obj->s2); free(obj); obj = NULL; } } /** Push element x to the back of queue. */ void myQueuePush(MyQueue* obj, int x) { int data; if (!PushStack(obj->s1, x)) { if (IsStackEmpty(obj->s2)) { while (PopStack(obj->s1,&data)) { PushStack(obj->s2, data); } PushStack(obj->s1, x); } } } /** Removes the element from in front of queue and returns that element. */ int myQueuePop(MyQueue* obj) { int data = 0; if (!PopStack(obj->s2, &data)) { while (PopStack(obj->s1, &data)) { PushStack(obj->s2, data); } PopStack(obj->s2, &data); } return data; } /** Get the front element. */ int myQueuePeek(MyQueue* obj) { int data = 0; if (!TopStack(obj->s2, &data)) { while (PopStack(obj->s1, &data)) { PushStack(obj->s2, data); } TopStack(obj->s2, &data); } return data; } /** Returns whether the queue is empty. */ bool myQueueEmpty(MyQueue* obj) { if (IsStackEmpty(obj->s1) && IsStackEmpty(obj->s2)) { return true; } return false; } // @lc code=end int main_LeetCode_232() { MyQueue* obj = myQueueCreate(); myQueuePush(obj, 1); myQueuePush(obj, 2); int param_3 = myQueuePeek(obj); int param_2 = myQueuePop(obj); bool param_4 = myQueueEmpty(obj); myQueueFree(obj); return 0; }
记录每天生活的点点滴滴,呵呵呵呵呵呵