为了快速记忆,以及遗忘速查,也为了减少碎片化记忆(这是什么鬼?。该板块的代码样例为完整版,本着复制就能运行的态度,大概会持续更新吧

创建链表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<bits/stdc++.h>
using namespace std;
typedef struct node *listnode;
struct node
{
int data;
listnode next;
};
listnode create(int a[]){
listnode p,pre,head;
head=new node;
head->next=nullptr;
pre=head;
for(int i=0;i<5;i++){
p=new node;
p->data=a[i];
p->next=nullptr;
pre->next=p;
pre=p;
}
return head;
}

int main(){
int a[]={3,2,5,8,7};
listnode k=create(a);
k=k->next;
while(k!=nullptr){
cout<<k->data;
k=k->next;
}
system("pause");
return 0;
}

查找元素

1
2
3
4
5
6
7
8
9
10
11
int search(listnode head,int x){
int count=0;//计数器
listnode p=head->next;
while(p!=nullptr){
if(p->data==x){
count++;
}
p=p->next;
}
return count;
}

插入元素

1
2
3
4
5
6
7
8
9
10
11
void insert(listnode head, int pos, int x)
{
listnode p=head;
for(int i=0;i<pos-1;i++){
p=p->next;
} //c插入到位置的前一个节点,就是pos-1哒
listnode q=new node;
q->data=x;
q->next=p->next;
p->next=q;
}

删除元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void del(listnode head,int x){
listnode p=head->next;//p从第一个节点开始枚举
listnode pre=head;//pre始终保存前驱节点的指针
while(p!=nullptr){
if(p->data==x){
pre->next=p->next;
delete(p);
p=pre->next;
}else{//如果不是x;吧pre和p都后移一位
pre=p;
p=p->next;
}
}
}