图片
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
struct film{
char name[20];
int id;
struct film *pre; //前向指针
struct film *next; //后向指针
};
struct film *head=NULL;
struct film *ls,*lspre,*work;
int i=0;
char input[20];
while(i<3){
puts("输入姓名:");
fgets(input,20,stdin);
work=(struct film*)malloc( sizeof(struct film));
if(head==NULL){
lspre=NULL;
head=work;
}
else
ls->next=work;
work->next=NULL;
work->pre=lspre;
strcpy(work->name,input);
work->id=i;
i++;
ls=work;
lspre=work;
}
//--------删除id=1 项--------------------------
work=head;
while(work!=NULL){
if((work->id)==1){
((work->pre)->next)=work->next; //后向指针
(work->next)->pre=work->pre; //前向指针
}
work=work->next;
}
//--------利用pre反向显示--------------------------
work=lspre;
while(work!=NULL){
printf("%d %s\n",work->id,work->name);
work=work->pre;
}
//-------链表遍历---------------------------------------------------
work=head;
while(work!=NULL){
// printf("name:%s id:%d\n",work->name,work->id );
work=work->next;
}
//--------释放全部malloc------------------------------
work=head;
while(work!=NULL){
ls=work->next;
free(work);
work=ls;
}
return 0;
}