当前位置:网站首页>双向链表—全部操作
双向链表—全部操作
2022-07-06 09:27:00 【帅帅气气的黑猫警长】
初始化,尾插(头插类似,相当于前一位的尾插),遍历,插入,删除,判空
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct dnode{ //定义数据类型
int data;
struct dnode *prior,*next; //双向链表有两个指针!
}dnode,*dlinklist;
bool initdlinklist(dlinklist &l) //初始化双向链表
{
l=(dnode *)malloc(sizeof(dnode));
if(l==NULL)
return 0;
l->prior=NULL;
l->next=NULL; //头指针和尾指针都置null
return 1;
}
bool empty(dlinklist l) //判断链表非空
{
if(l->next==NULL)
return 1;
else
return 0;
}
bool insertdlinklist(dlinklist &l,int i,int e) //插入操作
{
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) //健壮代码
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) //删除操作
{
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) //健壮代码
return 0;
e=p->data;
if(p->prior!=NULL)
p->prior->next=p->next;
if(p->next!=NULL) //判断是否为最后
p->next->prior=p->prior;
free(p);
return 1;
}
dlinklist dlinklist_tailinsert(dlinklist &l) //尾插
{
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) //遍历
{
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;
}
边栏推荐
猜你喜欢

信息安全-史诗级漏洞Log4j的漏洞机理和防范措施

程序员的你,有哪些炫技的代码写法?

STM32 how to use stlink download program: light LED running light (Library version)

Write web games in C language

Nodejs+vue网上鲜花店销售信息系统express+mysql

B - 代码派对(女生赛)

【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)

Borg Maze (BFS+最小生成树)(解题报告)

差分(一维,二维,三维) 蓝桥杯三体攻击

Determine the Photo Position
随机推荐
STM32 learning record: LED light flashes (register version)
[exercise-6] (UVA 725) division = = violence
F - birthday cake (Shandong race)
New to redis
1010 things that college students majoring in it must do before graduation
渗透测试 ( 1 ) --- 必备 工具、导航
C语言是低级和高级的分水岭
区间和------离散化
China's earthwork tire market trend report, technical dynamic innovation and market forecast
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
Ball Dropping
Borg maze (bfs+ minimum spanning tree) (problem solving report)
【练习4-1】Cake Distribution(分配蛋糕)
信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
Basic Q & A of introductory C language
China potato slicer market trend report, technical dynamic innovation and market forecast
Penetration test (8) -- official document of burp Suite Pro
B - 代码派对(女生赛)
MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’
nodejs爬虫