如何在单链表里复制不是数字'2',然后将复制好的内容存储到数组中
来源:互联网 发布:梵高固彩淘宝 编辑:程序博客网 时间:2024/06/05 07:24
关于单链表的知识点,小编就不在这里介绍了,如果有不懂的地方,自己查资料吧!
原题目是:Copy the contents of a linear linked list Except do not copy any 2’s and place the data into an array. Return the number of items copied
感觉这次目前小编用递归的方式来解决这道题,方法不是很好,太不简洁。若小编想到简洁的方法,还是用递归方式,就会更新这种方法。(别嘲笑小编!)
下面是list.h file 展示:
//This is the list.h file#include<iostream>#include<cctype>#include<cstring>using namespace std;struct node{ node * next; int data;};class list{ public: list(); //Supplied ~list(); //Supplied void build(); //Supplied void display(); //Supplied //Copy the contents of a linear linked list Except do not copy any 2's and place the data into an array //Return the number of items copied int copy_special(); private: //Copy the contents of a linear linked list Except do not copy any 2's and place the data into an array //Return the number of itmes copied int copy_special(int *& array, node * head, int index, int count); int copy_special(node * head); node * head; node * tail;};
因为题目要求是要将特殊的节点的内容输入到数组中,所以在argument list就得传进来数组。
下面是展示如何实现这三个函数的:
//This is the list.cpp file#include "list.h"int list::copy_special(){ int count = copy_special(head); int * array = new int[count]; int result = copy_special(array,head,0,count); for(int I = 0; I < count; ++I) { cout<<array[I]<<" "; } cout<<endl; return result;}int list::copy_special(int *& array, node * head, int index, int count){ if(!head) return 0; if(head->data != 2) { if(index < count) { array[index] = head->data; } return copy_special(array,head->next,index+1,count)+1; } return copy_special(array,head->next,index,count);}int list::copy_special(node * head){ if(!head) return 0; if(head->data != 2) { return copy_special(head->next) + 1; } return copy_special(head->next);}
至于如何在主函数里调用这些函数,小编就不在这里展示出来了
下面是展示结果:
小编想到更好的方法,第一时间会更新内容,敬请期待吧!
阅读全文
0 0
- 如何在单链表里复制不是数字'2',然后将复制好的内容存储到数组中
- 将单链表中的第一个节点的数字复制,然后将复制好的数字添加到最后一个节点,然后返回新的链表的最后两个节点的和
- 如何将字符串复制到数组中
- 将'文件1'的内容复制到'文件2'中
- 如何从系统相册提取一张照片,然后将其内容存储到一个事先准备好的文件中去
- 如何在筛选后的表里复制粘贴数据到同行的操作
- Excel中如何以文字而不是图片的方式将内容复制到QQ中
- 在dos下如何将一个盘的所有内容复制到另一个盘
- 使用递归来解决在单链表里删除第一节点不是数字‘2’
- js 将指定的内容复制到剪贴板中
- 在存储过程中,表记录内容之间的复制
- Android中如何复制内容到剪贴板
- 第一周第四天([大小写变换问题][判断是否为email][求对角线的值] [生成数组][数字中添加逗号][生成10个两位随机数,然后再进行排序] [复制数组||将指定数组的指定范围复制到新的数组])
- 将Excel单元格复制后,通过剪切板然后粘贴到页面上的table表中
- 如何在EXCEL中不复制隐藏的内容
- oracle 将一个表复制到另外一个表里
- 在单链表里如何将所有的unique data进行average
- 利用指针实现将5个整数输入到数组a中,然后将a逆序复制到数组b中,并输出b中各单元的值。
- python-pandas使用样例及速查表
- 统计水池个数
- HDU 2066 一个人的旅行【最短路dijkstra】
- Vim How To
- 机器学习的名词解释
- 如何在单链表里复制不是数字'2',然后将复制好的内容存储到数组中
- java笔试题及解答专辑201710241450
- 【Unity】【Shader】写一个初步的水波模拟Gerstner Waves
- 三、常量
- 【以太坊】ubuntu安装以太坊ethereum的测试网络ropsten-net以及雷电网络raiden-network环境
- Linux CentOS环境下安装图像识别工具Tesseract教程
- Grunt的一天
- 在Matlab下训练级联目标检测器
- 用eclipse启动tomcat项目默认发布地址