7、一个链表中的元素由另一个链表实现
2013-11-29 14:50
211 查看
/*7.给定一个链表L和另一个链表P,他们包含以升序排列的整数。
操作printLots(L,P)将打印出L中那些由P所指出的位置上的元素。
例如:如果P=1,3,4,6,那么L中的第1,3,4和6个元素被打印出来。
写出过程printLots(L,P)。该过程的运行时间是多少?
*/
操作printLots(L,P)将打印出L中那些由P所指出的位置上的元素。
例如:如果P=1,3,4,6,那么L中的第1,3,4和6个元素被打印出来。
写出过程printLots(L,P)。该过程的运行时间是多少?
*/
#include<iostream> #include<list> using namespace std; void printLots(const list<int> & P,const list<int> & L){ if(P.empty() || L.empty() || P.back() > L.size()) //P and L should not be empty, return; //the max number in P must be bigger than L.size(); list<int>::const_iterator con_iterP = P.begin(); list<int>::const_iterator con_iterL = L.begin(); int i = 0; for(;con_iterP != P.end();con_iterP++){ //cout the element be defined in P while(i != *con_iterP){ i++; con_iterL++; } cout << *con_iterL << endl; } } int main(){ list<int> L; list<int> P; L.push_back(1); L.push_back(3); L.push_back(4); L.push_back(6); P.push_back(1); P.push_back(2); P.push_back(3); P.push_back(4); P.push_back(5); P.push_back(6); P.push_back(7); printLots(L,P); return 0; }
相关文章推荐
- 单向链表 练习3.2 一个链表中的元素由另一个链表实现
- 面试-链表逆置 作业手写一个单链表,并且实现单链表元素的逆置,(a0, a1,a2,a3,..an)-> (an,an-1,… a1, a0),算法的空间复杂度和时间复杂度经可能低
- 编写一个多线程函数实现对数组排序,要求: 1.至少用两个线程 2.数组的元素值可以事先定义好,或者可以从键盘输入(增加一个线程)。 3.用一个线程对数组排序,用另一个线程输出排序结果。 4.保证先排好序,再输出。
- List的All方法使用问题:我用List1的All方法来确保List1的所有每个数组元素中是否包含Arr1中的任意元素,这个部分很难实现。 因为,没有一个数组中是否包含另一个数组中的任意元素这个功能
- (学习java)写一个完整的程序,实现随机生成20个元素的链表,快速查找中间结点的值并显示
- java 程序里如何实现从一个列表中拖动一个元素到另一个列表中?
- 实现对一个含n个元素的单链表的逆转.要去除存储链表本身所需的空间外,该过程只能使用固定大小的存储空间.
- 数据结构与算法分析 c++11 题3.1 由一个链表元素指定另一个链表对应位置的元素输出
- java实现输入一个链表,反转链表后,输出链表的所有元素。
- 用JAVA实现:非递归算法在O(n)时间内将一个含有n个元素的单链表逆置,要求其辅助空间为常量
- 链表的基本操作(创建,查找指定位置元素,删除指定元素,插入,倒置,去重,求集合的差,分别交换结点与交换结点值实现的冒泡排序,将两个有序链表合并成一个有序链表)c语言实现
- 给定一个单向链表(长度未知),请设计一个既节省时间又节省空间的算法来找出该链表中的倒数第m个元素。实现这个算法,并为可能出现的特例情况安排好处理措施。“倒数第m个元素”是这样规定的:当m=0时,链表的
- (2)单链表的操作 ① 输入一组整型元素序列,使用尾插法建立一个带有头结点的单链表。 ② 实现该线性表的遍历。 ③ 在该单链表的第i个元素前插入一个整数。 ④ 删除该单链表中的第i个元素,其值通过参数
- java实现输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head
- 用C语言实现在一个链表删除指定的一个或多个元素
- 输入一组整型元素序列,使用尾插法建立一个带有头结点的单链表。 ② 实现该线性表的遍历。 ③ 在该单链表的第i个元素前插入一个整数。 ④ 删除该单链表中的第i个元素,其值通过参数将其返回。 ⑤ 建立两个
- 编写算法实现建立一个带头结点的含n个元素的双向循环链表H,并在链表H中的第i个位置插入一个元素e
- 实现的效果是分别单击“显示”时,相应的内容展开,再单击另一个“显示”时,对上一个没影响;
- 实现双向链表删除一个节点P,在节点P后插入一个节点
- MySql中把一个表的数据插入到另一个表中的实现代码