3,4班第二次作业.pptVIP

  • 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())

文档评论(0)

1亿VIP精品文档

相关文档