#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct LinkedList{
int data;
struct LinkedList *next;
}LinkedList,*List;
int a[5]={5,6,9,2,7};
int n=5;
void build(List &L){
L=(List)malloc(sizeof(LinkedList));
LinkedList *s;
LinkedList *r=L;
for(int i=0;i<n;i++){
s=(LinkedList *)malloc(sizeof(LinkedList));
s->data=a[i];
r->next=s;
r=r->next;
}
r->next=NULL;
}
void show(List &L){
LinkedList *s=L->next;
while(s){
printf("%d ",s->data);
s=s->next;
}
}
void del(List &L,int x,int y){
LinkedList *pre=L;
LinkedList *s=pre->next;
while(s){
if(s->data>x&&s->data<y){
pre->next=s->next;//此时pre的next和s的next指向的同一个数
//只需要s往后移动
s=s->next;
}else{//如果没有删两个指针都需要往后移动
pre=pre->next;
s=s->next;
}
}
}
int main(){
List L;
build(L);
show(L);
printf("\n");
printf("删除后:\n");
del(L,6,11);
show(L);
return 0;
}