删除在链表区间值的元素

#include<iostream>
using namespace std;
#include<vector>
struct node
{
	int data;
	node* next;
};
typedef node* list;
list init()
{
	list a = new node;
	a->next = NULL;
	return a;
}
list insert(list a,int i,int x)
{
	list p = a;
	for (int k = 0; k < i; k++)
	{
		if (p == NULL) break;
		else p = p->next;
	}
	list newNode = new node;
	newNode->data = x;
	newNode->next = p->next;
	p->next = newNode;
	return a;
}
list Delete(list a, int min, int max)
{
	list temp = a;
	while (temp!= NULL)
	{
		if (temp->next)
		{
			if (temp->next->data <= max && temp->next->data >= min)
			{
				list del = temp->next;
				temp->next = del->next;
				delete del;
			}
			else
			{
				temp = temp->next;
			}
		}
		else
		{
			break;
		}
	}
	return a;
}
void printf(list a)
{
	if (a->next != NULL)
	{
		while (a->next != NULL)
		{
			a = a->next;
			cout << a->data << " ";
		}
	}
	else
	{
		cout << "无元素" << endl;
	}
	cout << endl;
	return;
}

int main()
{
	list a = init(), b = init();
	for (int i = 0; i < 4; i++)
	{
		a = insert(a, i, i);
	}
	printf(a);
	a=Delete(a, 3, 3);
	printf(a);
}




原文地址:https://www.cnblogs.com/Hsiung123/p/13811922.html