最新【数据结构】栈和队列(1)

void StackInit(Stack\* ps) {
	assert(ps);
	ps->a = (STDataType\*)malloc(sizeof(STDataType) \* 4);
	if (ps->a == NULL) {
		perror("malloc fail");
		exit(-1);
	}
	ps->top = 0;
	ps->capacity = 4;
}

入栈
void StackPush(Stack\* ps, STDataType x) {
	assert(ps);
    //判断栈是否满了,如果满了就扩容
	if (ps->top == 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->a[ps->top] = x;
	ps->top++;
}

出栈
void StackPop(Stack\* ps) {
	assert(ps);
	assert(!StackEmpty(ps));
	ps->top--;
}

获取栈顶元素
STDataType StackTop(Stack\* ps) {
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->top - 1];
}

获取栈中有效元素个数
int StackSize(Stack\* ps) {
	assert(ps);

	return ps->top;
}

判断栈是否为空
bool StackEmpty(Stack\* ps) {
	assert(ps);
	return ps->top == 0;
}

销毁栈
void StackDestroy(Stack\* ps) {
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

括号匹配问题

思路:这题主要思路,就是遇见左括号就入栈,遇见右括号就将栈顶元素,拿出来对比是否匹配,如果不匹配就直接返回false.

image-20221118113513457

typedef char STDatatype;
typedef struct Stack
{
	STDatatype\* a;
	int capacity;
	int top;   // 初始为0,表示栈顶位置下一个位置下标
}ST;


bool StackEmpty(ST\* ps)
{
	assert(ps);
	return ps->top == -1;
}
void StackInit(ST\* ps)
{
	assert(ps);

	//ps->a = NULL;
	//ps->top = 0;
	//ps->capacity = 0;

	ps->a = (STDatatype\*)malloc(sizeof(STDatatype)\* 4);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}

	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语言没有栈这个类,所以我们需要自己实现栈,并调用来实现。

队列
队列的概念及结构

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出特性。

入队列:进行插入操作的一端称为队尾

出队列:进行删除操作的一端称为对头

image-20221118115525187

队列的实现

image-20221118144645617

队列也可以数组和链表的结构实现,使用链表的结构更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率比较低。

image-20221118144112948

初始化队列
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;
}

销毁队列

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

ssert(q);

assert(!QueueEmpty(q));
return q->rear->data;
}



销毁队列

[外链图片转存中…(img-9KdY4Kyy-1714823907852)]
[外链图片转存中…(img-3ecKAweR-1714823907853)]
[外链图片转存中…(img-DJk9AWfA-1714823907854)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值