目录
6.访问或者编辑可变数组时,可以像普通的数组一样使用方括号索引,比如v[10]就可以访问对应的元素,不过和普通数组一样不要越界。
(1)头插法:void headInsert(Node* list,int data);
(2)尾插法:void tailInsert(Node* list,int data);
A.中_前插法:bool midInsert_front(Node* list,int data1,int data2);
B.中_后插法:bool midInsert_behind(Node* list,int data1,int data2);
3.遍历函数:void printList(Node* list);
4.检索函数:bool searchNode(Node* list,int data);
5.删除函数:bool deleteNode(Node* list,int data);
6.交换函数:void exchangeNode(Node* list, int data1, int data2);
7.释放函数:void freeList(Node* list);
一、数组
例15-1:(洛谷P3156)询问学号
#include <iostream>
using namespace std;
int a[2000005];
int main() {
int n, m;
int ask;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= m; i++) {
cin >> ask;
cout << a[ask] << endl;
}
return 0;
}
STL容器的可变长度数组:
1.头文件<vector>
2.vertor<int> v(N,i)
建立一个可变长度数组v,内部元素类型为int,该可变数组最开始有N个元素,每个元素都初始化为i。如果省略i,那么默认值为0,;如果同时省略(N,i),此时这个数组的长度就是0.内部元素类型可以替换成其他的类型,比如double.
3.v.push_back(a)
将元素a插入到数组v的末尾,并增加数组的长度。
4.v.size()
返回数组v的长度
5.v.resize(n,m)
重新调整数组大小为n,如果n比原来的小,则撒删除多余的信息;如果n比原来大,则新增的部分都初始化为m,其中m是可以省略的。
6.访问或者编辑可变数组时,可以像普通的数组一样使用方括号索引,比如v[10]就可以访问对应的元素,不过和普通数组一样不要越界。
本题用可变数组:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m,tmp;
cin >> n >> m;
vector<int> stu;
for (int i = 0; i < n; i++) {
cin >> tmp;
stu.push_back(tmp);
}
for (int i = 0; i < m; i++) {
cin >> tmp;
cout << stu[tmp - 1] << endl;
}
return 0;
}
例15-2:(洛谷P3613)寄包柜:
明确此题问什么要选择可变数组:
此题建立普通二维数组,需要定义一个大小为10的5次方*10的5次方的Int数组,大小占用了4*10^5*10^5字节约为40GB,显然会超出内存限制。仍可以使用vector来解决。
vector其他的用法:
1.vector<int>::iterator it
定义一个名字叫叫做it的迭代器。
2.v.begin()
返回数组v的首元素(也就是v[0])的指针(迭代器)
3.v.end()
返回数组v首元素末尾的下一个元素的指针(迭代器),这个指针有点类似于空指针,不指向任何元素。
迭代器:
除了使用数组下标,还可以通过“迭代器”来访问数组中的元素。
迭代器有点类似于指针(虽然不能完全画等号),这里的it就可以认为是一个指向vector中的元素的指针(下文如果再提到STL元素的指针,一般都指迭代器),it可以++或者--变成前一个或者后一个元素的指针,也能和指针一样用*it取该指针中的元素。
由于迭代器和指针在表现方式上很接近,所以v[i]和*(v.begin()+i)是一样的,都是取对应元素的值。
本题代码:
#include <iostream>
using namespace std;
#include <vector>
int main() {
int n, q;
cin >> n >> q;
vector< vector<int> > locker(n + 1);
int method;
int i, j,k;
for (int h = 1; h <= q; h++) {
cin >> method;
cin >> i >> j;
if (method == 1) {
cin >> k;
if (locker[i].size() < j + 1) {
locker[i].resize(j + 1);
}
locker[i][j]=k;
}
else {
cout<< locker[i][j]<<endl;
}
}
return 0;
}
定义一个一维定长,二维不定长的可变数组,即比如由10个可变数组组成的一个二维数组:
vector<int> v[10]
定义一个二维都不定长的二维数组,可以:
vector< vector<int> > v
注意尖括号里的空格,以免被认为成移位运算符导致编译错误。
二、栈
栈的写法:
1.手写:
实用版:
#define MAXN 1000
int p=0;
int stack[MAXN];
void push(int x){
stack[p]=x;
p++;
}
void pop(){
p--;
}
int top(){
return stack[p-1];
}
工程版:
#define MAXN 1000
int p=0;
void push(int x){
if(p>=MAXN){
printf("Stack overflow.");
}else{
stack[p]=x;
p++;
}
}
void pop(){
if(p==0) printf("Stack is empty.");
else p--;
}
int top(){
if(p==0){
printf("Stack is empty.");
return -1;
}else{
return stack[p-1];
}
}
2.STL提供的stack容器版:
1.头文件:<stack>
2.stack<int>s 建立一个栈,其内部元素类型是int
3.s.push(a)
4.s.pop()
5.s.top()
6.s.size() 查询s的元素个数
7.s.empty() 查询s是否为空
8.弊端:不开O2优化就速度慢,追求速度不适用。
例15-6:(洛谷P1449)后缀表达式:
#include <iostream>
using namespace std;
#include <stack>
stack<int> s;
int main() {
char ch;
int m=0;
s.push(0);
int last_top;
while ((ch = getchar()) != '@') {
if (ch >= '0' && ch <= '9') {
m = m * 10 + (ch - '0');
}
else if (ch == '.') {
s.push(m);
m = 0;
continue;
}
else {
switch (ch) {
case '+': {
int h = s.top();
s.pop();
last_top = s.top();
s.pop();
s.push(h + last_top);
break;
}
case '-': {
int h = s.top();
s.pop();
last_top = s.top();
s.pop();
s.push(last_top - h);
break;
}
case '*': {
int h = s.top();
s.pop();
last_top = s.top();
s.pop();
s.push(last_top * h);
break;
}
case '/': {
int h = s.top();
s.pop();
last_top = s.top();
s.pop();
s.push(last_top / h);
break;
}
}
}
}
cout << s.top();
return 0;
}
三、队列
FIFO:First In First Out
队列的写法:
1.手写:
实用版:
#define MAXN 1000
int head, tail;
int queue[MAXN];
void pus