当前位置:网站首页>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 #799 (Div. 4)A~H
- 日期加1天
- 【练习-2】(Uva 712) S-Trees (S树)
- JS call camera
- Gartner: five suggestions on best practices for zero trust network access
- Pyside6 signal, slot
- C language is the watershed between low-level and high-level
- Auto. Getting started with JS
- Interval sum ----- discretization
- Opencv learning log 29 -- gamma correction
猜你喜欢

Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools

力扣:第81场双周赛

【练习-7】Crossword Answers

X-forwarded-for details, how to get the client IP

QT实现圆角窗口

(POJ - 3685) matrix (two sets and two parts)

Penetration test (1) -- necessary tools, navigation

Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures

Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B

1855. Maximum distance of subscript alignment
随机推荐
2027. Minimum number of operations to convert strings
Pytorch extract skeleton (differentiable)
第 300 场周赛 - 力扣(LeetCode)
Opencv learning log 26 -- detect circular holes and mark them
Penetration test (8) -- official document of burp Suite Pro
Determine the Photo Position
Flag framework configures loguru logstore
Understand what is a programming language in a popular way
JS call camera
Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
CEP used by Flink
Vs2019 initial use
[exercise-2] (UVA 712) s-trees
(POJ - 2739) sum of constructive prime numbers (ruler or two points)
F - birthday cake (Shandong race)
Frida hook so layer, protobuf data analysis
【练习-3】(Uva 442)Matrix Chain Multiplication(矩阵链乘)
Codeforces Round #802(Div. 2)A~D
X-forwarded-for details, how to get the client IP
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具