单链表
1.插入时判断是否为结尾
2.中途插入时,先连接后面
temp->next=pre->next;
//后连前面
pre->next=temp;
双向链表
1.带头结点的双链表当head->next为NULL时链表为空,
2.不带头结点的双链表当head为NULL时链表为空。
栈
1.头文件
#include <stack>
stack<int> s;
2.操作
s.empty(); //如果栈为空则返回true, 否则返回false;
s.size(); //返回栈中元素的个数
s.top(); //返回栈顶元素, 但不删除该元素
s.pop(); //弹出栈顶元素, 但不返回其值
s.push(i); //将元素压入栈顶
队列
- 头文件
#inlcude<queue>
queue<int> q;
- 操作
q.empty() 如果队列为空返回true,否则返回false
q.size() 返回队列中元素的个数
q.pop() 删除队列首元素但不返回其值
q.front() 返回队首元素的值,但不删除该元素
q.back() 返回队列尾元素的值,但不删除该元素
q.push() 在队尾压入新元素
- 循环队列:用 数组+取模 实现
堆
- 头文件
#inlcude<queue>
queue<int> q;
- 基础
//对于基础类型 默认是大顶堆
priority_queue<int> s;
//等同于 priority_queue<int, vector<int>, less<int> > s;
priority_queue<int, vector<int>, greater<int> > ss;
//小顶堆
priority_queue<string> sss;
- 自定义
#include <iostream>
#include <queue>
using namespace std;
//方法1
struct tmp1 //运算符重载<
{
int x;
tmp1(int a) {x = a;}
bool operator<(const tmp1& a) const
{
return x < a.x; //大顶堆
}
};
//方法2
struct tmp2 //重写仿函数
{
bool operator() (tmp1 a, tmp1 b)
{
return a.x < b.x; //大顶堆
}
};
int main()
{
tmp1 a(1);
tmp1 b(2);
tmp1 c(3);
priority_queue<tmp1> d;
d.push(b);
d.push(c);
d.push(a);
while (!d.empty())
{
cout << d.top().x << '\n';
d.pop();
}
cout << endl;
priority_queue<tmp1, vector<tmp1>, tmp2> f;
f.push(c);
f.push(b);
f.push(a);
while (!f.empty())
{
cout << f.top().x << '\n';
f.pop();
}
}
5万+

被折叠的 条评论
为什么被折叠?



