栈和队列基本操作--出,入,获取,检测,销毁

1.栈的定义

栈的概念及结构
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端
称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈。
在这里插入图片描述

2.栈的增删查改

对于栈的实现,我们选用顺序表来实现
在这里插入图片描述

(1)初始化栈

void StackInit(Stack* ps)
{
	ps->a = 0;
	ps->top = 0;
	ps->capacity = 0;
}

(2)入栈

在这里插入图片描述

void StackPush(Stack* ps, STDataType data)
{
	assert(ps);
	if (ps->capacity == ps->top)
	{
		STDataType* tmp = (STDataType*)realloc(ps->a,sizeof(STDataType) * ps->capacity * 2);
			if (tmp == NULL)
			{
				printf("realloc fail\n");
				exit(-1);//结束整个程序
			}
			ps->a = tmp;
			ps->capacity = ps->capacity * 2;
	}
	ps->a[ps->top] = data;
	ps->top++;
}

(3)出栈

在这里插入图片描述

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

(4)获取栈顶元素

STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->top - 1];
}

(5)获取栈中有效元素个数

int StackSize(Stack* ps) 
{
	assert(ps);
	return ps->top;
}

(6)检测栈是否为空,如果为空返回非零结果,如果不为空返回0

bool StackEmpty(Stack* ps)
{
	assert(ps);
	return ps->top == 0;
}

(7)销毁栈

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

3.队列的定义

队列的概念及结构
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出
FIFO(First In First Out) 入队列:进行插入操作的一端称为队尾 出队列:进行删除操作的一端称为队头
在这里插入图片描述
在这里插入图片描述

typedef int QDataType;
typedef struct QListNode
{
	struct QListNode* next;
	QDataType data;
}QNodeNode;
// 队列的结构
typedef struct Queue
{
	QNodeNode* head;
	QNodeNode* tail;
}Queue;

4.队列的增删查改

(1)初始化队列

空队列插入第一个元素
在这里插入图片描述

void QueueInit(Queue* pq)
{
	assert(pq);

	pq->head = pq->tail = NULL;
}

(2)队尾入队列

void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)
	{
		printf("malloc fail\n");
		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;
	}
}

(3)队头出队列

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QueueNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
}

(4)获取队列头部元素

QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->head->data;
}

(5)获取队列队尾元素

QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->tail->data;
}

(6)获取队列中有效元素个数

int QueueSize(Queue* pq)
{
	int size = 0;
	QueueNode* cur = pq->head;
	while (cur)
	{
		++size;
		cur = cur->next;
	}

	return size;
}

(7)检测队列是否为空,如果为空返回非零结果,如果非空返回0

bool QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->head == NULL;
}

(8)销毁队列

void QueueDestroy(Queue* pq)
{
	assert(pq);

	QueueNode* cur = pq->head;
	while (cur)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}

	pq->head = pq->tail = NULL;
}

5.相关练习

题目描述:给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
在这里插入图片描述

解题思路:采用入栈和出栈的方法,当我们遇到左括号,即’(‘或者’[‘或者’{‘时压入栈中,s继续往后遍历,当我们遇到右括号的时候,即’)‘或者’]‘或者’}’,此时我们将栈顶数据弹出,看是否匹配,如果不匹配,直接返回false,如果匹配继续往下遍历,直到s为空。
代码实现如下:

bool isValid(char * s)
{
    Stack 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);
            if((top=='['&&*s!=']')
            ||(top=='('&&*s!=')')
            ||(top=='{'&&*s!='}'))
            {
                StackDestroy(&st);
                return false;
            }
            else
            {
               StackPop(&st);
               ++s;
            }
        }
    }
    bool ret=StackEmpty(&st);
    StackDestroy(&st);
    return ret;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值