# C/C++ 双链表的实现

9 篇文章 0 订阅

#include<iostream>
#include <cstdio>
#include<stdlib.h>
using namespace std;
//双链表
typedef struct DLNode{
int data;
struct DLNode *prior;
struct DLNode *next;

//双链表的初始化，也就是创建一个新的节点
{
DL = new DLNode();//创建头结点
DL->data = -1;
DL->prior = NULL;
DL->next = NULL;
return DL;
}

//头插法创建
{
int val;
cin>>val;

while(val!=9999)
{
temp = new DLNode();//新节点
temp->data  = val;

temp->prior = DL;
cin>>val;
}

}
//尾插法建立单链表
{
int val;
cin>>val;
do
{
temp = new DLNode();
temp->data = val;
temp->next = NULL;

rear->next = temp;
temp->prior = rear;
rear = temp;
cin>>val;

}while(val != 9999);
return DL;
}
//双链表的长度
{
int length = 0;
while(p != NULL)
{
length++;
p = p->next;
}
return length;
}
//打印所以元素
{
int foot = 0;
int flag = 0;

s = DL;

printf("DList Information\n");
while (flag == 0)
{
if (s->next == NULL)
{ //当遍历到最后一个元素时退出
flag = 1;
}
printf(" |- List %d : %d\n", foot++, s->data);
s = s->next;
}
}

//双链表的删除,按查找位置删除
{
int len = 0;
return false;
while(len<i)
{
p = p->next;
len++;
}
p->prior->next = p->next;
p->next->prior = p->prior;
delete(p);
return true;
}
//双链表的插入，按给定位置之后插入
{
int val,len = 0;
return false;
while(len<i)
{
p = p->next;
len++;
}
s =  new DLNode();
cout<<"输入插入节点的值：";
cin>>val;
s->data = val;
s->next = p->next;
p->next->prior = s;
s->prior = p;
p->next = s;
return true;
}
int main()
{
cout<<"删除1号链表节点："<<endl;
Delete(DL,1);
Insert(DL,1);
}


• 0
点赞
• 0
评论
• 1
收藏
• 一键三连
• 扫一扫，分享海报

05-10
07-10 2万+

09-24 384
10-23 250
08-23 5562
04-01 110
11-30 220
03-07 192
08-10 3006
07-25 2102
10-01 362
05-26 1407
04-04 4651
11-22 321
01-03 7634