因为C语言没有栈和队列的库,所以以下例题栈和队列均要手搓
![在这里插入图片描述](https://img-blog.csdnimg.cn/75ada8a4314b42b683d24371f7b03153.png)
1. 括号匹配问题
括号匹配问题
整体思路:如果为左括号就入栈,右括号就拿栈顶的元素进行匹配,全部匹配成功返回true,否则返回false
typedef char STDatatype;
typedef struct stack
{
STDatatype* a;
int top;
int capacity;
}ST;
//初始化
void STInit(ST* ps);
//销毁
void STDestroy(ST* ps);
//插入
void STPush(ST* ps,STDatatype x);
//删除
void STPop(ST* ps);
//栈的大小
int STSize(ST* ps);
//判空
bool STEmpty(ST* ps);
//返回栈顶元素
STDatatype STTop(ST* ps);
//初始化
void STInit(ST* ps)
{
assert(ps);
ps->a = (STDatatype*)malloc(sizeof(STDatatype) * 4);
if (ps->a == NULL)
{
perror("STInit::malloc");
exit(-1);
}
ps->capacity = 4;
ps->top = 0;//top是栈顶元素的下一个位置
//ps->top=-1;//top是栈顶元素的位置
}
//销毁
void STDestroy(ST* ps)
{
assert(ps);
free(ps->a);
//ps->a = NULL;
ps->capacity = ps->top = 0;
}
//插入
void STPush(ST* ps, STDatatype x)
{
assert(ps);
//满了扩容
if (ps->top == ps->capacity)
{
STDatatype* tmp = (STDatatype*)realloc(ps->a, sizeof(STDatatype) * ps->capacity * 2);
if (tmp == NULL)
{
perror("STPush::realloc");
exit(-1);
}
ps->a = tmp;
ps->capacity *= 2;
}
ps->a[ps->top] = x;
ps->top++;
}
//删除
void STPop(ST* ps)
{
assert(ps);
assert(!STEmpty(ps));
ps->top--;
}
//栈的大小
int STSize(ST* ps)
{
assert(ps);
return ps->top;
}
//判空
bool STEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
//返回栈顶元素
STDatatype STTop(ST* ps)
{
assert(ps);
assert(!STEmpty(ps));
return ps->a[ps->top - 1];
}
bool isValid(char * s)
{
ST st;
//字符串单数必定不匹配,所以直接返回false
int n = strlen(s);
if (n % 2 == 1)
{
return false;
}
STInit(&st);
while(*s)
{
//如果是左括号就入栈
if(*s=='('||*s=='{'||*s=='[')
{
STPush(&st,*s);
}
else//出栈顶的元素进行比较
{
//栈不为空就比较
if(!STEmpty(&st))
{
//栈顶元素不匹配返回false
if(*s==')' && STTop(&st)!='('
||*s=='}' && STTop(&st)!='{'
||*s==']' && STTop(&st)!='[')
{
STDestroy(&st);
return false;
}
//匹配栈顶元素删除
else
{
STPop(&st);
}
}
//栈为空就结束
else
{
STDestroy(&st);
return false;
}
}
s++;
}
//循环结束后栈不为空则代表未全部匹配成功,返回false
//为空返回true
if(!STEmpty(&st))
{
STDestroy(&st);
return false;
}
STDestroy(&st);
return true;
}
2. 用队列实现栈
用队列实现栈
整体思想:
1.保持一个队列为空,一个队列存数据
2.出栈,把前面的数据倒入空队列(保留最后一个数据在原队列)
typedef int QDatatype;
typedef struct QNode
{
struct QNode* next;
QDatatype data;
}QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
int size;
}Queue;
//队列的初始化
void QueueInit(Queue* pq);
//队列的销毁
void QueueDestroy(Queue* pq);
//插入
void QueuePush(Queue* pq, QDatatype x);
//删除
void QueuePop(Queue* pq);
//返回队列当前元素个数
int QueueSize(Queue* pq);
//判空
bool QueueEmpty(Queue* pq);
//返回头元素
QDatatype QueueFront(Queue* pq);
//返回尾元素
QDatatype QueueBack(Queue* pq);
//队列的初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = NULL;
pq->tail = NULL;
pq->size = 0;
}
//队列的销毁
void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
pq->size = 0;
}
//插入
void QueuePush(Queue* pq, QDatatype x)
{
assert(pq);
QNode* tmp = (QNode*)malloc(sizeof(QNode));
if (tmp == NULL)
{
perror("QueuePush::malloc");
return;
}
tmp->data = x;
tmp->next = NULL;
if (pq->head == NULL)
{
//头为空时判断尾一定要为空
assert(pq->tail == NULL);
pq->head = pq->tail = tmp;
}
else
{
pq->tail->next = tmp;
pq->tail = tmp;
}
pq->size++;
}
//删除
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
//防止tail为野指针
if (pq->head->next == NULL)
{
free(pq->head);
pq->head = pq->tail = NULL;
}
else
{
QNode* next = pq->head->next;
free(pq->head);
pq->head = next;
}
pq->size--;
}
//返回队列当前元素个数
int QueueSize(Queue* pq)
{
assert(pq);
return pq->size;
}
//判空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->size == 0;
}
//返回头元素
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;
}
typedef struct {
Queue q1;
Queue q2;
} MyStack;
MyStack* myStackCreate() {
MyStack*pst=(MyStack*)malloc(sizeof(MyStack));
if(pst==NULL)
{
perror("myStackCreate:::malloc");
return NULL;
}
//已经写好了队列,直接用写好的队列进行操作
QueueInit(&pst->q1);
QueueInit(&pst->q2);
return pst;
}
void myStackPush(MyStack* obj, int x) {
//把数据入进不为空的队列里
if(!QueueEmpty(&obj->q1))
{
QueuePush(&obj->q1,x);
}
else
{
QueuePush(&obj->q2,x);
}
}
int myStackPop(MyStack* obj) {
//假定出为空和不为空的两个指针
Queue*empty=&obj->q1;
Queue*nonempty=&obj->q2;
if(!QueueEmpty(&obj->q1))
{
empty=&obj->q2;
nonempty=&obj->q1;
}
//除了最后一个数据,其他全部倒过去
while(QueueSize(nonempty)>1)
{
QueuePush(empty,QueueFront(nonempty));
QueuePop(nonempty);
}
//返回并删除最后一个数据
int top=QueueBack(nonempty);
QueuePop(nonempty);
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);
}
3. 用栈实现队列
用栈实现队列
整体思想:一个栈为Pushst用来入栈,一个栈为Popst用来出栈。入栈时,直接把数据Push到Pushst栈中;出栈时,当Popst为空,则把Pushst的数据全部倒过来,Popst不为空时,直接出Popst栈里的数据。
typedef int STDatatype;
typedef struct stack
{
STDatatype* a;
int top;
int capacity;
}ST;
//初始化
void STInit(ST* ps);
//销毁
void STDestroy(ST* ps);
//插入
void STPush(ST* ps,STDatatype x);
//删除
void STPop(ST* ps);
//栈的大小
int STSize(ST* ps);
//判空
bool STEmpty(ST* ps);
//返回栈顶元素
STDatatype STTop(ST* ps);
//初始化
void STInit(ST* ps)
{
assert(ps);
ps->a = (STDatatype*)malloc(sizeof(STDatatype) * 4);
if (ps->a == NULL)
{
perror("STInit::malloc");
exit(-1);
}
ps->capacity = 4;
ps->top = 0;//top是栈顶元素的下一个位置
//ps->top=-1;//top是栈顶元素的位置
}
//销毁
void STDestroy(ST* ps)
{
assert(ps);
free(ps->a);
//ps->a = NULL;
ps->capacity = ps->top = 0;
}
//插入
void STPush(ST* ps, STDatatype x)
{
assert(ps);
//满了扩容
if (ps->top == ps->capacity)
{
STDatatype* tmp = (STDatatype*)realloc(ps->a, sizeof(STDatatype) * ps->capacity * 2);
if (tmp == NULL)
{
perror("STPush::realloc");
exit(-1);
}
ps->a = tmp;
ps->capacity *= 2;
}
ps->a[ps->top] = x;
ps->top++;
}
//删除
void STPop(ST* ps)
{
assert(ps);
assert(!STEmpty(ps));
ps->top--;
}
//栈的大小
int STSize(ST* ps)
{
assert(ps);
return ps->top;
}
//判空
bool STEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
//返回栈顶元素
STDatatype STTop(ST* ps)
{
assert(ps);
assert(!STEmpty(ps));
return ps->a[ps->top - 1];
}
typedef struct {
ST Pushst;
ST Popst;
} MyQueue;
MyQueue* myQueueCreate() {
MyQueue*pst=(MyQueue*)malloc(sizeof(MyQueue));
if(pst==NULL)
{
perror("myStackCreate:::malloc");
return NULL;
}
//已经写好了队列,直接用写好的队列进行操作
STInit(&pst->Pushst);
STInit(&pst->Popst);
return pst;
}
void myQueuePush(MyQueue* obj, int x) {
//直接往Pushst里入
STPush(&obj->Pushst,x);
}
int myQueuePop(MyQueue* obj) {
//如果Popst为空,则倒数据
/*if(STEmpty(&obj->Popst))
{
while(!STEmpty(&obj->Pushst))
{
STPush(&obj->Popst,STTop(&obj->Pushst));
STPop(&obj->Pushst);
}
}
int ret=STTop(&obj->Popst);*/
int ret=myQueuePeek(obj);
STPop(&obj->Popst);
return ret;
}
int myQueuePeek(MyQueue* obj) {
//如果Popst为空,则倒数据
if(STEmpty(&obj->Popst))
{
while(!STEmpty(&obj->Pushst))
{
STPush(&obj->Popst,STTop(&obj->Pushst));
STPop(&obj->Pushst);
}
}
int ret=STTop(&obj->Popst);
return ret;
}
bool myQueueEmpty(MyQueue* obj) {
return STEmpty(&obj->Popst) && STEmpty(&obj->Pushst);
}
void myQueueFree(MyQueue* obj) {
STDestroy(&obj->Popst);
STDestroy(&obj->Pushst);
free(obj);
}
4. 设计循环队列
设计循环队列
整体思想:用数组模拟实现循环队列,多增加一个数组的容量去判空来实现循环,以取模的方式来控制头尾指针在数组内部以进行操作。
typedef struct {
int*a;//数组
int front;//头下标
int rear;//尾下标
int k;//队列长度
} MyCircularQueue;
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue*obj=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
//assert(obj);
//assert(tmp);
obj->a=(int*)malloc(sizeof(int)*(k+1));//多开一个空间用来判断空满
obj->rear=obj->front=0;
obj->k=k;
return obj;
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
//头指针和尾指针在相同位置数组为空
return obj->front==obj->rear;
}
bool myCircularQueueIsFull(MyCircularQueue* obj) {
//尾指针的下一个位置是头指针数组为满
return (obj->rear+1)%(obj->k+1)==obj->front;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
if(myCircularQueueIsFull(obj))
{
return false;
}
else
{
obj->a[obj->rear++]=value;
obj->rear%=obj->k+1;
}
return true;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return false;
}
else
{
obj->front+=1;
obj->front=(obj->front)%(obj->k+1);
}
return true;
}
int myCircularQueueFront(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return -1;
}
else
{
return obj->a[obj->front];
}
}
int myCircularQueueRear(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return -1;
}
else
{
//先加上数组再取模防止指针跳出数组合法范围
return obj->a[((obj->rear-1)+obj->k+1)%(obj->k+1)];
}
}
void myCircularQueueFree(MyCircularQueue* obj) {
free(obj->a);
free(obj);
}
/**
* Your MyCircularQueue struct will be instantiated and called as such:
* MyCircularQueue* obj = myCircularQueueCreate(k);
* bool param_1 = myCircularQueueEnQueue(obj, value);
* bool param_2 = myCircularQueueDeQueue(obj);
* int param_3 = myCircularQueueFront(obj);
* int param_4 = myCircularQueueRear(obj);
* bool param_5 = myCircularQueueIsEmpty(obj);
* bool param_6 = myCircularQueueIsFull(obj);
* myCircularQueueFree(obj);
*/
注意:凡是front或rear++都要进行取模判断,防止头尾指针跳出指定数组。