当前位置:网站首页>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;
}
边栏推荐
- 树莓派CSI/USB摄像头使用mjpg实现网页摄像头监控
- Flask框架配置loguru日志庫
- Auto.js入门
- [exercise-2] (UVA 712) s-trees
- Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
- Analysis of protobuf format of real-time barrage and historical barrage at station B
- 2027. Minimum number of operations to convert strings
- 渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
- 【练习-7】Crossword Answers
- D - function (HDU - 6546) girls' competition
猜你喜欢
QT按钮点击切换QLineEdit焦点(含代码)
Openwrt source code generation image
PySide6 信号、槽
Maximum product (greedy)
QT实现窗口渐变消失QPropertyAnimation+进度条
pytorch提取骨架(可微)
1605. Sum the feasible matrix for a given row and column
1013. Divide the array into three parts equal to and
【练习-7】Crossword Answers
Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
随机推荐
Write web games in C language
MySQL grants the user the operation permission of the specified content
渗透测试 ( 1 ) --- 必备 工具、导航
Essai de pénétration (1) - - outils nécessaires, navigation
1903. Maximum odd number in string
Data storage in memory & loading into memory to make the program run
Information security - threat detection engine - common rule engine base performance comparison
Radar equipment (greedy)
Codeforces Round #798 (Div. 2)A~D
【练习-2】(Uva 712) S-Trees (S树)
快速转 TypeScript 指南
滲透測試 ( 1 ) --- 必備 工具、導航
Borg maze (bfs+ minimum spanning tree) (problem solving report)
图图的学习笔记-进程
Truck History
Sword finger offer II 019 Delete at most one character to get a palindrome
Codeforces Round #799 (Div. 4)A~H
pytorch提取骨架(可微)
Understand what is a programming language in a popular way
QT按钮点击切换QLineEdit焦点(含代码)