Java數據結構和算法--鏈表

  1. package ChapterFive;  
  2.   
  3. class Link<E> {  
  4.   
  5.     public E data;  
  6.   
  7.     public Link<E> next;  
  8.   
  9.     public Link(E data) {  
  10.         this.data = data;  
  11.     }  
  12. }  
  13.   
  14. class LinkList<E> {  
  15.   
  16.     public Link<E> first;  
  17.     //鏈表中數據項的個數  
  18.     public int size;  
  19.   
  20.     public LinkList() {  
  21.         first = null;  
  22.         size = 0;  
  23.     }  
  24.     //在表頭插入新的數據  
  25.     public void insertFirst(E value) {  
  26.         Link<E> link = new Link<E>(value);  
  27.         link.next = first;  
  28.         first = link;  
  29.         size++;  
  30.     }  
  31.     //判斷鏈表是否爲空  
  32.     public boolean isEmpty() {  
  33.         return size == 0;  
  34.     }  
  35.     //刪除表頭  
  36.     public Link<E> deleteFirst() {  
  37.         Link<E> temp = first;  
  38.         first = first.next;  
  39.         size--;  
  40.         return temp;  
  41.     }  
  42.     //輸出鏈表中的所有數據  
  43.     public void display() {  
  44.         Link<E> curr = first;  
  45.         while (curr != null) {  
  46.             System.out.print(curr.data + " ");  
  47.             curr = curr.next;  
  48.         }  
  49.         System.out.println();  
  50.     }  
  51.     //返回鏈表中數據項的個數  
  52.     public int size() {  
  53.         return size;  
  54.     }  
  55.     //獲取從頭至尾的第i個數據項  
  56.     public Link<E> get(int i) {  
  57.         if (i > size() - 1 || i < 0)  
  58.             try {  
  59.                 throw new IndexOutOfBoundsException();  
  60.             } catch (Exception e) {  
  61.                 e.printStackTrace();  
  62.             }  
  63.         Link<E> curr = first;  
  64.         for (int n = 0; n < size(); n++) {  
  65.             if (n == i)  
  66.                 return curr;  
  67.             else  
  68.                 curr = curr.next;  
  69.         }  
  70.         return null;  
  71.     }  
  72.     //輸出從頭至尾的第i個數據項  
  73.     public void remove(int i) {  
  74.         if (i == 0)  
  75.             deleteFirst();  
  76.         else if (i == size() - 1)  
  77.             get(i - 1).next = null;  
  78.         else {  
  79.             get(i - 1).next = get(i + 1);  
  80.         }  
  81.         size--;  
  82.     }  
  83. }  
  84.   
  85. public class Link_list {  
  86.     public static void main(String[] args) {  
  87.         LinkList<Long> ll = new LinkList<Long>();  
  88.         for (int i = 0; i < 10; i++) {  
  89.             Long value = (long) (Math.random() * 100);  
  90.             ll.insertFirst(value);  
  91.         }  
  92.         ll.display();  
  93.         while (!ll.isEmpty()) {  
  94.             ll.deleteFirst();  
  95.             ll.display();  
  96.         }  
  97.         System.out.println("Ok");  
  98.     }  
  99. }  

(2)鏈棧 
Java代碼 
  1. package ChapterFive;  
  2.   
  3. class LinkStack<E> {  
  4.   
  5.     LinkList<E> linkList;  
  6.   
  7.     int size;  
  8.   
  9.     public LinkStack() {  
  10.         size = 0;  
  11.         linkList = new LinkList<E>();  
  12.     }  
  13.     //入棧  
  14.     public void push(E value) {  
  15.         linkList.insertFirst(value);  
  16.         size++;  
  17.     }  
  18.     //出棧  
  19.     public Link<E> pop() {  
  20.         size--;  
  21.         return linkList.deleteFirst();  
  22.     }  
  23.     //返回棧頂元素  
  24.     public Link<E> top() {  
  25.         return linkList.first;  
  26.     }  
  27.     //判斷棧是否爲空  
  28.     public boolean isEmpty() {  
  29.         return size == 0;  
  30.     }  
  31.     //顯示棧中的全部數據  
  32.     public void display() {  
  33.         linkList.display();  
  34.     }  
  35. }  
  36.   
  37. public class Link_stack {  
  38.     public static void main(String[] args) {  
  39.         LinkStack<Long> ls = new LinkStack<Long>();  
  40.         for (int i = 0; i < 10; i++) {  
  41.             Long value = new Long((long) (Math.random() * 100));  
  42.             ls.push(value);  
  43.         }  
  44.         while (!ls.isEmpty()) {  
  45.             ls.pop();  
  46.             ls.display();  
  47.         }  
  48.         System.out.println("Ok");  
  49.     }  
  50. }  

