#if 1
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
//初始化
Node *init()
{
Node *head=new Node;
head->next=NULL;
return head;
}
//头插法创建节点
void insetList(Node *head,int i)
{
Node *cur=new Node;
cur->data=i;
cur->next=head->next;
head->next=cur;
}
//链表A生成
void CreateList(Node *head_A)
{
for(int i=20;i>0;i--)
{
insetList(head_A,--i);
}
}
//打印链表
void print(Node *head)
{
Node *temp=head->next; //防止头指针移动
while(temp)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
void DeleteElement(Node *head_A)
{
Node *pa=head_A->next; //pa指向链表A的首元节点
int mink=5;int maxk=15;
Node *pc=head_A; //pc为pa前驱
while(pa)
{
if(pa->data>mink&&pa->data<maxk)
{
pc->next=pa->next; //pc指向pa的后继节点
delete pa;
}
else
{
pc->next=pa; //pc指向小于mink的节点
pc=pc->next; //pc指针移动
}
pa=pc->next; //pa
}
}
void main()
{
Node *head_A=init(); //链表A初始化头节点
//创建链表A
CreateList(head_A);
//打印链表A
cout<<"链表A为:"<<endl;
print(head_A);
cout<<endl;
//调用函数排除元素
DeleteElement(head_A);
//打印链表C
cout<<endl<<"删除后链表为:"<<endl;
print(head_A);
cout<<endl;
system("pause");
}
#endif
/*
总结:
删除顺序链表区间元素,需要一个前驱节点,其中两个临时的移动结构体指针
*/
原文:https://www.cnblogs.com/huxiaobai/p/10546096.html