给定min和max删除链表中在其中的元素(包括min和max)
代码
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
node *next;
}Node;
void Creat(Node *L)
{
node *s,*tc=L;
printf("请输入元素个数n:");
int n,i;
scanf("%d", &n);
while(n--){
s=(Node*)malloc(sizeof(Node));
scanf("%d",&s->data );
tc->next =s;
tc=s;
}
tc->next =NULL;
}
void Delete(Node *L,int min,int max)
{
Node *p,*pre;
pre=L;
p=pre->next;
while(p != NULL){
if(p->data >= min && p->data <= max){
if(p->next == NULL){
pre->next =p->next ;
free(p);
pre->next =NULL;
break;
}
pre->next =p->next ;
free(p);
p=pre->next;
continue;
}
pre=pre->next ;
p=p->next ;
}
}
int DisList(Node *L)
{
if(L->next == NULL) return 0;
node *p=L->next ;
while(p != NULL){
printf("%d ", p->data );
p=p->next ;
}
printf("\n");
return 1;
}
int main()
{
Node *L=(Node *)malloc(sizeof(Node));
Creat(L);
Delete(L,1,3);
DisList(L);
return 0;
}