当前位置:网站首页>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;
}
边栏推荐
- Opencv learning log 27 -- chip positioning
- QNetworkAccessManager实现ftp功能总结
- [analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
- 滲透測試 ( 1 ) --- 必備 工具、導航
- 日期加1天
- QT模拟鼠标事件,实现点击双击移动拖拽等
- Pyside6 signal, slot
- AcWing:第58场周赛
- Information security - security professional name | CVE | rce | POC | Vul | 0day
- The "sneaky" new asteroid will pass the earth safely this week: how to watch it
猜你喜欢

Pyside6 signal, slot

Codeforces Round #802(Div. 2)A~D

Penetration test (4) -- detailed explanation of meterpreter command

滲透測試 ( 1 ) --- 必備 工具、導航

1605. Sum the feasible matrix for a given row and column

渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞

第 300 场周赛 - 力扣(LeetCode)
Quick to typescript Guide

Flask框架配置loguru日志庫

Advancedinstaller安装包自定义操作打开文件
随机推荐
Find 3-friendly Integers
树莓派CSI/USB摄像头使用mjpg实现网页摄像头监控
Suffix expression (greed + thinking)
树莓派4B安装opencv3.4.0
Alice and Bob (2021 Niuke summer multi school training camp 1)
Codeforces Round #803 (Div. 2)A~C
1013. Divide the array into three parts equal to and
1689. Ten - the minimum number of binary numbers
计算时间差
B - Code Party (girls' competition)
Penetration test (8) -- official document of burp Suite Pro
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
807. Maintain the urban skyline
MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’
D - function (HDU - 6546) girls' competition
[exercise 4-1] cake distribution
1605. Sum the feasible matrix for a given row and column
1855. Maximum distance of subscript alignment
frida hook so层、protobuf 数据解析
pytorch提取骨架(可微)