#include
#include
int main()
{
int i=0;
struct list
{
int age;
char name[8];
struct list *next;
struct list *back;
};
struct list *p1,*p2,*p3,*p,*head,*end;
p1=p2=p=head=end=(struct list*)malloc(sizeof(struct list));
scanf("%d%s",&p1->age,p1->name);
while(p1->age!=999) //输入age为999退出
{
i++;
if(i==1)
{
head=p3=p1;
p1->back=NULL;
}
else
{
p2->next=p1;
p1->back=p2;
p2=p1;
}
p1=(struct list*)malloc(sizeof(struct list));
scanf("%d%s",&p1->age,p1->name);
}
end=p2;
p2->next=NULL;
p=head; //输出正向
while(p!=NULL)
{
printf("%d %s\n",p->age,p->name);
p=p->next;
}
printf("\n");
p=end; //输出反向
while(p!=NULL)
{
printf("%d %s\n",p->age,p->name);
p=p->back;
}
printf("\n");
p=head;
int k=1;
while(p->age!=25) //找到age为25的成员
{
k++;
p=p->next;
}
if(k==1) //位于表头
{
p1=p->next;
head=p1;
p1->back=NULL;
free(p);
}
else if(k==i) //位于表尾
{
p1=p->back;
end=p1;
p1->next=NULL;
free(p);
}
else
{
p1=p->next;
p2=p->back;
p1->back=p2;
p2->next=p1;
free(p);
}
p=head;
while(p!=NULL)
{
printf("%d %s\n",p->age,p->name);
p=p->next;
}
printf("\n");
p=end;
while(p!=NULL)
{
printf("%d %s\n",p->age,p->name);
p=p->back;
}
printf("\n");
}
阅读(236) | 评论(0) | 转发(0) |