数据结构与算法5:栈和队列(相关算法:括号匹配问题(有效的括号))

是一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶。
栈的实现一般可以使用数组和链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较少。

//stack.h  动态为数组分配空间
#pragma once
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

typedef int STDataType;

//#define MAX 10000
//struct Stack {
//	STDataType a[MAX];
//	int top;
//};

typedef struct Stack {
	STDataType* a;
	int capacity;
	int top;
}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);

//stack.c
#include "stack.h"

void StackInit(ST* ps) {
	assert(ps);
	//如果初始top是0,那么top指向的是栈顶元素的下一个
	//如果初始top是-1,那么top指向的是栈顶元素
	ps->a = (STDataType*)malloc(sizeof(STDataType) * 4);
	if (ps->a == NULL) {
		printf("realloc fail!");
		exit(-1);
	}
	ps->capacity = 4;
	ps->top = 0;
}
void StackDestory(ST* ps) {
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}
//入栈
void StackPush(ST* ps, STDataType x) {
	assert(ps);
	//满了
	if (ps->top == ps->capacity) {
		STDataType* tmp = (STDataType*)realloc(ps->a, ps->capacity * 2 * sizeof(STDataType));
		if (NULL == tmp) {//判断相等还是尽量常量写在左边,这个错误找了半个小时
			printf("realloc fail!");
			exit(-1);
		}
		else {
			ps->a = tmp;
			ps->capacity *= 2;
		}
	}
	ps->a[ps->top] = x;
	ps->top++;
}
//出栈
void StackPop(ST* ps) {
	assert(ps);
	//栈空了,调用pop,报错
	                         assert(ps->top > 0);
	ps->top--;
}

STDataType StackTop(ST* ps) {
	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top - 1];
}
int StackSize(ST* ps) {
	assert(ps);
	return ps->top;
}
bool StackEmpty(ST* ps) {
	assert(ps);
	return ps->top == 0;
}
//test.c
#include "stack.h"
int main() {
	ST st;
	StackInit(&st);
	StackPush(&st, 1);
	StackPush(&st, 2);
	printf("%d ", StackTop(&st));
	StackPop(&st);
	StackPush(&st, 3);
	StackPush(&st, 4);
	StackPush(&st, 5);
	while (!StackEmpty(&st)) {
		printf("%d ", StackTop(&st));
		StackPop(&st);
	}
	StackDestory(&st);
	return 0;
}

队列

队列只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出特点。
入队列:进行插入操作的一端称为队尾。
出队列:进行删除操作的一端称为队头。
队列的实现一般可以使用数组和链表,相对而言链表的结构实现较优,链表可以使用尾插和头删,实现先进先出。

数组:不适合,队头出数据需要挪动数据

单链表:尾插头删效率都很高

//test.c
#include "stack.h"
int main() {
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	printf("%d ", QueueFront(&q));
	QueuePop(&q);
	QueuePush(&q, 4);
	QueuePush(&q, 5);
	while (!QueueEmpty(&q)) {
		printf("%d ", QueueFront(&q));
		QueuePop(&q);
	}
	return 0;
}
//Queue.c

#include "stack.h"

void QueueInit(Queue* pq) {
	assert(pq);
	pq->head = pq->tail = NULL;

}void QueueDestory(Queue* pq) {
	assert(pq);
	QNode* cur = pq->head;
	while (cur) {
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
}

void QueuePush(Queue* pq, QDataType x) {
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL) {
		printf("malloc fail");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;

	if (pq->tail == NULL) {
		pq->head = pq->tail = newnode;
	}
	else {
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}

void QueuePop(Queue* pq) {
	assert(pq);
	assert(pq->head);
	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;
	}
}

QDataType QueueFront(Queue* pq) {
	assert(pq);
	assert(pq->head);
	return pq->head->data;
}
QDataType QueueBack(Queue* pq) {
	assert(pq);
	assert(pq->head);
	return pq->tail->data;
}

int QueueSize(Queue* pq) {
	assert(pq);
	int size = 0;
	QNode* cur = pq->head;
	while (cur) {
		size++;
		cur = cur->next;
	}
	return size;
}
bool QueueEmpty(Queue* pq) {
	assert(pq);
	return pq->head == NULL;
}
//Queue.h
#pragma once
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

typedef int QDataType;

typedef struct QueueNode {
	struct QueueNode* next;
	QDataType data;
}QNode;

typedef struct Queue {
	QNode* head;
	QNode* 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);

算法:有效的括号

给定一个只包含’ ( ', ’ ) ', ’ { ', ’ } ', ’ [ ', ’ ] ’ 的字符串s,判断字符串是否有效。

有效字符串满足:

  1. 左括号必须用相同类型的右括号闭合
  2. 左括号必须以正确的顺序闭合
  3. 每个右括号都有一个对应的相同类型的左括号。

示例 1:

输入:s = “()”
输出:true

示例 2:

输入:s = “()[]{}”
输出:true

示例 3:

输入:s = “(]”
输出:false

思路:如果是左括号入栈,要是右括号首先判断栈是否为空,如果为空,返回false,如果不为空,取栈顶的元素,再判断栈顶的元素是否和要入栈的元素匹配。如果匹配就++继续看是否匹配,如果不匹配返回false,比较完后,判断元素时候为空,因为有可能全是左括号入栈,而没有右括号匹配,消除这种错误。

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>


typedef char STDataType;
typedef struct Stack {
	STDataType* a;
	int capacity;
	int top;
}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);
	//如果初始top是0,那么top指向的是栈顶元素的下一个
	//如果初始top是-1,那么top指向的是栈顶元素
	ps->a = (STDataType*)malloc(sizeof(STDataType) * 4);
	if (ps->a == NULL) {
		printf("realloc fail!");
		exit(-1);
	}
	ps->capacity = 4;
	ps->top = 0;
}
void StackDestory(ST* ps) {
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}
//入栈
void StackPush(ST* ps, STDataType x) {
	assert(ps);
	//满了
	if (ps->top == ps->capacity) {
		STDataType* tmp = (STDataType*)realloc(ps->a, ps->capacity * 2 * sizeof(STDataType));
		if (NULL == tmp) {
			printf("realloc fail!");
			exit(-1);
		}
		else {
			ps->a = tmp;
			ps->capacity *= 2;
		}
	}
	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(ps->top > 0);
	return ps->a[ps->top - 1];
}
int StackSize(ST* ps) {
	assert(ps);
	return ps->top;
}
bool StackEmpty(ST* ps) {
	assert(ps);
	return ps->top == 0;
}

bool isValid(char* s) {
	ST st;
	StackInit(&st);
	while (*s != '\0') {
		switch (*s) {
		case '{':
		case '[':
		case '(': {
			StackPush(&st, *s);
			s++;
			break;
		}
		case '}':
		case ']':
		case ')': {
			if (StackEmpty(&st)) {
				StackDestory(&st);
				return false;
			}
			char top = StackTop(&st);
			StackPop(&st);
			//匹配
			if ((*s == '}' && top != '{')
				|| (*s == ']' && top != '[')
				|| (*s == ')' && top != '(')) {
				
				s++;
			}
			//不匹配
			else {
				StackDestory(&st);
				return false;
			}
			break;
		}
		default:
			break;
		}
	}
	bool ret = StackEmpty(&st);
	StackDestory(&st);
	return ret;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值