(3)有序表 
Java代碼 
  1. package ChapterFive;  
  2.   
  3. class SortedLink {  
  4.   
  5.     public Link<Long> first;  
  6.   
  7.     int size;  
  8.   
  9.     public SortedLink() {  
  10.         first = null;  
  11.         size = 0;  
  12.     }  
  13.     //向有序鏈表中插入數據  
  14.     public void insert(long value) {  
  15.         Link<Long> newLink = new Link<Long>(value);  
  16.         Link<Long> previous = null;  
  17.         Link<Long> curr = first;  
  18.         while (curr != null && (value > curr.data)) {  
  19.             previous = curr;  
  20.             curr = curr.next;  
  21.         }  
  22.         if (previous == null)// 鏈表爲空(在表頭插入)  
  23.             first = newLink;  
  24.         else  
  25.             previous.next = newLink;//插入新的節點  
  26.         newLink.next = curr;  
  27.         size++;  
  28.     }  
  29.     //刪除第一個節點  
  30.     public Link<Long> remove() {  
  31.         Link<Long> temp = first;  
  32.         first = first.next;  
  33.         size--;  
  34.         return temp;  
  35.     }  
  36.     //判斷鏈表是否爲空  
  37.     public boolean isEmpty() {  
  38.         return size == 0;  
  39.     }  
  40.     //輸出鏈表的所有數據  
  41.     public void display() {  
  42.         Link<Long> curr = first;  
  43.         while (curr != null) {  
  44.             System.out.print(curr.data + " ");  
  45.             curr = curr.next;  
  46.         }  
  47.         System.out.println();  
  48.     }  
  49. }  
  50.   
  51. public class SortedLinkApp {  
  52.     public static void main(String[] args) {  
  53.         SortedLink sl = new SortedLink();  
  54.         for (int i = 0; i < 10; i++) {  
  55.             sl.insert((long) (Math.random() * 100));  
  56.         }  
  57.         while (!sl.isEmpty()) {  
  58.             sl.remove();  
  59.             sl.display();  
  60.         }  
  61.     }  
  62. }  

(4)雙向鏈表 
Java代碼 
  1. package ChapterFive;  
  2.   
  3. class DoubleLink<E> {  
  4.   
  5.     public Link<E> first;  
  6.   
  7.     public Link<E> last;  
  8.   
  9.     int size;  
  10.   
  11.     @SuppressWarnings("hiding")  
  12.     class Link<E> {  
  13.         public E data;  
  14.   
  15.         public Link<E> next;// 鏈表的下一項  
  16.   
  17.         public Link<E> previous;// 鏈表的前一項  
  18.   
  19.         public Link(E value) {  
  20.             this.data = value;  
  21.         }  
  22.     }  
  23.   
  24.     public DoubleLink() {  
  25.         first = null;  
  26.         last = null;  
  27.         size = 0;  
  28.     }  
  29.   
  30.     // 在鏈表的首部插入一項  
  31.     public void insertFirst(E value) {  
  32.         Link<E> newLink = new Link<E>(value);  
  33.         if (isEmpty())// 如果鏈表爲空則first == last  
  34.             last = newLink;  
  35.         else  
  36.             first.previous = newLink;// 確定原first與newLink的前後關係  
  37.         newLink.next = first;  
  38.         first = newLink;// 設置新的first值  
  39.         size++;  
  40.     }  
  41.   
  42.     // 在鏈表的尾部插入一項  
  43.     public void insertLast(E value) {  
  44.         Link<E> newLink = new Link<E>(value);  
  45.         if (isEmpty())// 如果鏈表爲空則last == first  
  46.             first = newLink;  
  47.         else {  
  48.             last.next = newLink;// 確定原last與newLink的前後關係  
  49.             newLink.previous = last;  
  50.         }  
  51.         last = newLink;// 設置新的last值  
  52.         size++;  
  53.     }  
  54.   
  55.     // 刪除雙向鏈表的表頭  
  56.     public Link<E> deleteFirst() {  
  57.         Link<E> temp = first;  
  58.         if (first.next == null)// 鏈表中只有一項數據  
  59.             last = null;  
  60.         else  
  61.             first.next.previous = null;// 銷燬原鏈表的頭部  
  62.         first = first.next;  
  63.         size--;  
  64.         return temp;  
  65.     }  
  66.   
  67.     // 刪除鏈表的最後一項  
  68.     public Link<E> deleteLast() {  
  69.         Link<E> temp = last;  
  70.         if (first.next == null)// 鏈表中只有一項數據  
  71.             first = null;  
  72.         else  
  73.             last.previous.next = null;// 銷燬原鏈表的尾部  
  74.         last = last.previous;  
  75.         size--;  
  76.         return temp;  
  77.     }  
  78.   
  79.     // 判斷鏈表是否爲空  
  80.     public boolean isEmpty() {  
  81.         return size == 0;  
  82.     }  
  83.   
  84.     // 輸出鏈表中的所有數據項  
  85.     public void display() {  
  86.         Link<E> curr = first;  
  87.         while (curr != null) {  
  88.             System.out.print(curr.data + " ");  
  89.             curr = curr.next;  
  90.         }  
  91.         System.out.println();  
  92.     }  
  93. }  
  94.   
  95. public class DoubleLinkApp {  
  96.     public static void main(String[] args) {  
  97.         DoubleLink<Integer> dl = new DoubleLink<Integer>();  
  98.         for (int i = 0; i < 5; i++) {  
  99.             dl.insertFirst((int) (Math.random() * 100));  
  100.         }  
  101.         for (int i = 0; i < 5; i++) {  
  102.             dl.insertLast((int) (Math.random() * 100));  
  103.         }  
  104.         dl.display();  
  105.         while (!dl.isEmpty()) {  
  106.             dl.deleteFirst();  
  107.             dl.deleteLast();  
  108.             dl.display();  
  109.         }  
  110.         System.out.println("Ok");  
  111.     }  
  112. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章