已知线性表中的元素以值递增有序排列,并以单链表作存储结构。试写一算法,删除表中所有大于x且小于y的元素(若表中存在这样的元素)同时释放被删除结点空间。
typedef struct { int data; struct LNode *next; }LNode; void Test(LNode *head,int x,int y){ LNode *pre,*p; if(head->next!=NULL){ pre=head; p=head->next; while (p->data<x){ pre=p; p=p->next; } if(p->data>y){ return; } else{ while (p->data<=y){ pre->next=p->next; free(p); p=pre->next; } } } }
void DelElems(LinkList* head,int x,,int y)
{
if(y<x) return;
LinkList *p=head;LinkList *q;
while(p->next!=NULL){
if(p->next->data>x&&p->next->data<y)
q=p->next;
p->next=p->next->next;
free(q);
p=p->next;
}
//
void filter(Lnode* L,int min,int max)
Lnode* cur=L;
while(cur->next->data!=Null)
if(cur->next->data>x && cur->next->data<y)
delete(cur,cur-next);
cur=cur->next;
void delete(Lnode* prenode ,Lnode *delnode)
Lnode* cur=delnode;
prenode->next=curnode->next;
free(delnode);
gaoshipeng 回复 gaoshipeng: if(cur->next->data>min && cur->next->data<max) { delete(cur,cur->next); }else { cur=cur->next; }
void Delete_list(Lnode *head, ElemType x, ElemType y) {
Lnode *p, *q;
if (!head) return ERROR;
p = head; q = p;
while(!p) {
if ((p->data>x) && (p->data<y)) {
if(p==head) {
head=p->next;
free(p);
p=head;
q=p;
else {
q->next=p->next;
p=q->next;
boolean deleteLNode(LinkList *L,int x,int y){
LinkNode* prior=L->next;
if(!L->next){
return false;
while(prior&&prior->date<y&&prior->date>x){
L->next=prior->next;
piror=piror->next;
return true;
LinkNode* q=prior->next;
while(prior&&q){
if(q->date<y&&q->date>x){
prior->next=q->next;
}else{
prior=prior->next;
q=q->next;
p = L->next;
if(p->next->data >x && p->next->data < y){
node * d = p->next;
p->next = d->next;
delete *p;
zibingo 回复 zibingo: 第一句改为p = L-.>head;
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct List
int data;
struct List *next;
}List;
List* creat(int n)
List* head = (List*)malloc(sizeof(List));
head->next=NULL;
List *p;
for(int i=0;i<n;i++)
List* q = (List*)malloc(sizeof(List));
scanf("%d",&q->data);
q->next=NULL;
p->next=q;
p=q;
return head;
void print(List* head)
p=head->next;
while (p)
printf("%d ",p->data);
void delete(List* head,int x,int y)
List *p,*q;
q=head;
if(p->data>x&&p->data<y)
else
int main ( )
int n,x,y;
printf("请输入单链表的长度:\n");
scanf("%d",&n);
printf("请输入表中各元素:\n");
List* L=creat(n);
printf("请输入x,y:\n");
scanf("%d %d",&x,&y);
printf("结果:\n");
delete(L,x,y);
print(L);
return 0;
#include<stdio.h> #include<stdlib.h> #define M (List)malloc(sizeof(Node)) typedef struct Node{ int data; Node* next; }Node,*List;
List create(int n){ List head=NULL,tail=NULL; for(int i=0;i<n+10;i++){ List p=M; p->data=i; if(head==NULL) head=tail=p; else{ tail->next=p; tail=p; } } tail->next=NULL; return head; } void print(List head){ while(head){ printf("%d ",head->data); head=head->next; } printf("\n"); } void Delete(List head,int x,int y){ while(head->data>x&&head->data<y){ head->data=head->next->data; List q=head->next; head->next=q->next; free(q); q=NULL; } List front=head,tail=head->next; while(tail){ if(tail->data>x&&tail->data<y){ front->next=tail->next; free(tail); tail=front->next; }else{ front=tail; tail=tail->next; } } }
int main(){ List head=create(0); int x,y; scanf("%d %d",&x,&y); print(head); Delete(head,x,y); print(head); return 0; }
void deletespan(List *head, Ele x, Ele y) { int n = length(head); List *p = head, *a, *b; for (int i = 0; i < n; i++) { if (p->next->data > x && p->next->data < y) { *a = p->next; p->next = p->next->next; free(a); } else { p = p->next; } } }
void Delete_lis...
用户登录可进行刷题及查看答案
void Delete_list(Lnode *head, ElemType x, ElemType y) { Lnode *p, *q; if (!head) return ERROR; p = head; q = p; while(!p) { if ((p->data>x) && (p->data<y)) { if(p==head) { head=p->next; free(p); p=head; q=p; } else { q->next=p->next; free(p); p=q->next; } } else { q=p; p=p->next; } } }
登录后提交答案