什么是链表?
顾名思义链表是一种形如铁链状的数据结构,每个锁链可以理解成链表中的一个节点,每一节链表只会记录他的前一节、后一节。
链表的优势
链表的每一解都可以随时打开调整,当频繁进行中间插入、删除一段数据,或对某一段数据进行翻转操作时,链表相对于传统的数据具有很大的操作优势。
链表的劣势
由于链表中的元素只能记录上一个或下一个元素,所以在访问特定节点的过程中需要按链上顺序依次查找,数组则可以直接通过数组下标进行访问。
链表的基本写法
struct Node{//节点
int val;
Node *nxt;//指向下一节点
//如果是双向链表
//Node *prev;//指向上一节点
}*head,N[100010];NULL介绍
在c++中,定义了NULL指针用来表示一个空节点,实际上是一个定义在标准库中值为0的常量。
初试链表
(链表的建立、增加与查询)
思考这个图尝试建立、增加、遍历链表
// Created by DreamMeng on 2025-07-30.
//
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node *next;
}*head,N[1001];
int main (){
int n;
cin>>n;
head = NULL;
for(int i=1;i<=n;i++){
cin>>N[i].val;
N[i].next = NULL;//避免野指针
if(head == NULL){
head = &N[i];
head->next = NULL;//同理避免野指针
}
else {
Node *now;
for(now = head;now->next;now=now->next);//如果判断调钱为now now到时候是空指针,没有办法找到上一个,所以要写now—>next
// 目的是让now等于最后一个节点,然后把最后一个节点的next赋值地址N[i];
now->next = &N[i];
}
}
for(Node *p = head;p;p = p->next){
cout<<p->val<<" ";
}
return 0;
}
想想每次都要从头开始查找,能不能设置一个尾节点指针,每次更新尾节点即可新增
//
// Created by DreamMeng on 2025-07-30.
//
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node *next;
}*head,*tail,N[100010];
int main (){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>N[i].val;
N[i].next = NULL; //避免野指针
if(head == NULL){
head = &N[i];
head->next = tail;
tail = head;
}
else {
tail->next = &N[i];//让tail的下一个指针得到值
tail = tail->next;//更新tail指针
}
}
for(Node *p = head; p != NULL;p=p->next)
cout<<p->val<<" ";
return 0;
}第二种比较成熟的写法
#include<bits/stdc++.h>
using namespace std;
struct Node {
int val;
Node *next;
}*head,*tail,N[10010];
int main(){
int n;
cin>>n;
head = NULL;
for(int i=1;i<=n;i++){
cin>>N[i].val;
N[i].next = NULL;
if(head == NULL){
head = &N[i];
tail = &N[i];
}
else {
tail->next = &N[i];
tail = &N[i];
}
}
for(Node *p = head;p;p=p->next)
cout<<p->val<<" ";
return 0;
}链表的插入与删除操作
这是已有的链表结构,和将要插入的节点now
我们要将now,插入到p节点后面
要解决的问题有:
1.将p的next指向now节点 2.将now的next指向p的next节点(节点q)
但是如果先进行1操作的话p的next节点会丢失,所以调整顺序。
1.先进行2操作:先将now的节点指向p的next节点即节点q。
2.再进行1操作:将p的next节点指向节点now即可完成。
如果想要删除某一节点,比如说是当前刚插入的now节点 那我们可以之间将 p节点的next 指向now节点的next。 在课上的时候,还需要把now节点的next设置为空。因为我们不需要通过now->next这种形式去访问原来now的next节点,原来now的next节点是现在p的next节点了。
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node *next;
}*head,*tail,N[100010];
void insert(Node *p,Node *now){//p节点后增加now节点
now->next = p->next; //先让now的下一位等于p的下一位,保证了p的下一位地址不丢失
p->next = now; //再让p的下一位等于p 搞定
}
void del(Node *p,Node *now){//p节点后删除now节点
p->next = now->next;
now->next = NULL;//这句话一定要加么
}
int main(){
int n;
cin>>n;
head = NULL;
for(int i=1;i<=n;i++){
cin>>N[i].val;
if(head == NULL) {
head = &N[i];
head->next = NULL;
tail = head;
}
else{
tail-> next = &N[i];
tail = tail->next;
}
}
//删除head后面的next
del(head,head->next);
for(Node *p =head;p;p=p->next)
cout<<p->val<<" ";
cout<<" "<<endl;
//插入一个节点
Node ww ={100,NULL};//新建一个Node
//在头节点后插入一个100
insert(head,&ww);
for(Node *p =head;p;p=p->next)
cout<<p->val<<" ";
return 0;
}课后问题:
//问题1:修改x节点内的值为10 函数形式
//问题2:在x的前面插入 y;
//问题3:删除x前面的节点;
//问题4:删除x节点
//问题5;修改x节点为新的y节点
//问题3://删除x前面的节点;
void del1 (Node *x){
if(head->next == x){
head->next = NULL;
head = x;
}
else {
Node *p;
Node *q;
for(q = head,p=q->next;p->next!=x;p=p->next,q=q->next);
q->next = x;
p->next =NULL;
}
}
// //问题4://删除x节点
void del2 (Node *x){
Node *p;
if(x == head){
head = head->next;
x->next = NULL;
}
else if(x == tail){
for(p=head;p->next!=tail;p=p->next);
p->next = NULL;
tail = p;
}
else{
for(p=head;p->next!=x;p=p->next);
p->next = x->next;
}
}
//问题5:修改x节点为新的y节点
void update(Node *x,Node *y){
if(x == head){
y->next = x->next;
head = y;
}
else if(x == tail){
Node *p;
for(p=head;p->next != tail;p=p->next);
p->next = y;
tail = y;
}
else{
Node *p;
for(p=head;p->next != x ; p=p->next);
p->next = y;
y->next =x->next;
}
}环状的单向链表
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node *next;
}*head,*tail,N[10010];
int main(){
int n;
cin>>n;
head = tail = NULL;
for(int i=1;i<=n;i++){
cin>>N[i].val;
N[i].next = NULL;
if(head == NULL)
head = tail = &N[i];
else{
tail->next = &N[i];
tail = &N[i];
}
}
tail->next = head;
int i=1;
for(Node *p =head;i<=n*2;p=p->next,i++)
cout<<p->val<<" ";
return 0;
}例题:B3631 单向链表
基础写法 20 pts
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node *next;
}*head,N[100010];
int main(){
int n,cnt=1;
cin>>n;
//初始只有1
N[0].val=1;
N[0].next = NULL;
head = &N[0];
while(n--){
int ope;
cin>>ope;
switch(ope){
case 1:{
int x,y;
cin>>x>>N[++cnt].val;
N[cnt].next = NULL;
for(Node *p = head; p;p=p->next){
if(p->val == x){
N[cnt].next = p->next;
p->next = &N[cnt];
break;
}
}
break;
}
case 2:{
int x;
cin>>x;
Node *p;
for(p =head; p;p=p->next){
if(p->val == x){
cout<<p->next->val<<endl;
break;
}
}
if(p == NULL)
cout<<0<<endl;
break;
}
case 3:{
int x;
cin>>x;
for(Node *p =head;p;p=p->next){
if(p->val==x){
//注意这里是删除x后面的元素,不是
p->next= p->next->next;
p->next= NULL;
break;
}
}
break;
}
}
}
return 0;
}
100pts
因为题中说每个数都不相同
根据这点进行优化
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node *next;
}*head,N[1000010];
int main(){
int n,cnt=1;
cin>>n;
//初始只有1
N[1].val=1;
N[1].next = NULL;
head = &N[1];
while(n--){
int ope;
cin>>ope;
switch(ope){
case 1:{
int x,y;
cin>>x>>y;
N[y].val=y;//这里修改成
N[y].next = N[x].next;
N[x].next = &N[y];
break;
}
case 2:{
int x;
cin>>x;
if(N[x].next == NULL)
cout<<0<<endl;
else
cout<<N[x].next->val<<endl;
break;
}
case 3:{
int x;
cin>>x;
N[x].next= N[x].next->next;
break;
}
}
}
return 0;
}



