当前位置:网站首页>Bidirectional linked list - all operations
Bidirectional linked list - all operations
2022-07-06 16:13:00 【Handsome black cat Sheriff】
initialization , Tail insertion ( Head insert similar , It is equivalent to the last insertion of the previous digit ), Traverse , Insert , Delete , Sentenced to empty
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct dnode{ // Define data types
int data;
struct dnode *prior,*next; // Double linked list has two pointers !
}dnode,*dlinklist;
bool initdlinklist(dlinklist &l) // Initializing a double linked list
{
l=(dnode *)malloc(sizeof(dnode));
if(l==NULL)
return 0;
l->prior=NULL;
l->next=NULL; // Both the head pointer and the tail pointer are set null
return 1;
}
bool empty(dlinklist l) // Judge that the linked list is not empty
{
if(l->next==NULL)
return 1;
else
return 0;
}
bool insertdlinklist(dlinklist &l,int i,int e) // The insert
{
int j=0;
dnode *p,*s;
p=(dnode *)malloc(sizeof(dnode));
p=l;
while(p->next!=NULL&&j<i-1)
{
j++;
p=p->next;
}
if(p==NULL||j!=i-1) // Robust code
return 0;
s=(dnode *)malloc(sizeof(dnode));
s->data=e;
s->next=p->next;
if(p->next!=NULL)
p->next->prior=s;
p->next=s;
s->prior=p;
return 1;
}
bool deletedlinklist(dlinklist &l,int i,int &e) // Delete operation
{
int j=0;
dnode *p;
p=(dnode *)malloc(sizeof(dnode));
p=l;
while(p->next!=NULL&&j<i)
{
j++;
p=p->next;
}
if(p==NULL||j!=i) // Robust code
return 0;
e=p->data;
if(p->prior!=NULL)
p->prior->next=p->next;
if(p->next!=NULL) // Judge whether it is the last
p->next->prior=p->prior;
free(p);
return 1;
}
dlinklist dlinklist_tailinsert(dlinklist &l) // Tail insertion
{
int x;
dnode *p,*s;
p=(dnode *)malloc(sizeof(dnode));
p=l;
cin>>x;
while(x!=0)
{
s=(dnode *)malloc(sizeof(dnode));
s->data=x;
s->next=NULL;
p->next=s;
s->prior=p;
p=s;
cin>>x;
}
return l;
}
void lookdlinklist(dlinklist l) // Traverse
{
dnode *p;
int j=1;
p=(dnode *)malloc(sizeof(dnode));
p=l->next;
while(p!=NULL)
{
cout<<"number "<<j<<" is "<<p->data<<" , ";
j++;
p=p->next;
}
cout<<endl;
}
int main() {
dlinklist l;
if(initdlinklist(l))
cout<<"initdlinklist succeed"<<endl;
if(empty(l))
cout<<"dlinklist is empty!"<<endl;
else cout<<"dlinklist not empty!"<<endl;
dlinklist_tailinsert(l);
if(empty(l))
cout<<"empty"<<endl;
else cout<<"not empty"<<endl;
lookdlinklist(l);
if(insertdlinklist(l,2,88)&&(insertdlinklist(l,4,99)))
cout<<"insertdlinklist succeed\n";
else cout<<"insertdlinklist faild\n";
lookdlinklist(l);
int e=0;
if(deletedlinklist(l,2,e))
cout<<"deletedlinklist succeed,delete element is "<<e<<endl;
else cout<<"delete error"<<endl;
lookdlinklist(l);
cout << "over\n";
return 0;
}
边栏推荐
- Codeforces Round #801 (Div. 2)A~C
- Raspberry pie csi/usb camera uses mjpg to realize web camera monitoring
- 树莓派4B安装opencv3.4.0
- (POJ - 3258) River hopper (two points)
- Luogu P1102 A-B number pair (dichotomy, map, double pointer)
- Codeforces Round #798 (Div. 2)A~D
- [exercise-7] (UVA 10976) fractions again?! (fraction split)
- [exercise-3] (UVA 442) matrix chain multiplication
- Penetration test (7) -- vulnerability scanning tool Nessus
- 【练习-7】Crossword Answers
猜你喜欢
Codeforces Round #801 (Div. 2)A~C
Frida hook so layer, protobuf data analysis
X-forwarded-for details, how to get the client IP
MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’
1005. Maximized array sum after K negations
[exercise-4] (UVA 11988) broken keyboard = = (linked list)
Pytorch extract skeleton (differentiable)
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
B - Code Party (girls' competition)
随机推荐
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
Frida hook so layer, protobuf data analysis
Penetration test (8) -- official document of burp Suite Pro
Penetration test (3) -- Metasploit framework (MSF)
Essai de pénétration (1) - - outils nécessaires, navigation
AcWing:第58场周赛
The concept of C language array
[exercise-5] (UVA 839) not so mobile (balance)
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
读取和保存zarr文件
Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
[exercise-3] (UVA 442) matrix chain multiplication
Interesting drink
Codeforces Round #803 (Div. 2)A~C
Pytorch extract skeleton (differentiable)
b站 实时弹幕和历史弹幕 Protobuf 格式解析
Write web games in C language
[exercise-7] (UVA 10976) fractions again?! (fraction split)
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集