Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. package lectures;
  2.  
  3. public class Prac1 {
  4.  
  5. public static void main(String[] args) {
  6. SLList la = new SLList();
  7. SLList lb = new SLList();
  8.  
  9. //la = {5,3,1}
  10. la.add(1);
  11. la.add(3);
  12. la.add(5);
  13.  
  14. //lb = {8,7,6,4,2}
  15. lb.add(2);
  16. lb.add(4);
  17. lb.add(6);
  18. lb.add(7);
  19. lb.add(8);
  20.  
  21. SLList lr = new SLList();
  22. lr.add(1);
  23. lr.add(2);
  24. lr.add(3);
  25. lr.add(4);
  26. lr.add(5);
  27. lr.add(6);
  28.  
  29. la.llreverse(); //{1,3,5}
  30. lb.llreverse(); //{2,4,6,7,8}
  31. lr.llreverse();
  32.  
  33.  
  34. //list should be {1,2,3,4,5,6,7,8}
  35. interleaf(la,lb).printAll();
  36.  
  37. }
  38.  
  39. /**
  40. *
  41. * @param l1 {1,3,5}
  42. * @param l2 {2,4,6,7,8}
  43. * @return should be {1,2,3,4,5,6,7,8}
  44. */
  45. public static SLList interleaf(SLList l1, SLList l2) {
  46.  
  47.  
  48. SLList interList = new SLList(); //our final result
  49.  
  50. //determines if any of list is empty as each iteration we
  51. //go through the head is removed
  52. while(!l1.isEmpty() && !l2.isEmpty()) {
  53.  
  54. interList.add(l1.head.info);
  55. l1.deleteHead();//after add, l1.head.info is removed
  56.  
  57.  
  58. interList.add(l2.head.info);
  59. l2.deleteHead();//after add, l2.head.info is removed
  60.  
  61. }
  62.  
  63. /**if lists are uneven in size, we will put the
  64. remaining larger list in our interList
  65. */
  66. while(!l1.isEmpty()) {
  67. interList.add(l1.head.info);
  68. l1.deleteHead();
  69. }
  70. while(!l2.isEmpty()) {
  71. interList.add(l2.head.info);
  72. l2.deleteHead();
  73. }
  74.  
  75. //right now its backwards, so we need to reverse it
  76. interList.llreverse();
  77. return interList;
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement