ps->top = -1;
ps->capacity = 4;
}void StackDestroy(ST* ps)
{
assert(ps);free(ps->a);
ps->a = NULL;
ps->top = -1;
ps->capacity = 0;
}void StackPush(ST* ps, STDatatype x)
{
assert(ps);//
if (ps->top+1 == ps->capacity)
{
STDatatype* tmp = (STDatatype*)realloc(ps->a, ps->capacity * 2 * sizeof(STDatatype));
if (tmp == NULL)
{
perror(“realloc fail”);
exit(-1);
}ps->a = tmp; ps->capacity \*= 2;
}
ps->top++;
ps->a[ps->top] = x;
}// 20:20
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));ps->top–;
}STDatatype StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));return ps->a[ps->top];
}int StackSize(ST* ps)
{
assert(ps);return ps->top+1;
}bool isValid(char * s){
ST st;
StackInit(&st);
while(*s) {
if(*s == ‘[’ || *s == ‘(’ || *s == ‘{’) {
StackPush(&st, *s);
s ++;
}else {
if(StackEmpty(&st)) {
StackDestroy(&st);
return false;
}
char top = StackTop(&st);
StackPop(&st);
if(*s == ‘]’ && top != ‘[’ || *s == ‘}’ && top != ‘{’ || *s == ‘)’ && top != ‘(’) {
StackDestroy(&st);
return false;
}else {
s ++;
}
}
}
bool ret = StackEmpty(&st);
StackDestroy(&st);
return ret;
}由于C语言没有栈这个类,所以我们需要自己实现栈,并调用来实现。
队列
队列的概念及结构
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出特性。
入队列:进行插入操作的一端称为队尾
出队列:进行删除操作的一端称为对头
队列的实现
队列也可以数组和链表的结构实现,使用链表的结构更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率比较低。
初始化队列
void QueueInit(Queue\* q) { assert(q); q->front = NULL; q->rear = NULL; q->size = 0; }
队尾入队列
void QueuePush(Queue\* q, QDataType data) { assert(q); QNode\* newnode = (QNode\*)malloc(sizeof(QNode)); if (newnode == NULL) { perror("malloc fail"); exit(-1); } newnode->data = data; newnode->pNext = NULL; if (q->rear == NULL) { q->front = q->rear = newnode; } else { q->rear->pNext = newnode; q->rear = newnode; } q->size++; }
队头出队列
void QueuePop(Queue\* q) { assert(q); assert(!QueueEmpty(q)); if (q->front->pNext == NULL) { free(q->front); q->front = q->rear = NULL; } else { QNode\* del = q->front; q->front = q->front->pNext; free(del); } q->size--; }
获取队头元素
QDataType QueueFront(Queue\* q) { assert(q); assert(!QueueEmpty(q)); return q->front->data; }
获取队尾元素
QDataType QueueBack(Queue\* q) { assert(q); assert(!QueueEmpty(q)); return q->rear->data; }
销毁队列
void QueueDestroy(Queue\* q) { assert(q); QNode\* cur = q->front; while (cur) { QNode\* del = cur; cur = cur->pNext; free(del); } q->front = q->rear = NULL; q->size = 0; }
判断队列是否为空
bool QueueEmpty(Queue\* q) { assert(q); return q->front == NULL && q->rear == NULL; }
用队列实现栈
思路:
队列:先进先出 栈:先进后出
保持一个队列存数据,一个队列为空,队列出数据是在队头的,而栈相当于在队尾出数据。
代码:
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); }else { QueuePush(&obj->q2, x); } } int myStackPop(MyStack\* obj) { Queue\* emptyQ = &obj->q1; Queue\* noemptyQ = &obj->q2; if(!QueueEmpty(&obj->q1)) { emptyQ = &obj->q2; noemptyQ = &obj->q1; } while(QueueSize > 1) { QueuePush(emptyQ, QueueFront(noemptyQ)); QueuePop(noemptyQ); } int top = QueueFront(noemptyQ); QueuePop(noemptyQ); 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); }
用栈实现队列
思路:两个栈一个栈用来插入数据,另一个栈用来弹出数据。
代码:
typedef struct { ST pushst; ST popst; } MyQueue; MyQueue\* myQueueCreate() { MyQueue\* pq = (MyQueue\*)malloc(sizeof(MyQueue)); StackInit(&pq->pushst); StackInit(&pq->popst); return pq; } void myQueuePush(MyQueue\* obj, int x) { assert(obj); StackPush(&obj->pushst, x); } int myQueuePop(MyQueue\* obj) { assert(obj); assert(!myQueueEmpty(obj)); int peek = myQueuePeek(obj); StackPop(&obj->popst); return peek; } int myQueuePeek(MyQueue\* obj) { assert(obj); assert(!myQueueEmpty(obj)); if(StackEmpty(&obj->popst)) { while(!StackEmpty(&obj->pushst)) { StackPush(&obj->pushst, StackTop(&obj->popst)); StackPop(&obj->pushst); } } return StackTop(&obj->popst); } bool myQueueEmpty(MyQueue\* obj) { assert(obj); return StackEmpty(&obj->pushst) && StackEmpty(&obj->popst); } void myQueueFree(MyQueue\* obj) { assert(obj); StackDestroy(&obj->pushst); StackDestroy(&obj->popst);
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
id myQueueFree(MyQueue* obj) {
assert(obj); StackDestroy(&obj->pushst); StackDestroy(&obj->popst);
[外链图片转存中…(img-0pmW2UrY-1714175748726)]
[外链图片转存中…(img-yCFSe5qI-1714175748727)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!