#include <iostream>
#include <stdio.h>
using namespace std;
struct DoubleList
{
int data;
DoubleList *pre;
DoubleList *next;
};
//双链表的建立
DoubldeList *CreatList()
{
DoubleList *head=new DoubleList();//新建头结点
DoubleList *p=head;
int cycle=1;//标记输入是否停止
while(cycle)
{
printf("\nPlease input node key:");
int x;
scanf("%d",&x);
if(x!=0)
{
DoubleList *s=new DoubleList();//新建结点
s->data=x;
printf("the input value is:%d",s->data);
p->next=s;
s->pre=p;
p=s;
}
else
cycle=0;
}
head->pre=NULL;
p->next=NULL;//尾结点的next置空
return head;
}
//双链表中删除某一个节点
DoubleList *DeleteList(DoubleList *head,int num)
{
DoubleList *p=head;
//查找删除的结点
while(p->data!=num&&p->next!=NULL)
{
p=p->next;
}
if(p->data==num)//查找到了待删除的结点
{
if(p==head)//待删除的结点为头结点
{
head=head->next;
head->pre=NULL;
delete p;
}
else if(p->next==NULL)//待删除的结点为尾结点
{
p->pre->next=NULL;
delete p;
}
else//待删除节点为中间节点
{
p->next->pre=p->pre;
p->pre->next=p->next;
}
}
else
printf("\n %d could not been found in list.",&num);
return head;
}
//双链表中插入某一值num
DoubleList *InsertList(DoubleList *head,int num)
{
DoubleList *p0=new DoubleList();
p0->data=num;
DoubleList *p=head;
//查找插入位置
while(p->data<num&&p->next!=NULL)
p=p->next;
if(p->data==num)//查找到了要插入的位置
{
if(p==head)//插入到头结点之前
{
p0->next=p;
p->pre=p0;
head=p0;
}
else//插入到中间节点
{
p0->next=p1;
p0->pre=p1->pre;
p1->pre->next=p0;
p1->pre=p0;
}
}
else
{
p1->next=p0;
p0->pre=p1;
p0->next=NULL;
}
return head;
}
双链表的建立,删除及插入
最新推荐文章于 2023-07-28 22:19:22 发布