詳談單鏈表之合併and冒泡排序

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>  線性表一章基本看完了,但是感覺還學得太少,應該和一些經典的算法相結合,看看自己應用的如何。不得不承認自己只是做簡單的實現,並沒有對代碼進行太多的優化。。。希望各位大神莫怪~~廢話少說,代碼搞起。。</strong></span>
<strong><span style="font-family:KaiTi_GB2312;font-size:18px;">//有序鏈表的合併
class MergerSingleList {
 private Nod head;

 public Nod getHead() {
  return head;
 }
 public void setHead(Nod head) {
  this.head = head;
 }
 public Nod getTail() {
  return tail;
 }
 public void setTail(Nod tail) {
  this.tail = tail;
 }
 // 查找指定位置的結點
 public Nod FindKth(int k) {
  Nod temp = head;
  int i = 1;
  while (temp != null && i < k) { // 移動元素,直到找到相應位置
   temp = temp.next;
   i++;
  }
  if (i == k)
   return temp;
  else
   return null;
 }
 // 將結點插入指定位置
 public void insert(int index, int a) {
  Nod newNode = new Nod(a);
  if (index > Length + 1) {
   System.out.println("超出範圍,無法插入");
  } else {
   if (index == 1) {
    newNode.next = head;
    head = newNode;
    Length++;
   } else if (index > 1 && index < 100) {
    newNode.next = FindKth(index - 1).next;
    FindKth(index - 1).next = newNode;
    Length++;
   }
  }
 }
 // 打印鏈表
 public void display() {
  Nod current = head;
  while (current != null) {
   current.displayNod();
   current = current.next;
  }
 }
 // Sort
 public void BubbleSort(MergerSingleList m1) {
  Nod current1 = null;
  Nod current2 = null;
  int temp = 0;
  for (int k = m1.Length - 1; k > 0; k--) {
   for (int i = 1; i < k + 1; i++) {
    if (i <= m1.Length) {
     current1 = m1.FindKth(i);
     current2 = m1.FindKth(i + 1);
     if (current1.key > current2.key) {
      temp = current1.key;
      current1.key = current2.key;
      current2.key = temp;
     }
    }
   }
  }
  m1.display();
 }
 public static void main(String args[]) {
  MergerSingleList ms1 = new MergerSingleList();
  MergerSingleList ms2 = new MergerSingleList();
  // 在單鏈表1中插入元素
  ms1.insert(1, 1);
  ms1.insert(2, 2);
  ms1.insert(3, 3);
  ms1.insert(4, 8);
  ms1.insert(5, 9);
  // 在單鏈表2中插入元素
  ms2.insert(1, 4);
  ms2.insert(2, 5);
  ms2.insert(3, 6);
  ms2.insert(4, 7);
  ms2.insert(5, 7);
  // 打印單鏈表1、2
  System.out.println("單鏈表1");
  ms1.display();
  System.out.println();
  System.out.println("單鏈表2");
  ms2.display();
  System.out.println();
  /** ************************方法1************************** */
  // ms1.FindKth(ms1.Length).next = ms2.head;
  // System.out.println("單鏈表合併後爲:");
  // ms1.display();
  /** ************************方法2************************** */
  // 建立單鏈表3,存放單鏈表1、2的元素
  MergerSingleList ms3 = new MergerSingleList();
  for (int i = 0; i <= (ms1.Length + ms2.Length); i++) {
   ms3.insert(i, 0);
  }
  System.out.println("單鏈表3");
  ms3.display();
  System.out.println();
  ms3 = ms1;
  System.out.println("將單鏈表1存放到單鏈表3後");
  ms3.display();
  System.out.println();
  for (int j = 1; j <= ms2.Length; j++) {
   int a = ms2.FindKth(j).key;
   ms3.insert(ms1.Length + 1, a);
  }
  System.out.println("合併的鏈表");
  ms3.display();
  System.out.println();
  System.out.println("排序後的鏈表");
  ms3.BubbleSort(ms3);
 }
}
class Nod {
 public int key;
 public Nod next;
 // 初始化頭結點
 public Nod(int head) {
  this.key = head;
  this.next = null;
 }
 public void displayNod() {
  System.out.print(key + " -> ");
 }
}
</span></strong>

發佈了51 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章