链表,队列,堆栈中的练习
2015-11-11 18:01
381 查看
1,给定一个表L和另一个表P他们包含以升序排列的整数,操作在printLots(L,P)将打印L中那些有P所指位置上的元素。写出printLots(L,P)。
2,通过只调整链表来交换两个相邻的元素,使用
a.单链表
b.双链表
(a) For singly linked lists:
(b) For doubly linked lists:
public static <AnyType> void printLots(List<AnyType> L,List <Integer> P){ Iterator<AnyType> iterL = L.iterator(); Iterator<Integer> iterP = P.iterator(); AnyType itemL=null; Integer itemP=0; int start = 0; while ( iterL.hasNext() && iterP.hasNext() ) { itemP = iterP.next(); System.out.println("Looking for position " + itemP); while ( start < itemP && iterL.hasNext() ) { start++; itemL = iterL.next(); } System.out.println( itemL ); } }
2,通过只调整链表来交换两个相邻的元素,使用
a.单链表
b.双链表
(a) For singly linked lists:
public static void swapWithNext( Node beforep ) { Node p, afterp; p = beforep.next; afterp = p.next; // Both p and afterp assumed not null. p.next = afterp.next; beforep.next = afterp; afterp.next = p; }
(b) For doubly linked lists:
public static void swapWithNext( Node p ) { Node beforep, afterp; beforep = p.prev; afterp = p.next; p.next = afterp.next; beforep.next = afterp; afterp.next = p; p.next.prev = p; p.prev = afterp; afterp.prev = beforep; }
相关文章推荐
- Linux系统的快速启动机制(内核切换) 【转】
- 使用imeOptions
- 安装Intellij Idea14/15
- Maven实战之Quick Start
- android BitmapFacty.Options的用法
- 【C语言】reverse_str递归调用函数
- 第十一周 项目1-4哈夫曼树
- Android Studio常用插件——codota
- Spark directStream保存/读取kafka offset
- Android4.0之后添加虚拟按键方法
- TCP协议疑难杂症全景解析
- leetcode DFS
- Linux 串口、usb转串口驱动分析(2-2) 【转】
- 变量、作用域和内存问题
- 关系数据库ORMlite的用法;
- QT网络相关
- PLSQL developer 连接64位Oracle 的解决方法
- windows 下 启动solr
- WPF Combox selectedItem”不能绑定”的问题
- 【C#】正则表达式