#include using namespace std; typedef struct student { int data; struct student *next; struct student *pre; }dnode; dnode *create() { dnode *head,*p,*s; head=(dnode*)malloc(sizeof(dnode)); head->pre=NULL; p=head; int clycle=1; int i; while(clycle) { cout<<"input number: "; cin>>i; if(i!=0) { s=(dnode*)malloc(sizeof(dnode)); s->data=i; p->next=s; s->pre=p; p=s; } else clycle=0; } p->next=NULL; head=head->next; return (head); } dnode* del(dnode *head,int num) { dnode *s; s=head; while(s->data!=num&&s->next!=NULL) s=s->next; if(s->data==num) { if(s==head) { head=head->next; head->next=NULL; free(s); } else if(s->next==NULL) { s->pre->next=NULL; free(s); } else { s->next->pre=s->pre; s->pre->next=s->next; free(s); } } else cout<<"not found "<data<<" "; s=s->next; } } void main() { dnode *head; head=create(); print(head); int num; cin>>num; head=del(head,num); print(head); }
双链表操作
最新推荐文章于 2024-01-21 17:59:49 发布