【本节目标】
1.栈
2.队列
3.栈和队列面试题
1.栈
1.1栈的概念及结构
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端
称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶后进先出:Last In First Out
1.2栈的实现
栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。
// 下面是定长的静态栈的结构,实际中一般不实用,所以我们主要实现下面的支持动态增长的栈
typedef int STDataType;
#define N 10
typedef struct Stack
{
STDataType _a[N];
int _top; // 栈顶
}Stack;
// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
STDataType* _a;
int _top; // 栈顶
int _capacity; // 容量
}Stack;
// 初始化栈
void StackInit(Stack* ps);
// 入栈
void StackPush(Stack* ps, STDataType data);
// 出栈
void StackPop(Stack* ps);
// 获取栈顶元素
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);
初始化栈
//栈的初始化
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
pst->capacity = 0;
// 表示top指向栈顶元素的下一个位置
pst->top = 0;
// 表示top指向栈顶元素
//pst->top = -1;
}
入栈
// 栈顶插入
void STPush(ST* pst, STDataType x)
{
assert(pst);
if (pst->top == pst->capacity)
{
int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType) * newcapacity);
if (tmp == NULL)
{
perror("realloc fail");
return;
}
pst->a = tmp;
pst->capacity = newcapacity;
}
pst->a[pst->top] = x;
pst->top++;
}
出栈
// 栈顶删除
void STPop(ST* pst)
{
assert(pst);
// 不为空
assert(pst->top > 0);
pst->top--;
}
获取栈顶元素
//
STDataType STTop(ST* pst)
{
assert(pst);
// 不为空
assert(pst->top > 0);
return pst->a[pst->top - 1];
}
获取栈中有效元素个数
//
int STSize(ST* pst)
{
assert(pst);
return pst->top;
}
检测栈是否为空,如果为空返回非零结果,如果不为空返回0
//判断栈空
bool STEmpty(ST* pst)
{
assert(pst);
/*if (pst->top == 0)
{
return true;
}
else
{
return false;
}*/
return pst->top == 0;
}
销毁栈
//销毁栈
void STDestroy(ST* pst)
{
assert(pst);
free(pst->a);
pst->a = NULL;
pst->top = pst->capacity = 0;
}
2.队列
2.1队列的概念及结构
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出
FIFO(First In First Out) 入队列:进行插入操作的一端称为队尾 出队列:进行删除操作的一端称为队头
2.2队列的实现
// 链式结构:表示队列
typedef struct QListNode
{
struct QListNode* _pNext;
QDataType _data;
}QNode;
// 队列的结构
typedef struct Queue
{
QNode* _front;
QNode* _rear;
}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 * pq)
{
assert(pq);
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
队尾入队列
//插入
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc fail");
return;
}
newnode->val = x;
newnode->next = NULL;
if (pq->ptail == NULL)
{
pq->ptail = pq->phead = newnode;
}
else
{
pq->ptail->next = newnode;
pq->ptail = newnode;
}
pq->size++;
}
对头出队列
// 删除
void QueuePop(Queue* pq)
{
assert(pq);
//
assert(pq->phead);
QNode* del = pq->phead;
pq->phead = pq->phead->next;
free(del);
del = NULL;
if (pq->phead == NULL)
pq->ptail = NULL;
pq->size--;
}
获取队列头部元素
//对头数据
QDataType QueueFront(Queue* pq)
{
assert(pq);
//
assert(pq->phead);
return pq->phead->val;
}
获取队列尾部元素
//队尾数据
QDataType QueueBack(Queue* pq)
{
assert(pq);
//
assert(pq->ptail);
return pq->ptail->val;
}
获取队列中有效元素个数
//队列数据个数
int QueueSize(Queue* pq)
{
assert(pq);
return pq->size;
}
检测队列是否为空,如果为空返回非零结果,非空返回0
//判断是否为空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->phead == NULL;
}
销毁队列
//销毁
void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* cur = pq->phead;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
另外扩展了解一下,实际中我们有时还会使用一种队列叫循环队列。如操作系统课程讲解生产者消费者模型
时可以就会使用循环队列。环形队列可以使用数组实现,也可以使用循环链表实现
3.栈和队列面试题
1. 括号匹配问题。OJ链接
在运用了已经写好的栈导入代码之后,再写出如下代码:
bool isValid(char* s) {
ST s1;
STInit(&s1);
while(*s)
{
if(*s=='{' || *s=='(' || *s=='[')
{
STPush(&s1,*s);
}
else
{
//栈为空,即没有坐括号入栈的情况
if(STEmpty(&s1))
{
STDestroy(&s1);
return false;
}
//取左括号
char tmp = STTop(&s1);
STPop(&s1);
//左右括号匹配
if((*s=='}' && tmp!='{')
|| (*s==')' && tmp!='(')
|| (*s==']' && tmp!='['))
{
STDestroy(&s1);
return false;
}
}
s++;
}
//栈为空,返回false
bool ret = STEmpty(&s1);
STDestroy(&s1);
return ret;
}
左右括号匹配,将所有的左括号的入栈,然后将栈内的左括号与右括号进行匹配,但也要分清只有左括号和只有右括号的情况
2. 用队列实现栈。OJ链接
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);
if (!QueueEmpty(&obj->q2))
QueuePush(&obj->q2, x);
if(QueueEmpty(&obj->q1)&& QueueEmpty(&obj->q2))
QueuePush(&obj->q1, x);
}
int myStackPop(MyStack* obj) {
Queue* emptyq = &obj->q1;
Queue* nonemptyq = &obj->q2;
if (!QueueEmpty(&obj->q1))
{
emptyq = &obj->q2;
nonemptyq = &obj->q1;
}
//非空队列前N-1个元素导入空队列
while (nonemptyq->phead != nonemptyq->ptail)
{
QueuePush(emptyq, QueueFront(nonemptyq));
QueuePop(nonemptyq);
}
int top = QueueFront(nonemptyq);
QueuePop(nonemptyq);
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) {
QueueDestroy(&obj->q1);
QueueDestroy(&obj->q2);
free(obj);
}
运用自己已经写好的队列,从而引入队列,用两个队列实现栈
入栈时,则将数据进入一个为空的队列中
出栈时,则将那个有数据的队列中的N-1个数据导入另一个队列,之后再将原先那个最后一个数据出队列,则这个就是出栈的数据
为空时,两个队列都为空
3. 用栈实现队列。OJ链接
typedef struct {
ST pushst;//入数据栈
ST popst;//出数据栈
} MyQueue;
MyQueue* myQueueCreate() {
MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
STInit(&obj->pushst);
STInit(&obj->popst);
return obj;
}
void myQueuePush(MyQueue* obj, int x) {
STPush(&obj->pushst, x);
}
int myQueuePeek(MyQueue* obj) {
if(STEmpty(&obj->popst))
{
while(!STEmpty(&obj->pushst))
{
STPush(&obj->popst, STTop(&obj->pushst));
STPop(&obj->pushst);
}
}
return STTop(&obj->popst);
}
int myQueuePop(MyQueue* obj) {
int front = myQueuePeek(obj);
STPop(&obj->popst);
return front;
}
bool myQueueEmpty(MyQueue* obj) {
if (STEmpty(&obj->pushst) && STEmpty(&obj->popst))
{
return true;
}
return false;
}
void myQueueFree(MyQueue* obj) {
STDestroy(&obj->pushst);
STDestroy(&obj->popst);
free(obj);
}
运用自己已经写好的栈来解决,用两个栈来实现队列,队列是先进先出的,一个栈负责入数据,另一个栈负责出数据
入队列时,直接应用写好的入栈函数调用即可
出队列时,先看判断出数据的那个栈中是否有数据,如果有,则先出那个出数据栈的数据,如果没有,则先将入数据的栈中的数据先导入到出数据的栈中,之后再将出数据的栈中的栈顶数据用出栈函数调用输出
4. 设计循环队列。
//front == bank为空
//back的下一个是front则是满
typedef struct {
int *a;
int front;
int back;
int k;
} MyCircularQueue;
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue*obj=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
obj->a=(int*)malloc(sizeof(int)*(k+1));
obj->back=0;
obj->front=0;
obj->k=k;
return obj;
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
if(obj->front == obj->back)
{
return true;
}
return false;
}
bool myCircularQueueIsFull(MyCircularQueue* obj) {
return (obj->back+1)%(obj->k+1)==obj->front;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
if(myCircularQueueIsFull(obj))
{
return false;
}
obj->a[obj->back]=value;
obj->back++;
obj->back%=(obj->k+1);
return true;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return false;
}
obj->front++;
obj->front%=(obj->k+1);
return true;
}
int myCircularQueueFront(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return -1;
}
return obj->a[obj->front];
}
int myCircularQueueRear(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return -1;
}
return obj->a[(obj->back+obj->k)%(obj->k+1)];
}
void myCircularQueueFree(MyCircularQueue* obj) {
free(obj->a);
obj->a=NULL;
obj->back=0;
obj->front=0;
free(obj);
}
此题可用顺序表,也可以用链表,而今天用顺序表来解决
在开辟空间时,应该多开辟一个空间(k+1),用两个下标来表示出入的数据位置,back为出数据下标,front为入数据的下标
判断空:front==back
判断满:back%(k+1)==front (为防止数组越界而取模)
插入时:back++
删除时:front++
取对头数据时:a[front]
取队尾数据时:由于back指的是对位数据的下一个数据,因而防止数组越界问题,则为
a[(back+k)%(k+1)]