解题思路:
注意事项:
参考代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct LNode{
int data;
struct LNode * next;
}LNode,*pNode;
pNode Create(int n){
pNode head;
head=(pNode)malloc(sizeof(LNode));
pNode p=head,s;
while(n--){
s=(pNode)malloc(sizeof(LNode));
s->next=NULL;
scanf("%d",&s->data);
p->next=s;
p=s;
}
p->next=NULL;
return head;
}
pNode Delete(pNode head,int x){
pNode p=head->next,pre,q;
while(p!=NULL){
if(p->data==x){
q=p;
p=q->next;
pre->next=p;
free(q);
}
else{
pre=p;
p=p->next;
}
}
return head;
}
void Printf(pNode head){
pNode p=head->next;
while(p!=NULL){
printf("%d ",p->data);
p=p->next;
}
}
int main(){
pNode head,p,s,pre,q;
int n,x;
scanf("%d",&n);
head=Create(n);
scanf("%d",&x);
head=Delete(head,x);
Printf(head);
return 0;
}
0.0分
0 人评分