(一)单向链表
例题1:B3630排队记录
问题:每个人只知道自己后面的人是谁,从前向后输出队列中每个小朋友的编号。
#include<iostream>
#include<cstdio>
using namespace std;
int nxt[1000010],head,n;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>nxt[i];
}
cin>>head;
int now=head;
// for(int i=now;i!=0;i=nxt[i]){
// cout<<i<<" ";
// }
while(now!=0){
cout<<now<<" ";
now = nxt[now];
}
return 0;
}1、建立链表:
for(int i=1;i!=0;i=nxt[i]) cout<<i<<" ";2、在x后面插入y
void insert(int x,int y){
nxt[y]=nxt[x];
nxt[x]=y;
}3、删除x后面的元素
void delete(int x){
nxt[x]=nxt[nxt[x]];
}例题2:B3631 单向链表
#include<iostream>
#include<cstdio>
using namespace std;
int nxt[1000010],q;
void insert(int x,int y){ //在x后插入y
nxt[y]=nxt[x];
nxt[x]=y;
}
void remove(int x){ //删除x后面的元素
nxt[x] = nxt[nxt[x]] ;
}
int main(){
cin>>q;
while(q--){
int opr,x,y;
cin>>opr;
if(opr==1){
cin>>x>>y;
insert(x,y);
}else if(opr==2){
cin>>x;
cout<<nxt[x]<<endl;
}else{
cin>>x;
remove(x);
}
}
return 0;
}注意:
单向链表采用数组存放数据,由于链表数据的物理位置不一定相邻,会造成大量空间的浪费,改进:采用结构体存储。
struct node{
int val,nxt;
};(二)双向链表
struct node{
int val,pre,nxt;
};每次操作前,都需要先知道一个元素在表中的编号。
int find(int x){
int now=1;
while(now && s[now].val!=x)
now = s[now].nxt;
return now;
}1、将元素y插到元素x后面;即插入第tot+1个元素
void ins_back(int x,int y){
int now = find(x);
s[++tot]=node(y, now, s[now].nxt);//分别表示元素、前驱、后继
s[s[now].nxt].pre = tot;//tot表示s[]数组中保存节点使用了多少节点位置。
s[now].nxt = tot;
}2、将元素y插到元素x前面;
void ins_front(int x,int y){
int now = find(x);
s[++tot] = node(y,s[now].pre,now);
s[s[now].pre].nxt = tot;
s[now].pre=tot;
}3、查询元素x前的元素;
int ask_back(int x){
int now =find(x);
return s[s[now].nxt].val;
}4、查询元素x后的元素;
int ask_front(int x){
int now = find(x);
return s[s[now].pre].val;
}5、删除元素x
void delete(int x){
int now = find(x);
s[s[now].pre].nxt = s[now].nxt;
s[s[now].nxt].pre = s[now].pre;
}例题3:P1160 队列安排
#include<bits/stdc++.h>
using namespace std;
struct node{
int val,pre,nxt;
node(int val1=0,int pre1=0,int nxt1=0){
val = val1;
pre = pre1;
nxt = nxt1;
}
}s[100005];
int n,m,tot=0,ind[100005]={0};
//1、将元素y插到元素x后面;即插入第tot+1个元素
void ins_back(int x,int y){
// int now = find(x);
int now = ind[x];
s[++tot]=node(y, now, s[now].nxt);//分别表示元素、前驱、后继
s[s[now].nxt].pre = tot;//tot表示s[]数组中保存节点使用了多少节点位置。
s[now].nxt = tot;
ind[y]=tot;
}
//2、将元素y插到元素x前面;
void ins_front(int x,int y){
// int now = find(x);
int now = ind[x];
s[++tot] = node(y,s[now].pre,now);
s[s[now].pre].nxt = tot;
s[now].pre=tot;
ind[y]=tot;
}
//3、删除元素x
void del(int x){
//int now = find(x);
int now = ind[x];
s[s[now].pre].nxt = s[now].nxt;
s[s[now].nxt].pre = s[now].pre;
ind[x]=0;
}
int main(){
int x,k,p,now;
cin>>n;
s[0]=node();//代码技巧:令0恒为最左边的节点,有利于之后处理问题
ins_back(0,1) ;
for(int i=2;i<=n;i++){
cin>>k>>p;
p?ins_back(k,i):ins_front(k,i);
}
cin>>m;
for(int i=1;i<=m;i++){
cin>>x;
if(ind[x])del(x);
}
now = s[0].nxt;
while(now){
cout<<s[now].val<<" ";
now = s[now].nxt;
}
return 0;
}例题4:P1996约瑟夫问题
#include<bits/stdc++.h>
using namespace std;
list<int>a;
int n,m,cnt=0;
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
a.push_back(i);
}
list<int>::iterator it,now;
it = a.begin();
while(!a.empty()){
cnt++;
now = it;//备份一个带删除的指针
if(++it ==a.end())it = a.begin();//遍历下一个,循环链表
if(cnt==m){//数到第m个
cout<<*now<<" ";
a.erase(now) ;
cnt=0;
}
}
return 0;
}#include<bits/stdc++.h>
using namespace std;
int n,m,nxt[110];
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
nxt[i]=i+1;
}
nxt[n]=1;
int now = n;
for(int t=1;t<=n;t++){
for(int j=1;j<=m-1;j++){
now = nxt[now];
}
cout<<nxt[now]<<" ";
nxt[now]=nxt[nxt[now]];
}
return 0;
}