栈和队列
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;
}