栈
只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一段称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出的原则。
队列
只允许在一端进行插入操作,而在另一端进行删除操作的线性表。
队列(Queue)是一种先进先出(First In First Out)的线性表,简称FIFO。
允许插入的一端叫做队尾,删除的一端叫做队头。
最近在刷leetcode的时候遇到了两道经典的题目 用队列实现栈 栈实现队列
上代码
typedef int STDataType;
typedef struct Stack
{
STDataType* _a;
int _top; // 栈顶
int _capacity; // 容量
}Stack;
// 初始化栈
void StackInit(Stack* ps)
{
ps->_a = NULL;
ps->_top = 0;
ps->_capacity = 0;
}
// 入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
if (ps->_top == ps->_capacity)
{
size_t newcapacity = ps->_capacity == 0 ? 4 : ps->_capacity * 2;
ps->_a = (STDataType*)realloc(ps->_a, newcapacity*sizeof(STDataType));
ps->_capacity = newcapacity;
}
ps->_a[ps->_top] = data;
ps->_top++;
}
// 出栈
void StackPop(Stack* ps)
{
assert(ps && ps->_top > 0);
--ps->_top;
}
// 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
return ps->_a[ps->_top - 1];
}
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->_top;
}
// 检测栈是否为空,如果为空返回非零(1),如果不为空返回0
bool StackEmpty(Stack* ps)
{
assert(ps);
return ps->_top == 0 ? 1 : 0;
}
// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->_a);
ps->_a = NULL;
ps->_top = ps->_capacity = 0;
}
typedef struct {
Stack st1;
Stack st2;
} MyQueue;
/** Initialize your data structure here. */
MyQueue* myQueueCreate() {
MyQueue* myQ=(MyQueue*)malloc(sizeof(MyQueue));
StackInit(&myQ->st1);
StackInit(&myQ->st2);
return myQ;
}
/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, int x) {//push数据进入队列 利用栈
if(!StackEmpty(&obj->st1))
{
StackPush(&obj->st1,x);
}
else
{
StackPush(&obj->st2,x);
}
}
/** Removes the element from in front of queue and returns that element. */
int myQueuePop(MyQueue* obj) {//删除并且出栈 并且得到它
Stack* emptySt=&obj->st1;
Stack* noemptySt=&obj->st2;
if(!StackEmpty(&obj->st1))
{
noemptySt=&obj->st1;
emptySt=&obj->st2;
}
while(!StackEmpty(noemptySt))
{
int tmp;
tmp=StackTop(noemptySt);
StackPop(noemptySt);
StackPush(emptySt,tmp);
}
int ret=StackTop(emptySt);
StackPop(emptySt);
while(!StackEmpty(emptySt))
{
int tmp;
tmp=StackTop(emptySt);
StackPop(emptySt);
StackPush(noemptySt,tmp);
}
return ret;
}
/** Get the front element. */
int myQueuePeek(MyQueue* obj) {//得到队列的头元素
Stack* emptySt=&obj->st1;
Stack* noemptySt=&obj->st2;
if(!StackEmpty(&obj->st1))
{
noemptySt=&obj->st1;
emptySt=&obj->st2;
}
while(!StackEmpty(noemptySt))
{
int tmp;
tmp=StackTop(noemptySt);
StackPop(noemptySt);
StackPush(emptySt,tmp);
}
int ret=StackTop(emptySt);
while(!StackEmpty(emptySt))
{
int tmp;
tmp=StackTop(emptySt);
StackPop(emptySt);
StackPush(noemptySt,tmp);
}
return ret;
}
/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {//判断是否为空
return StackEmpty(&obj->st1)&&StackEmpty(&obj->st2);
}
void myQueueFree(MyQueue* obj) {
StackDestroy(&obj->st1);
StackDestroy(&obj->st1);
free(obj);
}
/**
* Your MyQueue struct will be instantiated and called as such:
* MyQueue* obj = myQueueCreate();
* myQueuePush(obj, x);
* int param_2 = myQueuePop(obj);
* int param_3 = myQueuePeek(obj);
* bool param_4 = myQueueEmpty(obj);
* myQueueFree(obj);
*/
上代码
typedef int QDataType;
typedef struct QueueNode
{
QDataType _data;
struct QueueNode* _next;
}QueueNode;
typedef struct Queue
{
struct QueueNode* _front;
struct QueueNode* _back;
}Queue;
// 初始化队列
void QueueInit(Queue* q);
// 队尾入队列
void QueuePush(Queue* q, QDataType data);
// 队头出队列
void QueuePop(Queue* q);
// 获取队列头部元素
QDataType QueueFront(Queue* q);
// 获取队列队尾元素
QDataType QueueBack(Queue* q);
// 获取队列中有效元素个数
int QueueSize(Queue* q);
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
int QueueEmpty(Queue* q);
// 销毁队列
void QueueDestroy(Queue* q);
// 初始化队列
void QueueInit(Queue* q)
{
assert(q);
q->_front = q->_back = NULL;
}
// 队尾入队列
void QueuePush(Queue* q, QDataType data)
{
assert(q);
QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
newnode->_data = data;
newnode->_next = NULL;
if (q->_back == NULL)
{
q->_front = q->_back = newnode;
}
else
{
q->_back->_next = newnode;
q->_back = newnode;
}
}
// 队头出队列
void QueuePop(Queue* q)
{
assert(q);
if (q->_front->_next == NULL)
{
free(q->_front);
q->_front = q->_back = NULL;
}
else
{
QueueNode* next = q->_front->_next;
free(q->_front);
q->_front = next;
}
}
// 获取队列头部元素
QDataType QueueFront(Queue* q)
{
assert(q);
return q->_front->_data;
}
// 获取队列队尾元素
QDataType QueueBack(Queue* q)
{
assert(q);
return q->_back->_data;
}
// 获取队列中有效元素个数
int QueueSize(Queue* q)
{
int n = 0;
QueueNode* cur = q->_front;
while (cur)
{
n++;
cur = cur->_next;
}
return n;
}
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
int QueueEmpty(Queue* q)
{
return q->_front == NULL ? 1 : 0;
}
// 销毁队列
void QueueDestroy(Queue* q)
{
QueueNode* cur = q->_front;
while (cur)
{
QueueNode* next = cur->_next;
free(cur);
cur = next;
}
q->_front = q->_back = NULL;
}
//两个队列实现一个栈
typedef struct { //一个栈由两个队列组成
Queue q1;
Queue q2;
} MyStack;
/** Initialize your data structure here. */
MyStack* myStackCreate() {//栈的初始化
MyStack* st=(MyStack*)malloc(sizeof(MyStack));
QueueInit(&st->q1);
QueueInit(&st->q2);
return st;
}
/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) {//push 数据 两个队列 向不为空的队列进行输入数据
if (!QueueEmpty(&obj->q1))
{
QueuePush(&obj->q1, x);
}
else
{
QueuePush(&obj->q2, x);
}
}
/** Removes the element on top of the stack and returns that element. */
int myStackPop(MyStack* obj) {
Queue* pemptyQ = &obj->q1;
Queue* pnoemptyQ = &obj->q2;
if (!QueueEmpty(&obj->q1))
{
pemptyQ = &obj->q2;
pnoemptyQ = &obj->q1;
}//找到空的队列和非空的队列
while (QueueSize(pnoemptyQ) > 1)
{
QueuePush(pemptyQ, QueueFront(pnoemptyQ));
QueuePop(pnoemptyQ);
}
int top = QueueBack(pnoemptyQ);
QueuePop(pnoemptyQ);
return top;
}
/** Get the top element. */
int myStackTop(MyStack* obj) {
if(!QueueEmpty(&obj->q1))
{
return QueueBack(&obj->q1);
}
else
{
return QueueBack(&obj->q2);
}
}
/** Returns whether the stack is empty. */
bool myStackEmpty(MyStack* obj) {
return QueueEmpty(&obj->q1)&&QueueEmpty(&obj->q2);
}
void myStackFree(MyStack* obj) {
QueueDestroy(&obj->q1);
QueueDestroy(&obj->q2);
free(obj);
}
/**
* Your MyStack struct will be instantiated and called as such:
* MyStack* obj = myStackCreate();
* myStackPush(obj, x);
* int param_2 = myStackPop(obj);
* int param_3 = myStackTop(obj);
* bool param_4 = myStackEmpty(obj);
* myStackFree(obj);
*/
想要学好数据结构
栈和队列则是极其重要的一步!
只有打好基础才有机会成为一名合格的程序猿!