栈的概念
- 栈是一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。
- 进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。
栈的特点
- 栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
- 压栈(动作):栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
- 出栈(动作):栈的删除操作叫做出栈。出数据也在栈顶。
栈的实现
- 栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为链表实现的栈在出栈时需要确定新的尾结点,存在极大的不便性;而数组由于其特点‘下标’的存在,使得数组在尾上插入删除数据时的代价比较小。
考虑到后期与STL能够比较容易接轨,故选择以下函数实现功能以及函数命名方式:
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top; // 栈顶位置
int capacity; // 容量
}Stack;
// 初始化栈
void StackInit(Stack* ps);
// 入栈
void StackPush(Stack* ps, STDataType x);
// 出栈
void StackPop(Stack* ps);
// 获取栈顶元素
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数
int StackSize(Stack* ps);
// 检测栈是否为空
int StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);
栈相关函数功能具体实现
#include"Stack.h"
// 初始化栈
void StackInit(Stack* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
// 入栈
void StackPush(Stack* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity);
if (tmp == NULL)
{
printf("realloc fail!\n");
return;
}
ps->a = tmp;
ps->capacity = newcapacity;
}
ps->a[ps->top++] = x;
}
// 出栈
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 = 0;
ps->capacity = 0;
}
队列的概念
- 队列只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表。
队列的特点
- 队列具有先进先出FIFO(First In First Out) 的特点。
- 入队列:进行插入操作的一端称为队尾。
- 出队列:进行删除操作的一端称为队头。
队列的实现
- 队列同样也可以数组和链表的结构实现,但使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,则必须将后面的数据进行前移,使得效率会比较低;但使用链表只需将头指针与尾指针进行对应的改变即可。
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
//链式结构:表示队列
typedef int QDataType;
typedef struct QueueNode
{
struct QListNode* next;
QDataType data;
}QNode;
// 队列的结构
typedef struct Queue
{
QNode* head;
QNode* tail;
int size;
}Queue;
// 初始化队列
void QueueInit(Queue* pq);
// 队尾入队列
void QueuePush(Queue* pq, QDataType data);
// 队头出队列
void QueuePop(Queue* pq);
// 获取队列头部元素
QDataType QueueFront(Queue* pq);
// 获取队列队尾元素
QDataType QueueBack(Queue* pq);
// 获取队列中有效元素个数
int QueueSize(Queue* pq);
// 检测队列是否为空
bool QueueEmpty(Queue* pq);
// 销毁队列
void QueueDestroy(Queue* pq);
队列相关函数功能具体实现
#include "Queue.h"
// 初始化队列
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
pq->size = 0;
}
// 队尾入队列
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
printf("malloc fail\n");
return;
}
newnode->data = x;
newnode->next = NULL;
if (QueueEmpty(pq))
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
pq->size++;
}
// 队头出队列
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(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 QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->data;
}
// 获取队列队尾元素
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->data;
}
// 获取队列中有效元素个数
int QueueSize(Queue* pq)
{
assert(pq);
return pq->size;
}
// 检测队列是否为空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->size == 0;
}
// 销毁队列
void QueueDestroy(Queue* pq)
{
assert(pq);
while (pq->head)
{
QNode* cur = pq->head->next;
free(pq->head);
pq->head = cur;
}
pq->head = pq->tail = NULL;
pq->size = 0;
}
栈和队列相关题目