当前位置:网站首页>Bidirectional linked list simple template (pointer version)
Bidirectional linked list simple template (pointer version)
2022-06-11 07:30:00 【Master. Yi】
Freshman year … And began to lay the foundation for the program ( Dream back to the first day of junior high school )
I found that I haven't used the pointer for the problems that I have typed for several years , Let's write the linked list of the pointer version … It turned out to be disgusting
At the beginning of writing, I didn't insert a minimax value at the beginning and end , Then, when inserting and deleting, it is necessary to judge a lot of null pointers at the beginning and end …
Then it suddenly occurred to me to do Splay When the method … Insert a minimax … Instant harmony 100 times ..
#include<cstdio>
struct link{
int id,val;
link *pre,*nxt;
link(int id=0,int val=0,link *pre=0,link *nxt=0):id(id),val(val),pre(pre),nxt(nxt){
}
};
void insert(link *p,int idx,int w){
link *q;
while((q=p->nxt)->id<idx) p=q;
if(q->id==idx) q->val=w;
else{
link *t=new link(idx,w,p,q);
p->nxt=q->pre=t;
}
}
void del_by_id(link *p,int idx){
link *q;
while((q=p->nxt)->id<idx) p=q;
if(q->id==idx){
p->nxt=q->nxt,q->nxt->pre=p;
delete q;
}
}
void Print(link *p){
for(p=p->nxt;p->nxt!=NULL;p=p->nxt) printf("id: %d val: %d\n",p->id,p->val);
}
void DelList(link *p){
for(link *q;p!=NULL;p=q) {
q=p->nxt; delete p;}
}
int main()
{
link *head=new link(-1e9,0,0,0),*tail=new link(1e9,0,head,0);
head->nxt=tail;
insert(head,1,10);
insert(head,5,50);
insert(head,3,30);
insert(head,-1,-10);
del_by_id(head,3);
del_by_id(head,4);
Print(head);
DelList(head);
}
边栏推荐
- [Oracle database] mammy tutorial day02 use of database management tool sqlplus
- Regular Expression Matching
- 【AtCoder1998】Stamp Rally(整体二分+并查集)
- 2022年熔化焊接与热切割考试练习题及答案
- MFC debugger OutputDebugString override
- A case in which the MySQL administrator password cannot take effect
- MS office level II wrong question record [9]
- Classification of MNIST datasets with keras
- 【CF#262 (Div. 2)】 A. Vasya and Socks
- Seata的几种事务模式
猜你喜欢

C language function stack frame

The rotation of the earth and the moon (II)

Classification of MNIST datasets with keras

RTMP protocol
![[analysis of STL source code] summary notes (3): vector introduction](/img/70/faa40c273f6b3a6b124fb870058489.jpg)
[analysis of STL source code] summary notes (3): vector introduction
![[STL source code analysis] summary notes (5): a good helper for understanding iterators --list](/img/a7/c54bfb6a03c04e4ffeafdfcf8cedc2.jpg)
[STL source code analysis] summary notes (5): a good helper for understanding iterators --list

多线程复习总结之解析Volatile关键字
![[Oracle database] mammy tutorial day04 Sorting Query](/img/79/9db26aa2d9dbb5514427edf03004f4.png)
[Oracle database] mammy tutorial day04 Sorting Query

Summary of classic interview questions

JVM Learning record (7) - - class Loading Process and parent delegation Model
随机推荐
big. Js-- use / instance
【AtCoder1980】Mysterious Light(数学模拟)
【Oracle 数据库】奶妈式教程day02 数据库管理工具SQLPLUS的使用
Sdl-2 thread logic
P3811 [template] multiplicative inverse
20200803 T3 my friends [divide and conquer NTT optimization recursion]
Use definite integral to calculate triangle area
JVM tuning
QT interface nested movement based on qscrollarea
May 30-June 5, 2022 AI industry weekly (issue 100): three years
I/o multiplexing - select/poll/epoll
JVM learning record (VII) -- class loading process and parental delegation model
【CodeForces1019E】Raining season(边分治+斜率优化)
C language to write a calculator calculation logic
【HDU6357】Hills And Valleys(DP)
黑群晖DSM7.0.1物理机安装教程
Gobang interface of mobile console (C language)
Simple configuration of vscade
Import on CSDN MD file
SQLZOO刷题记录-3