- 32
- 0
- 约5.09千字
- 约 21页
- 2021-01-07 发布于安徽
- 举报
第二次作业 封煜佳 mf0932012@software.nju.edu.cn Chaper 3 1. Swap two adjacent elements by adjusting only the links(and not the data) using: a. Singly linked lists. b. Doubly linked lists. 通过只调整链(而不是数据)来交换两个相邻的元素,使用 a.单链表 b.双链表 1.a a.next = b.next; a b b.next = a.next.next; a.next.next = b; 1.b b.next = a.next.next; b.next.previous = b; a b a.next = b.next; a.next.next = b; b.previous = a.next; a.next.previous = a; 1—作业中出现的问题 temp=b b.next = b.next.next; a.next = temp.next; a.next.next = b; a b temp Chaper 3 2. Given two sorted lists L1 and L2,write a procedure to compute L1∩ L2 using only the basic list operations. 给定两个已排序的表L1和L2,只使用基本的链表操作编写计算L1∩L2的过程。 2、 L1∩L2 思考: L1 与L2是排序的,假设是从小到大的升序排列。 求L1与L2的交就是找到L1与L2 2、 L1∩L2 public LinkedList intersectionOfList(LinkedList L1,LinkedList L2) { LinkedListItr itr1 = L1.first(); LinkedListItr itr2 = L2.first(); LinkedList L = new LinkedList(); LinkedListItr itr = L.zeroth(); while(!itr1.isPastEnd() !itr2.isPastEnd()){ if(itr1.retrive() == itr2.retrive()){ L.insert(itr1.current,itr); itr1.advance(); itr2.advance(); }else{ if(itr1.current.element itr2.current.element) itr1.advance(); else itr2.advance(); } } return L; } 3、 L1 ∪ L2 思考: 求L1∪L2 与求L1∩L2基本思路相似,只是需要将所有不同的元素也加入到返回集中。 3、 L1 ∪ L2 public LinkedList unionOfList(LinkedList L1,LinkedList L2 ) { LinkedListItr itr1 = L1.first(); LinkedListItr itr2 = L2.first(); LinkedList L = new LinkedList(); LinkedListItr itr = L.zeroth(); while(!itr1.isPastEnd() !itr2.isPastEnd()){ if(itr1.retrive() == itr2.retrive()){ L.insert(itr1.current,itr); itr1.advance(); itr2.advance(); }else{ if(itr1.current.element itr2.current.element) {L.insert(itr1.current,itr);itr1.advance();} else {L.insert(itr2.current,itr);itr2.advance();} } } //如果L1没有结束,将L1元素添加到L中 for(;!itr1.isPastEnd();itr1.advance())
您可能关注的文档
最近下载
- 租房合同范本,租房租房合同范本.docx VIP
- 2024-2025学年八年级数学上册:全等三角形的判定(ASA与AAS) 知识梳理与讲解.pdf VIP
- 2026-2030中国沼气产业深度解析及发展前景对策建议研究报告.docx
- 2023年广西南宁市中考数学一模试卷.pdf VIP
- 场地设计真题06-09年.pdf VIP
- 广东省2025年初中学业水平考试地理真题(含答案).pdf VIP
- 广东省高考:2025年-2023年《生物》考试真题与参考答案.pdf
- 2025年烟叶评级技能竞赛理论参考试题库-上(单选题汇总).docx
- 《心脑血管类》课件.pptx VIP
- 化工单机设备试车方案与操作流程.docx VIP
原创力文档

文档评论(0)