1.目的
2つのリストを1つのリストで返したいです。
Java
1 2 List Merge(List list1, List list2) { 3 4 Cell c1 = header.next; 5 Cell c2 = list2.header; 6 Cell c3 = c2.next; 7 8 while(c1 != sentinel){ 9 if((Integer)c1.data > (Integer)c2.data){ 10 swap(c1,c2); 11 } 12 c1=c1.next; 13 } 14 15 16 while(c1 != sentinel){ 17 if ((Integer)c1.data > (Integer)c3.data){ 18 swap(c1,c3); 19 } 20 c1=c1.next; 21 } 22 23 24 header=list1.header; 25 sentinel=list2.sentinel; 26 27 List list3 = list1+list2; 28 return list3; 29 } 30 31
2.問題点
最後から二行目がエラーになります。
list3というlist1とlist2というマージされた1つのリストを返すには、list1にlist2を加えるという考え方ではないのでしょうか。アドバイスいただけると幸いです!
3.追記
線形リスト(連結リスト)でのマージソートです。
Java
1import java.util.Random; 2 3public class MergeSort { 4 public static void main(String[] args) { 5 List l = new List(15); 6 l.makeList(); 7 l.printList(); 8 l.mergeSort(); 9 l.printList(); 10 } 11} 12 13class List { 14 15 Cell header = new Cell("HEADER"); 16 Cell sentinel = new Cell(""); 17 int dataNum; 18 19 List(int num) { 20 dataNum = num; 21 header.next = sentinel; 22 } 23 24 void makeList() { 25 int[] tmpList = new int[dataNum]; 26 27 for (int i=0; i<dataNum; i++) { 28 tmpList[i]=i; 29 } 30 31 for (int i=0; i<dataNum-1; i++) { 32 Random rnd = new Random(); 33 int offset = rnd.nextInt(dataNum-i); 34 int tmp; 35 36 tmp = tmpList[i]; 37 tmpList[i] = tmpList[i + offset]; 38 tmpList[i + offset] = tmp; 39 } 40 41 for (int i=0; i<dataNum; i++) { 42 insertTop(new Cell(new Integer(tmpList[i]))); 43 } 44 } 45 46 void mergeSort() { 47 int center = dataNum/2; 48 Cell curr = header.next; 49 50 for (int i =0; i<center; i++) curr = curr.next; 51 52 List list1 = MergeSortRec(header.next,curr,center); 53 List list2 = MergeSortRec(curr,sentinel,dataNum-center); 54 List list3 = Merge(list1,list2); 55 56 header = list3.header; 57 sentinel = list3.sentinel; 58 } 59 60 List MergeSortRec(Cell start,Cell end, int num) { 61 int center2 = num/2; 62 start = header.next; 63 Cell c = header.next; 64 65 for (int i=0; i<center2 ; i++) start=start.next; 66 List list1 = MergeSortRec(c,start,center2); 67 if(center2==1){ 68 if((Integer)c.data>(Integer)start.data){ 69 swap(c,start); 70 } 71 } 72 73 List list2 = MergeSortRec(start,end,num-center2); 74 if(center2==1){ 75 if((Integer)start.data>(Integer)end.data){ 76 swap(start,end); 77 } 78 } 79 80 List list3 = Merge(list1,list2); 81 82 header = list3.header; 83 end = list3.sentinel; 84 85 return list3; 86 87 } 88 89 List Merge(List list1, List list2){ 90 // 最初に示したコードが下に入ります 91 } 92 93 void swap(Cell c1,Cell c2) { 94 if ((c1 != null) && (c2 != null)) { 95 Object tmp = c1.data; 96 c1.data = c2.data; 97 c2.data = tmp; 98 } 99 } 100 101 void insertTop(Cell c) { 102 c.next = header.next; 103 header.next = c; 104 105 } 106 107 void printList() { 108 Cell curr = header.next; 109 110 while (curr.next !=null) { 111 System.out.print(curr.data + " "); 112 curr = curr.next; 113 } 114 System.out.println(); 115 } 116} 117 118class Cell { 119 Cell next; 120 Object data; 121 122 Cell(Object obj) { 123 next = null; 124 data = obj; 125 } 126} 127 128
ソースを見る限りjava.util.Listを使用してるようではなそうなので、使用しているListクラスの情報を追記してください。