使用递归来实现在ARR里如何删除每一个list的第一个节点不是数字‘2’
2017-07-24 07:02
405 查看
在之前,小编写了如何用递归来解决同一个问题在不同的数据结构里,那现在,继续写如何在ARR里做同一个问题,同样也是不能用循环来实现。
下面是“arr.h” 文件里的代码,一些代码是已经实现了,所以小编就不写出来了
为了实现这道题,那就得使用pointer arithmetic to access the elements of the array。就得清楚考虑prototype如何写,下面就看小编如何在.h 文件里写prototype
下面,就是如何实现这些function 在 .cpp 文件夹里了。
是不是感觉这个做法跟单链表,循环链表和双向链表有点相似呢!就是添加了一个数组的index变换而已,剩下的几乎一模一样。
小编是在linux环境下进行编译和运行的,下面是结果的截图:
是不是感觉很简单呢,以后小编还会继续写在不同数据结构实现某种功能的代码,请听下回分解!
为小编点赞吧!!
下面是“arr.h” 文件里的代码,一些代码是已经实现了,所以小编就不写出来了
//arr.h #include<iostream> #include<cstring> #include<cctype> using namespace std; struct node { int data; node * next; }; class table { public: //These functions are already written table(); ~table(); void build(); void display(); private: node ** head; //dynamically allocated array int size; //the array size };
为了实现这道题,那就得使用pointer arithmetic to access the elements of the array。就得清楚考虑prototype如何写,下面就看小编如何在.h 文件里写prototype
//arr.h #include<iostream> #include<cstring> #include<cctype> using namespace std; struct node { int data; node * next; }; class table { public: //These function functions are already written table(); ~table(); void build(); void display(); //Write the function prototype here: //Remove the first node if the number is not 2 in every list void remove_first(); private: //Remove the first node if the number is not 2 in every list void remove_first(node * head[], int index); void remove_first(node *& head); node ** head; //dynamically allocated array int size; //the array size };
下面,就是如何实现这些function 在 .cpp 文件夹里了。
//arr.cpp #include "arr.h" //Remove the first node if the number is not 2 in every list void table::remove_first() { //这个得传head指针和数组的第一个index给recursive function remove_first(head,0); } void table::remove_fist(node * head[], int index) { //因为数组的size知道了 if(index < size) { //这个是传给recursive function remove_first(head[index]); //这个是本身函数,进行index的变化 remove_first(head,++index); } } void table::remove_first(node *& head) { if(!head) return; if(head->data != 2) { node * temp = head->next; delete head; head = NULL; head = temp; return; } }
是不是感觉这个做法跟单链表,循环链表和双向链表有点相似呢!就是添加了一个数组的index变换而已,剩下的几乎一模一样。
小编是在linux环境下进行编译和运行的,下面是结果的截图:
是不是感觉很简单呢,以后小编还会继续写在不同数据结构实现某种功能的代码,请听下回分解!
为小编点赞吧!!
相关文章推荐
- 使用递归来实现删除单链表中最后一个节点不是数字‘2’
- 使用递归来实现双向链表里删除第一节点不是数字‘2’
- 使用递归来实现在循环链表里删除第一节点不是数字‘2’
- 如何使用C++递归来实现在BST(Binary Search Tree)里将所有的叶子节点上的数字求和
- 设计一个整型链表类list,能够实现链表节点的插入、删除、以及链表数据的输出操作。
- 关于字符和数字类型的索引,Oracle如何实现内部自动转换以及索引使用的验证测试
- 【JAVASCRIPT】使用ztree树,实现右键增加,修改,删除节点。带有复选框。
- 关于字符和数字类型的索引,Oracle如何实现内部自动转换以及索引使用的验证测试
- 如何使用ListView实现一个带有网络请求,解析,分页,缓存的公共的List页面来大大的提高工作效率
- Spark如何使用Akka实现进程、节点通信的简明介绍
- <STL初探>list节点为自定义类型,如何访问节点中变量和函数,如何使用list自带的排序
- Spark如何使用Akka实现进程、节点通信的简明介绍
- 【C++ STL应用与实现】7: 如何使用std::forward_list 单链表 (since C++11)
- 如何使用JAVA实现数字水印
- 只知道某个单向链表的中的某个指针,且该指针不是尾指针,如何删除该节点。
- 使用自定义适配器实现ListView中的每一个list的不同显示风格
- 如何实现下拉刷新View处在顶端时,才能拉出下拉刷新View,而不是仅仅处于第一个可见View时就可拉出
- 用jQuery如何实现删除子节点和winform label显示数据分页
- 如何在SQL Server 2005中使用作业实现备份和特定删除
- 如何使用c语言实现双向链表的插入删除操作