上机考试常用数据结构及算法
1.栈stack(先进后出)
#include<Stack>//头函数
Stack<type> s;
s.size();
s.top();
s.pop();
s.push();
s.empty();//return a bool value;
2.队列queue(先进先出)
#include<queue>
queue<type> q;
q.size();
q.front();// return the primary element of queue
q.pop();
q.push(x);
q.empty();
3.pair<>
pari<type1,type2>Q;//头函数另说;
make_pair(type1 value1,type2 value2);
Q.first;
Q.second;
4.vector
#include<vector>
vector<type> V;
V.size();
V.push_back();
V.pop_back();
V.begin();//return a iterator pointing to begin of the vector
V.end();// return next iterator of the last element
V.insert(p,x)//insert x in the location of p;
V.erase(p);//delete the element of the location p;
V.clear();//clear all elements;
5.list
#include<list>
list<type>l;
l.size();
l.begin();
l.push_front();// add an element to the begin of list
l.push_back();--------------------------last
l.pop_front();
l.pop_back();
l.insert(p,x);
l.erase(p);
l.clear();
for(list<type>::iterator it=l.begin();it!=l.end();it++)//forum
6.Search
#include<algorithm>
binary_search();
lower_bound(a,a+n,x)//return the first iterator that pointing the element no less than x;
//the type of a is vary;
uper_bound();//using such as l_b;
7.递归与分治
#include<iostream>
//typical example of recursion:factorial
int factorial(int n){
if(n==1) return 1;
return n*factorial(n-1);
}
//find maximum achieve by Divide-and-Conquer
int findMaximum(type A,begin l, end r){
m=(l+r)/2;
if(l==r-1)
return A[l];
else{
u=findMaximum(A,l,m)//divide
v=findMaximum(A,m,r)
x=max(u,v)//conquer
}
return x;
}
8.二叉搜索树
#include<set>//set is sorted by value of element ,no element errepeation and ascending order
#include<map>
set<type>s;
s.size();
s.clear();
s.begin();
s.end();
s.insert(key);
s.earse(key);
s.find(key);
map<keyType key,vlaueType value>m;//no repeated key ,and key is the standard of sortion
//type is vary
m.size();
m.clear();
m.begin();
m.end();
m.insert((key,value));//pair can be used here
m.erase(key);
m.find(key);
9.priority_queue
#include<queue>
priority_queue<int>PQ;//the top element have the highest priority,default to ascending order
//usage is same as queue
10BFS and DFS
在这里插入代码片
暂时先写到这