zoukankan      html  css  js  c++  java
  • 删除在链表区间值的元素

    #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);
    }
    
    
    
    
    
  • 相关阅读:
    08测试环境配置_数据库配置
    11等价类
    15状态迁移
    12边界值分析法
    10用例格式
    python的转义字符和原字符
    13数据分析法
    14正交试验
    python软件安装
    cookie的secure属性
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13811922.html
Copyright © 2011-2022 走看看