栈
栈的定义
栈(stack)又名堆栈,它是一种运算受限的线性表。限定仅在表尾进行插入和删除操作的线性表。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。
简单定义:栈就是一种只允许在表尾进行插入和删除操作的线性表
如何理解栈的概念
在一个储物箱中,堆了一堆衣服,一件衣服在最下面,而我要拿这件衣服,就意味着我必须将上面的衣服全部拿出来才可以,但是由于储物箱只有一个口,我也只能从上面拿东西,导致结果就是先进后出!
栈的定义
stack<int> st1;//使用默认的适配器定义栈
stack<int, vector<int>> st2;//使用特定的适配器定义栈。
stack<int, list<int>> st3;//使用特定的适配器定义栈。
栈的成员函数
函数 | 功能 |
---|---|
push | 元素入栈 |
pop | 元素出栈 |
size | 返回栈内元素个数 |
top | 获取栈顶元素 |
empty | 判断栈是否为空 |
例子:
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int main()
{
stack<int, vector<int>> st;
st.push(4);
st.push(3);
st.push(2);
st.push(1);
cout << st.size() << endl; //4
while (!st.empty())
{
cout << st.top() << " ";
st.pop();
}
cout << endl; //1 2 3 4
return 0;
}
栈的实现
在上面,我们已经提过了,栈实际上就是一种线性表的特例,所以栈的实现和线性表一样,均使用数组实现,所以我们使用一个一维数组来存储元素。
1.初始化一个栈
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
//pst->top = -1; // top 指向栈顶数据
pst->top = 0; // top 指向栈顶数据的下一个位置
pst->capacity = 0;
}
2.向栈中插入数据(压栈、入栈、进栈)
void STPush(ST* pst, STDataType x)
{
if (pst->top == pst->capacity)
{
int newCapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
STDataType* tmp = (STDataType*)realloc(pst->a, newCapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
return;
}
pst->a = tmp;
pst->capacity = newCapacity;
}
pst->a[pst->top] = x;
pst->top++;
}
3.删除栈顶元素(出栈、弹栈)
void STPop(ST* pst)
{
assert(pst);
assert(!STEmpty(pst));
pst->top--;
}
4.获取栈顶元素
STDataType STTop(ST* pst)
{
assert(pst);
assert(!STEmpty(pst));
return pst->a[pst->top - 1];
}
5.判断栈是否为空
bool STEmpty(ST* pst)
{
assert(pst);
/*if (pst->top == 0)
{
return true;
}
else
{
return false;
}*/
return pst->top == 0;
}
6.得出栈中元素个数
int STSize(ST* pst)
{
assert(pst);
return pst->top;
}
7.销毁一个栈
void STDestroy(ST* pst)
{
assert(pst);
free(pst->a);
pst->a = NULL;
pst->top = pst->capacity = 0;
}
队列
队列的定义
队列(queue)是只允许在一端进行插入操作,在另一端进行删除操作的线性表,简称“队”。
队列是一种先进先出的线性表。
允许插入的一端称为队尾(rear),允许删除的一端称为队头(front)。
向队列中插入新的数据元素称为入队,新入队的元素就成为了队列的队尾元素。
从队列中删除队头元素称为出队,其后继元素成为新的队头元素。
如何理解队列的概念
当我们在买饭的时候,窗口总会给我们一个票让我们等着被叫号,以此来取饭,先拿票的一定是先取饭的这就对应着队列中的先进先出。
队列的定义
queue<int> q1; // 使用默认的适配器定义队列。
queue<int, vector<int>> q2;//使用特定的适配器定义队列。
queue<int, list<int>> q3;//使用特定的适配器定义队列。
队列的成员函数
函数 | 功能 |
---|---|
empty | 判断队列是否为空 |
size | 返回当前队列元素的个数 |
front | 获取队头元素 |
back | 获取队尾元素 |
push | 队尾入队 |
pop | 队头出队 |
例子: |
#include <iostream>
#include <list>
#include <queue>
using namespace std;
int main()
{
queue<int, list<int>> q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
cout << q.size() << endl; //4
while (!q.empty())
{
cout << q.front() << " ";
q.pop();
}
cout << endl; //1 2 3 4
return 0;
}
队列的实现
// 初始化队列
void InitQueue(SeqQueue& q)
{
q.front = q.rear = 0;
}
// 判断队列是否为空
bool QueueEmpty(SeqQueue& q)
{
if (q.rear == q.front) //队空条件
return true;
else
return false;
}
// 入队
bool EnQueue(SeqQueue& q, ElemType x)
{
if ((q.rear+1) % MaxSize == q.front)
return false; //队列满则报错
q.data[q.rear] = x; //将x插入队尾
q.rear = (q.rear + 1) % MaxSize; //队尾指针后移
return true;
}
// 出队
bool DeQueue(SeqQueue& q, ElemType& x)
{
if (q.rear == q.front)
return false; //队空则报错
x = q.data[q.front];
q.front = (q.front + 1) % MaxSize; //队头指针后移
return true;
}
// 获取队头元素
bool QueueFront(SeqQueue& q, ElemType& x)
{
if (q.rear == q.front)
return false; //队空则报错
x = q.data[q.front];
return true;
}
// 队列中元素的个数
int size(SeqQueue& q)
{
return (q.rear - q.front + MaxSize) % MaxSize;
}