【十二题解】 | #从单向链表中删除指定值的节点#
从单向链表中删除指定值的节点
http://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f
#include<stdio.h>
typedef struct no{
int data;
struct no*next;
}node;
void insert(node*a, int data, int head){
node*new=(node*)malloc(sizeof(node));
new->data=data;
node*tmp=a;
while(tmp->data != head)tmp=tmp->next;
new->next=tmp->next;
tmp->next=new;
}
void del(node*a, int data){
node*tmp=a;
while(tmp->next->data != data)tmp=tmp->next;
node*temp=tmp->next;
tmp->next=tmp->next->next;
free(temp);
}
void erc(node*a){
while(a->next != NULL){
printf("%d ", a->data);
a=a->next;
}
printf("%d ", a->data);
}
int main(){
int num;
scanf("%d", &num);
node*first=(node*)malloc(sizeof(node));
node*second=(node*)malloc(sizeof(node));
scanf("%d", &second->data);
first->next=second;
first->data=NULL;
second->next=NULL;
int data, head;
for(int i=0; i<num-1; i++){
scanf("%d %d", &data, &head);
insert(first, data, head);
}
int dele;
scanf("%d", &dele);
del(first, dele);
erc(second);
}