还有很多bug没有修复,简单的实现了下
///**********双链表的创建、删除、插入
#include<iostream>
using namespace std;
struct student
{
int data;
student *next;
student *pre;
};
student *create(int n)
{
student *head,*p,*s;
int x;
int i=1;//从1开始计算
head=(student*)malloc(sizeof(student));
p=head;
while(i++<=n)
{
s=(student*)malloc(sizeof(student));
cin>>x;
s->data=x;
p->next=s;
s->pre=p;
p=s;
}
head=head->next;
head->pre=NULL;
p->next=NULL;
return head;
}
void print(student* head)
{
student *p;
p=head;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
student *del(student *head,int num)
{
student *p1;
p1=head;
while(p1->data!=num&&p1->next!=NULL)
p1=p1->next;//找到了结点
if(p1->data==num)//等于num值
{
if(p1==head)//如果是头结点
{
head=head->next;
head->pre=NULL;
free(p1);
}
else if(p1->next==NULL)//末结点
{
p1->pre->next=NULL;
free(p1);
}
else//中间部分的结点
{
p1->pre->next=p1->next;
p1->next->pre=p1->pre;
free(p1);
}
}
return head;
}
student *insert(student* head,int num)
{
student *p=(student*)malloc(sizeof(student));
p->data=num;
p->next=head;
head->pre=NULL;
head=p;
head->pre=NULL;
return head;
}
int main(void)
{
int number;
cout<<"请输入结点个数:"<<endl;
cin>>number;
student* Head=create(number);
cout<<"遍历后:"<<endl;
print(Head);
cout<<"要删除哪个数:"<<endl;
cin>>number;
Head=del(Head,number);
print(Head);
cout<<"要插入的数:"<<endl;
cin>>number;
Head=insert(Head,number);
print(Head);
return 0;
}