C语言单链表删除表中的大于min小于max的所有节点,我写了一个,但是一直运行不成功……

#include "stdio.h"
#include "stdlib.h"
#define null 0

typedef struct node//结点结构
{
int data;
struct node *next;
}LNode;

LNode *create()//创建单链表
{
LNode *head,*tail,*p;
int x;
head=(LNode *)malloc(sizeof (LNode));
head->next=null;
tail=head;
scanf("%d",&x);
while(x!=0)
{
p=(LNode *)malloc(sizeof(LNode));//申请结点
p->data=x;//填入数据
p->next=null;
tail->next=p;
tail=p;
scanf("%d",&x);
}
return head;
}

void Delete(LNode *head,int min,int max)//删除大于Min小于Max的元素
{
LNode *p,*q;
p=head;
while (p->next!=NULL)
{
if (p->next->data<max&&p->next->data>min)
{q=p->next;
p->next=p->next->next;//删除指定的字符
free(q);
}
else
p=p->next;//不删除,只是指针后移
}
}

void print(LNode *head)//打印单链表
{
LNode *p;
p=head->next;
while(p!=null)
{
printf("%3d",p->data);
p=p->next;
}
}

void main()
{
LNode *H;
int min,max;
printf("创建单链表:\n");
H=create();
printf("输入删除区间的上下限:\n");
scanf("%d\n,%d\n",&min,&max);
Delete(H,min,max);
printf("删除元素后的单链表为: \n");
print(H);
printf("\n");
}

void Delete(LNode *head,int min,int max)//删除大于Min小于Max的元素
{
    LNode *p,*q;
    p = head;
    while (p->next!=NULL)
    {
        if (p->next->data<max&&p->next->data>min)
        {
            q=p->next;
            p->next=q->next;//删除指定的字符
            free(q);
        }
        p = p->next;//不删除,只是指针后移
    }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-05-06
十年了都忘记了,自己慢慢找吧。。。。
相似回答