源码如下:
#include<iostream>
using namespace std;
typedef struct tnode
{
int data;
struct tnode *next;
struct tnode *pre;
}dnode;
dnode *create()
{
dnode *head,*p,*s;
int x;
int circle=1;
head=(dnode*)malloc(sizeof(dnode));
p=head;
while(circle)
{
printf("\nplease input the data:");
scanf("%d",&x);
if(x!=0)
{
s=(dnode*)malloc(sizeof(dnode));
s->data=x;
printf("\n%d",s->data);
p->next=s;
s->pre=p;
p=s;
}
else
{
circle=0;
}
}
head=head->next;
head->pre=NULL;
p->next=NULL;
printf("\n%d",head->data);
return(head);
}
void print(dnode *head)
{
dnode *p;
p=head;
while(p!=NULL)
{
printf("\n%d",p->data);
p=p->next;
}
}
dnode *del(dnode *head,int num)
{
dnode *p1,*p2;
p1=head;
while(num!=p1->data&&p1->next!=NULL)
{
p1=p1->next;
}
if(num==p1->data)
{
if(p1==head)
{
head=head->next;
head->pre=NULL;
free(p1);
}
else if(p1->next==NULL)
{
p1->pre->next=NULL;
free(p1);
}
else
{
p1->next->pre=p1->pre;
p1->pre->next=p1->next;
}
}
else
printf("\n%d could not been found",num);
return(head);
}
dnode *insert(dnode *head,int num,int m)
{
dnode *p0,*p1,*p2;
p1=head;
p2=head;
p0=(dnode*)malloc(sizeof(dnode));
p0->data=num;
int i;
for(i=0;i<m;i++)
{
p1=p1->next;
}
for(i=0;i<m-1;i++)
{
p2=p2->next;
}
p2->next=p0;
p0->pre=p2;
p0->next=p1;
p1->pre=p0;
return(head);
}
int main()
{
dnode *head;
int n,del_num,insert_num,x;
head=create();
print(head);
cout<<"\nplease input the delete data:";
cin>>del_num;
head=del(head,del_num);
print(head);
cout<<"\nplease input the insert data:";
cin>>insert_num;
cout<<"\nplease input the position:";
cin>>x;
head=insert(head,insert_num,x);
print(head);
return 0;
}
运行结果: