当前位置:网站首页>双向链表—全部操作
双向链表—全部操作
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;
}
边栏推荐
- 【练习-7】(Uva 10976)Fractions Again?!(分数拆分)
- Auto.js入门
- Opencv learning log 31 -- background difference
- 0-1 knapsack problem (I)
- 7-1 understand everything (20 points)
- [exercise-2] (UVA 712) s-trees
- 【练习-6】(Uva 725)Division(除法)== 暴力
- [exercise -10] unread messages
- CEP used by Flink
- Research Report on market supply and demand and strategy of geosynthetics industry in China
猜你喜欢
滲透測試 ( 1 ) --- 必備 工具、導航
Borg Maze (BFS+最小生成树)(解题报告)
Vs2019 initial use
Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
Gartner:关于零信任网络访问最佳实践的五个建议
程序员的你,有哪些炫技的代码写法?
b站 实时弹幕和历史弹幕 Protobuf 格式解析
Borg maze (bfs+ minimum spanning tree) (problem solving report)
渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
基于web的照片数码冲印网站
随机推荐
HDU-6025-Coprime Sequence(女生赛)
C 基本语法
Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
Essai de pénétration (1) - - outils nécessaires, navigation
Penetration test (1) -- necessary tools, navigation
C语言必背代码大全
【练习-10】 Unread Messages(未读消息)
渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
最全编程语言在线 API 文档
X-forwarded-for details, how to get the client IP
初入Redis
Basic Q & A of introductory C language
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
Penetration test (3) -- Metasploit framework (MSF)
信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
【练习-6】(Uva 725)Division(除法)== 暴力
入门C语言基础问答
Auto.js入门
E. Breaking the Wall
对iptables进行常规操作