#include <stdio.h> #include<stdlib.h> typedef struct Node{ int value; struct Node *next; }node; //节点插入函数 void insert(node *head,int later,int prior){ while(head!=NULL){ if(prior==head->value){ node *tmp=(node*)malloc(sizeof(node)); tmp->value=later; tmp->next=head->next; head->next=tmp; return; } head=head->next; } return; } //节点遍历函数 void display(node *head){ while(head!=NULL){ printf("%d ",head->value); head=head->next; } return; } //节点删除函数 void delete(node *head,int num){ if(head->value==num){ node *tmp=(node*)malloc(sizeof(node)); tmp=head; head=head->next; free(tmp); } //前驱节点 /* node *current=head; node *previous=NULL; while(current!=NULL&&(current->value!=num)){ previous=current; current=current->next; } if(current!=NULL){ previous->next=current->next; free(current); } */ //后驱节点 node *head_t=head; node *head_t1=head->next; while(head_t1!=NULL){ if(head_t1->value==num){ head_t->next=head_t1->next; free(head_t1); break; } head_t=head_t->next; head_t1=head_t->next; } } //释放动态分配的内存 void free_node(node *head){ while(head!=NULL){ node *next=head->next; free(head); head=next; } return; } int main() { int n,head_value,del; scanf("%d %d",&n,&head_value); //链表初始化 node *head=(node*)malloc(sizeof(node)); head->value=head_value; head->next=NULL; int later,prior; while(n>1){ scanf("%d %d",&later,&prior); insert(head,later,prior); n--; } scanf("%d",&del); delete(head,del); display(head); free_node(head); return 0; }