目录
一.栈
1.概念
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶。
2.结构图
3.栈的实现(数组栈)
(1)栈的结构
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
STDataType top;//标记栈顶元素
STDataType capacity;
}ST;
(2)初始化栈
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
pst->capacity = 0;
pst->top = 0;
}
- 关于top初始化为0还是-1的问题
(3)插入数据
void STPush(ST* pst, STDataType x)
{
assert(pst);
//扩容
if (pst->top == pst->capacity)
{
STDataType newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
STDataType* ptr = (STDataType*)realloc(pst->a,sizeof(newcapacity));
if (ptr == NULL)
{
perror("ptr->realloc");
return;
}
pst->a = ptr;
pst->capacity = newcapacity;
}
pst->a[pst->top] = x;
pst->top++;
}
(4)删除数据
void STPop(ST* pst)
{
assert(pst);
//不为空
assert(pst->top > 0);
pst->top--;
}
(5)判断栈是否为空
bool STEmpty(ST* pst)
{
assert(pst);
return pst->top == 0;
//这里不要写成pst->capacity==0,capacity表示的是该栈的容量
}
(6)判断栈的元素个数
//栈内元素个数
int STSize(ST* pst)
{
assert(pst);
return pst->top;
(7)栈顶元素
STDataType STTop(ST* pst)
{
assert(pst);
assert(pst->top > 0);
return pst->a[pst->top - 1];
}
(8)销毁栈
void STDestroy(ST* pst)
{
assert(pst);
if (pst ->a != NULL)
{
free(pst->a);
pst->a = NULL;
pst->top = pst->capacity = 0;
}
}
4.注意事项
- 栈的压栈和出栈关系为一对多:压栈方式只有一种,但是出栈方式为多种(虽然有多种,但还是要遵循定义)
- 这里的栈和内存上的栈区是两个不同的理念
二.队列
1.概念
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out)
入队列:进行插入操作的一端称为队尾
出队列:进行删除操作的一端称为队头
2.结构图
3.队列的实现(单链表)
(1)队列结构
typedef int QDataType;
typedef struct QueueNode
{
QDataType val;
struct QueueNode* next;
}QNode;
struct Queue
{
QNode* phead;//指向队列的头
QNode* ptail;//指向队列的尾
int size;//队列内元素个数
};
- 如果不添加Queue这个结构体
在后面实现插入和删除的操作时:
//插入 void QueuePush(QNode**phead, QNode**ptail,QDataType x); //删除 void QueuePop(QNode*phead, QNode*ptail,QDataType x;
问题1:找对尾元素的时候并不好找,时间复杂度为O(n),因此还需要多传一个参数ptail
问题2:在插入第一个元素时要用到二级指针
- 添加上结构体Queue
通过直接访问结构体Queue的成员可以避免使用二级指针的问题,同时可以减少参数。
(2)队列的初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
(3)队列的插入操作
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc");
return;
}
newnode->val = x;
newnode->next = NULL;
if (pq->ptail == NULL)
{
pq->ptail = pq->phead = newnode;
}
else
{
pq->ptail->next = newnode;
pq->ptail = newnode;
}
pq->size++;
}
- 图示解析
(4)队列的删除操作
//删除
void QueuePop(Queue* p)
{
assert(pq);
//链表为空不进行删除
assert(pq->phead);
QNode* del = pq->phead;
pq->phead = pq->phead->next;
free(del);
if (pq->phead == NULL)
{
pq->ptail = NULL;
}
pq->size--;
}
- 图示
(5)取队头的元素
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(pq->phead);
return pq->phead->val;
}
(6)取队尾的元素
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(pq->ptail);
return pq->ptail->val;
}
(7)判断队列是否为空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->ptail == NULL;
}
(8)判断队列内的元素个数
int QueueSize(Queue* pq)
{
assert(pq);
return pq->size;
}
(9)销毁队列
void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* cur = pq->phead;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->phead =pq->ptail= NULL;
pq->size = 0;
}
4.注意
队列的插入和删除是一对一关系的,怎么插入队列的怎么出队列。