一、有效的括号
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef char STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
//初始化
void StackInit(ST* ps);
//销毁
void StackDestory(ST* ps);
//进栈
void StackPush(ST* ps,STDataType x);
//出栈
void StackPop(ST* ps);
//取栈顶的数据
STDataType StackTop(ST* ps);
//栈里面的数据
int StackSize(ST* ps);
//判断栈是否为空
bool StackEmpty(ST* ps);
//初始化
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
//销毁
void StackDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 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, Newcapacity * sizeof(STDataType));
if (tmp == NULL)
{
printf("realloc fail");
exit(-1);
}
ps->a = tmp;
ps->capacity = Newcapacity;
}
ps->a[ps->top] = x;
(ps->top)++;
}
//出栈
void StackPop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
ps->top--;
}
//取栈顶的数据
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top-1];
}
//栈里面的数据个数
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
//判断栈是否为空
bool StackEmpty(ST* ps)
{
//逻辑真假
return ps->top == 0;
}
bool isValid(char * s)
{
ST ps;
StackInit(&ps);
while(*s)
{
if(*s == '('
|| *s == '['
|| *s == '{')
{
StackPush(&ps,*s);
++s;
}
else
{
//遇到右括号的了,但是栈里面没有数据
//说明前面没有左括号,不匹配,返回false
if(StackEmpty(&ps))
{
//[[]]]
//内存泄漏,要释放内存
StackDestory(ps);
return false;
}
else
{
STDataType top = StackTop(&ps);
StackPop(&ps);
if((*s == '}' && top != '{')
|| (*s == ']' && top != '[')
|| (*s == ')' && top != '('))
{
StackDestory(&ps);
return false;
}
else
{
++s;
}
}
}
}
bool ret = StackEmpty(&ps);
StackDestory(&ps);
return ret;
}
二、用队列实现栈
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QueueNode;
typedef struct Queue
{
QueueNode* head;
QueueNode* tail;
}Queue;
//初始化
void QueueInit(Queue* pq);
//销毁队列,释放内存
void QueueDestory(Queue* pq);
//插入
void QueuePush(Queue* pq, QDataType x);
//删除
void QueuePop(Queue* pq);
//取队头的数据
QDataType QueueFront(Queue* pq);
//取队尾的数据
QDataType QueueBack(Queue* pq);
//查询数据个数
int QueueSize(Queue* pq);
//判断是否为空
bool QueueEmpty(Queue* pq);
//打印
void QueuePrint(Queue* pq);
//初始化
void QueueInit(Queue* pq)
{
pq->head = NULL;
pq->tail = NULL;
}
//销毁队列,释放内存
void QueueDestory(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->head;
while (cur != NULL)
{
QueueNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
}
//插入
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
newnode->data = x;
newnode->next = NULL;
if (pq->head == NULL)
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
}
//删除
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
QueueNode* next = pq->head->next;
free(pq->head);
pq->head = next;
if (pq->head == NULL)
{
pq->tail = NULL;
}
}
//取队头的数据
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;
}
//查询数据的个数
int QueueSize(Queue* pq)
{
assert(pq);
int n = 0;
QueueNode* cur = pq->head;
while (cur)
{
++n;
cur = cur->next;
}
return n;
}
//判断是否为空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}
//打印
void QueuePrint(Queue* pq)
{
QueueNode* cur = pq->head;
while (cur)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
typedef struct
{
Queue q1;
Queue q2;
}MyStack;
MyStack* myStackCreate()
{
MyStack* st = (MyStack*)malloc(sizeof(MyStack));
QueueInit(MyStack->q1);
QueueInit(Mystack->q2);
return st;
}
void myStackPush(MyStack* obj,int x)
{
if(!QueueEmpty(&obj->q1)
QueuePush(&obj->q1,x);
else
QueuePush(&obj->q2,x);
}
int myStackPop(MyStack* obj)
{
Queue* emptyQ = &obj->q1;
Queue* nonemptyQ = &obj->q2;
if(!QueueEmpty(&obj->q1))
{
emptyQ = &obj->q2;
nonemptyQ = &obj->q1;
}
while(QueueSize(nonemptyQ)>1)
{
QueuePush(emptyQ,QueueFront(nonemptyQ));
QueuePop(nonempty);
}
int top = QueueFront(nonemptyQ);
QueuePop(nonemptyQ);
return top;
}
int myStackTop(MyStack* obj)
{
if(!QueueEmpty(&obj->q1))
{
return QueueBack(&obj->q1));
}
else
{
reutnr QueueBack(&obj->q2));
}
}
bool myStackEmpty(MyStack* obj)
{
return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
}
void myStackFree(MyStack* obj)
{
QueueDestory(&obj->q1);
QueueDestory(&obj->q2);
free(obj);
}
三、用栈实现队列
typedef char STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
//初始化
void StackInit(ST* ps);
//销毁
void StackDestory(ST* ps);
//进栈
void StackPush(ST* ps,STDataType x);
//出栈
void StackPop(ST* ps);
//取栈顶的数据
STDataType StackTop(ST* ps);
//栈里面的数据
int StackSize(ST* ps);
//判断栈是否为空
bool StackEmpty(ST* ps);
//初始化
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
//销毁
void StackDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 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, Newcapacity * sizeof(STDataType));
if (tmp == NULL)
{
printf("realloc fail");
exit(-1);
}
ps->a = tmp;
ps->capacity = Newcapacity;
}
ps->a[ps->top] = x;
(ps->top)++;
}
//出栈
void StackPop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
ps->top--;
}
//取栈顶的数据
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top-1];
}
//栈里面的数据个数
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
//判断栈是否为空
bool StackEmpty(ST* ps)
{
//逻辑真假
return ps->top == 0;
}
typedef struct
{
ST pushST;
ST popST;
}MyQueue;
MyQueue* myQueueCreate()
{
//创建一个动态内存存储数据
MyQueue* q = (MyQueue*)malloc(sizeof(MyQueue));
//初始化
StackInit(&q->pushST);
StackInit(&q->popST);
return q;
}
void myQueuePush(MyQueue* obj,int x)
{
StackPush(&obj->pushST,x);
}
int myQueuePop(MyQueue* obj)
{
//如果popST中没有数据,将pushST的数据导进去
//popST中的数据先进先出的顺序
if(StackEmpty(&obj->popST)
{
while(!StackEmpty(&obj->pushST))
{
StackPush(*obj->popST,StackTop(&obj->pushST));
StackPop(&obj->pushST);
}
}
//取popST的栈上的数据
int front = StackTop(&obj->popST);
StackPop(&obj->popST);
return front;
}
int myQueuePeek(MyQueue* obj)
{
if(StackEmpty(&obj->popST))
{
while(!StackEmpty(&obj->pushST))
{
StackPush(&obj->popST,StackTop(&obj->pushST);
StackPop(&obj->pushST);
}
}
return StackTop(&obj->popST);
}
bool myQueueEmpty(MyQueue* obj)
{
return StackEmpty(&obj->pushST) && StackEmpty(&obj->popST);
}
void myQueueFree(MyQueue* obj)
{
StackDestory(&obj->pushST);
StackDestory(&obj->popST);
free(obj);
}
四、环形队列
另外扩展了解一下,实际中我们有时还会使用一种队列叫循环队列。如操作系统课程讲解生产者消费者模型
时可以就会使用循环队列。环形队列可以使用数组实现,也可以使用循环链表实现。
typedef struct
{
int* a;
int front;
int tail;
int k;
}MyCircularQueue;
bool myCircularQueueIsEmpty(MyCircularQueue* obj);
bool myCircularQueueIsFull(MyCircularQueue* obj);
MyCircularQueue* myCircularQueueCreate(int k)
{
MyCircularQueue* cq = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
cq->a = (int*)malloc(sizeof(int)*(k+1));
cq->front = cq->tail = 0;
cq->k = k;
return cq;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value)
{
//满了
if(!myCircularQueueIsFull(obj))
{
return false;
}
obj->a[obj->tail] = value;
++obj->tail;
//防止出现tail在最后一个位置的情况
obj->tail %= (obj->k+1)
retunr true;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj)
{
if(myCircularQueueIsEmpty(obj))
return false;
++obj->front;
//应对出现front在最后一个位置的情况
obj->front %= (obj->k+1);
}
int myCircularQueueFront(MyCircularQueue* obj)
{
if(myCircularQueueIsEmpty(obj))
return -1;
return obj->a[obj->front];
}
int myCircularQueueRear(MyCircularQueue* obj)
{
if(myCircularQueueIsEmpty(obj))
return -1;
//如果下标为0
if(obj->tail == 0)
{
return obj->a[obj->k];
}
else
return obj->a[obj->tail-1];
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj)
{
return obj->tail == obj->front;
}
bool myCircularQueueIsFull(MyCircularQueue* obj)
{
return (obj->tail+1) % (obj->k+1) == obj->front;
}
void myCircularQueueFree(MyCircularQueue* obj)
{
free(obj->a);
free(obj);
}