分别用递归和非递归方法删除列表[4,5,2,,5,6,1,5,9,4]中第一个5
时间: 2023-10-10 11:08:32 浏览: 44
使用递归方法删除第一个5:
```python
def delete_first_5_recursive(lst):
if not lst: # 如果列表为空,则返回空列表
return []
elif lst[0] == 5: # 如果第一个元素是5,则返回剩余元素的列表
return lst[1:]
else: # 否则,递归调用函数,去掉列表的第一个元素
return [lst[0]] + delete_first_5_recursive(lst[1:])
lst = [4, 5, 2, 5, 6, 1, 5, 9, 4]
print(delete_first_5_recursive(lst)) # [4, 2, 5, 6, 1, 5, 9, 4]
```
使用非递归方法删除第一个5:
```python
def delete_first_5_iterative(lst):
for i, val in enumerate(lst):
if val == 5: # 找到第一个5的位置,然后删除
del lst[i]
break
return lst
lst = [4, 5, 2, 5, 6, 1, 5, 9, 4]
print(delete_first_5_iterative(lst)) # [4, 2, 5, 6, 1, 5, 9, 4]
```
相关问题
分别用递归和非递归方法删除列表[4,5,2,,5,6,1,5,9,4]中的元素5
递归方法:
```python
def remove_element_recursive(lst, val):
if not lst: # 如果列表为空,直接返回空列表
return []
elif lst[0] == val: # 如果列表的第一个元素是要删除的元素,递归调用函数,去掉第一个元素
return remove_element_recursive(lst[1:], val)
else: # 如果列表的第一个元素不是要删除的元素,递归调用函数,保留第一个元素,并把后面的元素作为参数传入
return [lst[0]] + remove_element_recursive(lst[1:], val)
lst = [4, 5, 2, 5, 6, 1, 5, 9, 4]
val = 5
new_lst = remove_element_recursive(lst, val)
print(new_lst) # 输出 [4, 2, 6, 1, 9, 4]
```
非递归方法:
```python
def remove_element_nonrecursive(lst, val):
new_lst = [] # 新列表
for item in lst: # 遍历原列表
if item != val: # 如果元素不是要删除的元素,加入新列表
new_lst.append(item)
return new_lst
lst = [4, 5, 2, 5, 6, 1, 5, 9, 4]
val = 5
new_lst = remove_element_nonrecursive(lst, val)
print(new_lst) # 输出 [4, 2, 6, 1, 9, 4]
```
C++代码非递归方法实现删除Treeview中checkbox为true的节点
以下是一个实现删除TreeView中CheckBox为True的节点的非递归方法的C++代码示例:
```cpp
void DeleteCheckedNodes(HWND hTreeView)
{
HTREEITEM hRoot = TreeView_GetRoot(hTreeView);
// 如果没有根节点,直接返回
if (hRoot == NULL)
return;
// 创建一个栈来保存要删除的节点
std::stack<HTREEITEM> stackNodes;
stackNodes.push(hRoot);
while (!stackNodes.empty())
{
HTREEITEM hItem = stackNodes.top();
stackNodes.pop();
// 检查该节点的CheckBox是否为True
TVITEMEX tvItem = { 0 };
tvItem.hItem = hItem;
tvItem.mask = TVIF_STATEEX;
TreeView_GetItem(hTreeView, &tvItem);
if (tvItem.stateEx & TVIS_EX_DISABLED)
continue;
if ((tvItem.state & TVIS_STATEIMAGEMASK) >> 12 == 2)
{
// 如果是CheckBox为True的节点,删除该节点
TreeView_DeleteItem(hTreeView, hItem);
}
else
{
// 如果不是CheckBox为True的节点,将该节点的子节点加入栈中
HTREEITEM hChild = TreeView_GetChild(hTreeView, hItem);
while (hChild != NULL)
{
stackNodes.push(hChild);
hChild = TreeView_GetNextSibling(hTreeView, hChild);
}
}
}
}
```
该代码使用了一个栈来保存要删除的节点,然后通过循环遍历树的节点,检查每个节点的CheckBox是否为True。如果是,则直接删除该节点;如果不是,则将该节点的子节点加入栈中,以便后续处理。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)