文章目录
栈的概念和结构
栈是一种常见的数据结构,它遵循后进先出LIFO(Last In First Out)的原则。进行数据插入和操作的一端称为栈顶,另一端称为栈底。
压栈:栈的插入操作被称为压栈/进栈/入栈,入数据在栈顶。
出栈:栈的删除操作。出数据也在栈顶;
栈的实现
栈可以用数组或者是链表来实现;这里将使用数组来实现,因为数组在插入删除时消耗代价较小;对于链表,由于Top放在尾,删除时还需要由头指针循环遍历找到尾结点前一个;
栈的数据结构
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
栈的初始化和销毁
void STInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
void STDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = 0;
ps->top = 0;
}
出栈和入栈
void STPush(ST* ps, STDataType x)
{
assert(ps);
//满扩容
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = realloc(ps->a, sizeof(STDataType) * newcapacity);
if (tmp == NULL)
{
perror("Stack fail");
exit(-1);
}
ps->a =tmp;
ps->capacity = newcapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
void STPop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
ps->top--;
}
获取栈顶、大小,判空
STDataType STTop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
return ps->a[ps->top - 1];
}
int STSize(ST* ps)
{
assert(ps);
return ps->top;
}
bool STEmpty(ST* ps)
{
assert(ps);
return ps->top==0;
}
接下来我们简单的验证一下:
void Test1()
{
ST st;
STInit(&st);
STPush(&st, 1);
STPush(&st, 2);
STPush(&st, 3);
STPush(&st, 4);
while (!STEmpty(&st))
{
printf("%d ", STTop(&st));
STPop(&st);
}
printf("\n");
STDestory(&st);
}
int main()
{
Test1();
return 0;
}
队列的概念和结构
队列只允许在一端进行插入操作,在另一端删除数据操作的特殊线性表,具有先进先出FIFO(First In First Out) ;
入队列:在队尾进行插入的操作
出队列:在队头进行删除的操作
队列的实现
队列的实现可以用数组和链表来实现;这里将使用链表来实现;因为如果使用数组的结构,在数组头进行删除出数据,效率比较低;
队列的数据结构
typedef int QDataType;
typedef struct QueneNode
{
struct QueneNode* next;
QDataType data;
}QNode;
typedef struct Quene
{
QNode* head;
QNode* tail;
int size;
}Quene;
队列的初始化和销毁
void QueneInit(Quene* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
pq->size = 0;
}
void QueneDestory(Quene* 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 QuenePush(Quene* pq, QDataType x)
{
assert(pq);
//扩容
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc fail");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
//第一个插入
if (pq->head == NULL)
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
pq->size++;
}
队列的删除
void QuenePop(Quene* pq)
{
assert(pq);
//判断是否没有数据
assert(!QueneEmpty(pq));
QNode* next = pq->head->next;
//只有一个数据
if (pq->head->next == NULL)
{
free(pq->head);
pq->head = pq->tail = next;
}
else
{
free(pq->head);
pq->head = next;
}
pq->size--;
}
获取队头和队尾的数据
QDataType QueneFront(Quene* pq)
{
assert(pq);
assert(!QueneEmpty(pq));
return pq->head->data;
}
QDataType QueneBack(Quene* pq)
{
assert(pq);
assert(!QueneEmpty(pq));
return pq->tail->data;
}
获取队列长度和判空
int QueneSize(Quene* pq)
{
assert(pq);
return pq->size;
}
bool QueneEmpty(Quene* pq)
{
assert(pq);
return pq->head == NULL;
}
最后做一下简单的验证
void Test1()
{
Quene q;
QueneInit(&q);
QuenePush(&q, 1);
QuenePush(&q, 2);
QuenePush(&q, 3);
QuenePop(&q);
while (QueneSize(&q)>0)
{
printf("%d ", QueneBack(&q));
QuenePop(&q);
}
QueneDestory(&q);
}
int main()
{
Test1();
return 0;
}
栈和队列的一些题目
1.有效的括号
代码:
#define _CRT_SECURE_NO_WARNINGS 1
typedef char STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
void STInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
void STDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = 0;
ps->top = 0;
}
void STPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = realloc(ps->a, sizeof(STDataType) * newcapacity);
if (tmp == NULL)
{
perror("Stack fail");
exit(-1);
}
ps->a =tmp;
ps->capacity = newcapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
void STPop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
ps->top--;
}
STDataType STTop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
return ps->a[ps->top - 1];
}
int STSize(ST* ps)
{
assert(ps);
return ps->top;
}
bool STEmpty(ST* ps)
{
assert(ps);
return ps->top==0;
}
bool isValid(char * s){
ST st;
//栈初始化
STInit(&st);
while(*s)
{
//左括号就入栈
if(*s=='('||*s=='['||*s=='{')
{
STPush(&st,*s);
}
//右括号就出栈,判断
else
{
栈空,表明没有左括号
if(STEmpty(&st))
{
STDestory(&st);
return false;
}
char val=STTop(&st);
STPop(&st);
//不符合
if((*s==')'&&val!='(')
||(*s==']'&&val!='[')
||(*s=='}'&&val!='{'))
{
return false;
}
}
s++;
}
if(!STEmpty(&st))
{
return false;
}
STDestory(&st);
return true;
}
2.用队列实现栈
代码:
#include<assert.h>
#include<stdbool.h>
typedef int QDataType;
typedef struct QueneNode
{
struct QueneNode* next;
QDataType data;
}QNode;
typedef struct Quene
{
QNode* head;
QNode* tail;
int size;
}Quene;
void QueneInit(Quene* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
pq->size = 0;
}
void QueneDestory(Quene* 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 QuenePush(Quene* pq, QDataType x)
{
assert(pq);
//扩容
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc fail");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
//第一个插入
if (pq->head == NULL)
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
pq->size++;
}
bool QueneEmpty(Quene* pq)
{
assert(pq);
return pq->head==NULL;
}
int QueneSize(Quene* pq)
{
assert(pq);
return pq->size;
}
void QuenePop(Quene* pq)
{
assert(pq);
//判断是否没有数据
assert(!QueneEmpty(pq));
//只有一个数据
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--;
}
QDataType QueneFront(Quene* pq)
{
assert(pq);
assert(!QueneEmpty(pq));
return pq->head->data;
}
QDataType QueneBack(Quene* pq)
{
assert(pq);
assert(!QueneEmpty(pq));
return pq->tail->data;
}
typedef struct {
Quene q1;
Quene q2;
} MyStack;
//创建一个自己的栈
//用一个结构体进行包装
MyStack* myStackCreate() {
MyStack* obj=malloc(sizeof(MyStack));
QueneInit(&obj->q1);
QueneInit(&obj->q2);
return obj;
}
//插入判断哪个队列不为空
void myStackPush(MyStack* obj, int x) {
if(!QueneEmpty(&obj->q2))
{
QuenePush(&obj->q2,x);
}
else
{
QuenePush(&obj->q1,x);
}
}
//将队列分为空队列和不空队列
int myStackPop(MyStack* obj) {
Quene* empty=&obj->q1;
Quene* nonempty=&obj->q2;
if(!QueneEmpty(&obj->q1))
{
empty=&obj->q2;
nonempty=&obj->q1;
}
//数据转移
while(QueneSize(nonempty)>1)
{
QuenePush(empty,QueneFront(nonempty));
QuenePop(nonempty);
}
//删除数据,并返回
int Top=QueneFront(nonempty);
QuenePop(nonempty);
return Top;
}
int myStackTop(MyStack* obj) {
if(!QueneEmpty(&obj->q2))
{
return QueneBack(&obj->q2);
}
else
{
return QueneBack(&obj->q1);
}
}
bool myStackEmpty(MyStack* obj) {
return QueneEmpty(&obj->q1)&&QueneEmpty(&obj->q2);
}
void myStackFree(MyStack* obj) {
QueneDestory(&obj->q1);
QueneDestory(&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);
*/
3.用栈实现队列
代码:
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
void STInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
void STDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = 0;
ps->top = 0;
}
void STPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = realloc(ps->a, sizeof(STDataType) * newcapacity);
if (tmp == NULL)
{
perror("Stack fail");
exit(-1);
}
ps->a =tmp;
ps->capacity = newcapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
void STPop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
ps->top--;
}
STDataType STTop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
return ps->a[ps->top - 1];
}
int STSize(ST* ps)
{
assert(ps);
return ps->top;
}
bool STEmpty(ST* ps)
{
assert(ps);
return ps->top==0;
}
//栈分为插入栈和输出栈
typedef struct {
ST instack;
ST outstack;
} MyQueue;
MyQueue* myQueueCreate() {
MyQueue* obj=malloc(sizeof(MyQueue));
STInit(&obj->instack);
STInit(&obj->outstack);
return obj;
}
void myQueuePush(MyQueue* obj, int x) {
STPush(&obj->instack,x);
}
int myQueuePop(MyQueue* obj) {
//先判断输出栈是否为空
if(STEmpty(&obj->outstack))
{
while(STSize(&obj->instack))
{
STPush(&obj->outstack,STTop(&obj->instack));
STPop(&obj->instack);
}
}
int head=STTop(&obj->outstack);
STPop(&obj->outstack);
return head;
}
int myQueuePeek(MyQueue* obj) {
if(STEmpty(&obj->outstack))
{
while(STSize(&obj->instack))
{
STPush(&obj->outstack,STTop(&obj->instack));
STPop(&obj->instack);
}
}
return STTop(&obj->outstack);
}
bool myQueueEmpty(MyQueue* obj) {
return STEmpty(&obj->instack)&&STEmpty(&obj->outstack);
}
void myQueueFree(MyQueue* obj) {
STDestory(&obj->instack);
STDestory(&obj->outstack);
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);
*/
4.设计循环队列
代码:
typedef struct {
int* a;
int head;
int rear;
int k;
} MyCircularQueue;
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue* obj=malloc(sizeof(MyCircularQueue));
obj->a=malloc(sizeof(int)*(k+1));
obj->head=obj->rear=0;
obj->k=k;
return obj;
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
return obj->head==obj->rear;
}
bool myCircularQueueIsFull(MyCircularQueue* obj) {
//当rear超过k+1时,归0
return (obj->rear+1)%(obj->k+1)==obj->head;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
if(myCircularQueueIsFull(obj))
{
return false;
}
obj->a[obj->rear]=value;
obj->rear++;
//限制在0-k
obj->rear%=(obj->k+1);
return true;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return false;
}
obj->head++;
//限制在0-k
obj->head%=(obj->k+1);
return true;
}
int myCircularQueueFront(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return -1;
}
return obj->a[obj->head];
}
int myCircularQueueRear(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return -1;
}
//考虑rear-1等于-1的情况
return obj->a[(obj->rear+obj->k)%(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);
*/