LeetCode_225. 用佇列實現棧
阿新 • • 發佈:2019-01-24
題目
- push(x) – 元素 x 入棧
- pop() – 移除棧頂元素
- top() – 獲取棧頂元素
- empty() – 返回棧是否為空
注意:
- 你只能使用佇列的基本操作– 也就是 push to back, peek/pop from front, size, 和 is empty 這些操作是合法的。
- 你所使用的語言也許不支援佇列。 你可以使用 list 或者 deque(雙端佇列)來模擬一個佇列 , 只要是標準的佇列操作即可。
- 你可以假設所有操作都是有效的(例如, 對一個空的棧不會呼叫 pop 或者 top 操作)。
typedef struct {
} MyStack;
/** Initialize your data structure here. */
MyStack* myStackCreate(int maxSize) {
}
/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) {
}
/** Removes the element on top of the stack and returns that element. */
int myStackPop(MyStack* obj) {
}
/** Get the top element. */
int myStackTop(MyStack* obj) {
}
/** Returns whether the stack is empty. */
bool myStackEmpty(MyStack* obj) {
}
void myStackFree(MyStack* obj) {
}
/**
* Your MyStack struct will be instantiated and called as such:
* struct MyStack* obj = myStackCreate(maxSize);
* myStackPush(obj, x);
* int param_2 = myStackPop(obj);
* int param_3 = myStackTop(obj);
* bool param_4 = myStackEmpty(obj);
* myStackFree(obj);
*/
題解
思路: 佇列和棧基礎練習,用迴圈佇列模擬棧,隊尾為棧頂,入棧時直接入隊,出棧時先將隊首出隊併入隊count - 1次,將棧頂移動到隊首,然後將隊首出隊
typedef struct {
int *data;
int front, tail;
int size;
int count;
} MyQueue;
MyQueue *myQueueInit(int size) {
MyQueue *q = (MyQueue *)malloc(sizeof(MyQueue));
q->data = (int *)malloc(sizeof(int) * size);
q->front = 0;
q->tail = -1;
q->size = size;
q->count = 0;
return q;
}
bool myQueueEmpty(MyQueue *q) {
return q->count == 0;
}
void myQueuePush(MyQueue *q, int value) {
q->data[++q->tail] = value;
q->tail %= q->size;
q->count++;
}
int myQueuePop(MyQueue *q) {
q->count--;
int num = q->data[q->front++];
q->front %= q->size;
return num;
}
int myQueueFront(MyQueue * q) {
return q->data[q->front];
}
void myQueueFree(MyQueue *q) {
free(q->data);
free(q);
}
typedef struct {
MyQueue *q;
int count;
} MyStack;
/** Initialize your data structure here. */
MyStack* myStackCreate(int maxSize) {
MyStack *s = (MyStack *)malloc(sizeof(MyStack));
s->q = myQueueInit(maxSize);
s->count = 0;
return s;
}
/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) {
myQueuePush(obj->q, x);
obj->count++;
}
/** Removes the element on top of the stack and returns that element. */
int myStackPop(MyStack* obj) {
--obj->count;
int num = obj->count;
while (num--) {
myQueuePush(obj->q, myQueuePop(obj->q));
}
return myQueuePop(obj->q);
}
/** Get the top element. */
int myStackTop(MyStack* obj) {
int num = obj->count - 1;
while (num--) {
myQueuePush(obj->q, myQueuePop(obj->q));
}
int temp = myQueueFront(obj->q);
myQueuePush(obj->q, myQueuePop(obj->q));
return temp;
}
/** Returns whether the stack is empty. */
bool myStackEmpty(MyStack* obj) {
return myQueueEmpty(obj->q);
}
void myStackFree(MyStack* obj) {
myQueueFree(obj->q);
free(obj);
}
/**
* Your MyStack struct will be instantiated and called as such:
* struct MyStack* obj = myStackCreate(maxSize);
* myStackPush(obj, x);
* int param_2 = myStackPop(obj);
* int param_3 = myStackTop(obj);
* bool param_4 = myStackEmpty(obj);
* myStackFree(obj);
*/