- package ChapterFive;
- class Link<E> {
- public E data;
- public Link<E> next;
- public Link(E data) {
- this.data = data;
- }
- }
- class LinkList<E> {
- public Link<E> first;
- //鏈表中數據項的個數
- public int size;
- public LinkList() {
- first = null;
- size = 0;
- }
- //在表頭插入新的數據
- public void insertFirst(E value) {
- Link<E> link = new Link<E>(value);
- link.next = first;
- first = link;
- size++;
- }
- //判斷鏈表是否爲空
- public boolean isEmpty() {
- return size == 0;
- }
- //刪除表頭
- public Link<E> deleteFirst() {
- Link<E> temp = first;
- first = first.next;
- size--;
- return temp;
- }
- //輸出鏈表中的所有數據
- public void display() {
- Link<E> curr = first;
- while (curr != null) {
- System.out.print(curr.data + " ");
- curr = curr.next;
- }
- System.out.println();
- }
- //返回鏈表中數據項的個數
- public int size() {
- return size;
- }
- //獲取從頭至尾的第i個數據項
- public Link<E> get(int i) {
- if (i > size() - 1 || i < 0)
- try {
- throw new IndexOutOfBoundsException();
- } catch (Exception e) {
- e.printStackTrace();
- }
- Link<E> curr = first;
- for (int n = 0; n < size(); n++) {
- if (n == i)
- return curr;
- else
- curr = curr.next;
- }
- return null;
- }
- //輸出從頭至尾的第i個數據項
- public void remove(int i) {
- if (i == 0)
- deleteFirst();
- else if (i == size() - 1)
- get(i - 1).next = null;
- else {
- get(i - 1).next = get(i + 1);
- }
- size--;
- }
- }
- public class Link_list {
- public static void main(String[] args) {
- LinkList<Long> ll = new LinkList<Long>();
- for (int i = 0; i < 10; i++) {
- Long value = (long) (Math.random() * 100);
- ll.insertFirst(value);
- }
- ll.display();
- while (!ll.isEmpty()) {
- ll.deleteFirst();
- ll.display();
- }
- System.out.println("Ok");
- }
- }
(2)鏈棧
- package ChapterFive;
- class LinkStack<E> {
- LinkList<E> linkList;
- int size;
- public LinkStack() {
- size = 0;
- linkList = new LinkList<E>();
- }
- //入棧
- public void push(E value) {
- linkList.insertFirst(value);
- size++;
- }
- //出棧
- public Link<E> pop() {
- size--;
- return linkList.deleteFirst();
- }
- //返回棧頂元素
- public Link<E> top() {
- return linkList.first;
- }
- //判斷棧是否爲空
- public boolean isEmpty() {
- return size == 0;
- }
- //顯示棧中的全部數據
- public void display() {
- linkList.display();
- }
- }
- public class Link_stack {
- public static void main(String[] args) {
- LinkStack<Long> ls = new LinkStack<Long>();
- for (int i = 0; i < 10; i++) {
- Long value = new Long((long) (Math.random() * 100));
- ls.push(value);
- }
- while (!ls.isEmpty()) {
- ls.pop();
- ls.display();
- }
- System.out.println("Ok");
- }
- }
(3)有序表
- package ChapterFive;
- class SortedLink {
- public Link<Long> first;
- int size;
- public SortedLink() {
- first = null;
- size = 0;
- }
- //向有序鏈表中插入數據
- public void insert(long value) {
- Link<Long> newLink = new Link<Long>(value);
- Link<Long> previous = null;
- Link<Long> curr = first;
- while (curr != null && (value > curr.data)) {
- previous = curr;
- curr = curr.next;
- }
- if (previous == null)// 鏈表爲空(在表頭插入)
- first = newLink;
- else
- previous.next = newLink;//插入新的節點
- newLink.next = curr;
- size++;
- }
- //刪除第一個節點
- public Link<Long> remove() {
- Link<Long> temp = first;
- first = first.next;
- size--;
- return temp;
- }
- //判斷鏈表是否爲空
- public boolean isEmpty() {
- return size == 0;
- }
- //輸出鏈表的所有數據
- public void display() {
- Link<Long> curr = first;
- while (curr != null) {
- System.out.print(curr.data + " ");
- curr = curr.next;
- }
- System.out.println();
- }
- }
- public class SortedLinkApp {
- public static void main(String[] args) {
- SortedLink sl = new SortedLink();
- for (int i = 0; i < 10; i++) {
- sl.insert((long) (Math.random() * 100));
- }
- while (!sl.isEmpty()) {
- sl.remove();
- sl.display();
- }
- }
- }
(4)雙向鏈表
- package ChapterFive;
- class DoubleLink<E> {
- public Link<E> first;
- public Link<E> last;
- int size;
- @SuppressWarnings("hiding")
- class Link<E> {
- public E data;
- public Link<E> next;// 鏈表的下一項
- public Link<E> previous;// 鏈表的前一項
- public Link(E value) {
- this.data = value;
- }
- }
- public DoubleLink() {
- first = null;
- last = null;
- size = 0;
- }
- // 在鏈表的首部插入一項
- public void insertFirst(E value) {
- Link<E> newLink = new Link<E>(value);
- if (isEmpty())// 如果鏈表爲空則first == last
- last = newLink;
- else
- first.previous = newLink;// 確定原first與newLink的前後關係
- newLink.next = first;
- first = newLink;// 設置新的first值
- size++;
- }
- // 在鏈表的尾部插入一項
- public void insertLast(E value) {
- Link<E> newLink = new Link<E>(value);
- if (isEmpty())// 如果鏈表爲空則last == first
- first = newLink;
- else {
- last.next = newLink;// 確定原last與newLink的前後關係
- newLink.previous = last;
- }
- last = newLink;// 設置新的last值
- size++;
- }
- // 刪除雙向鏈表的表頭
- public Link<E> deleteFirst() {
- Link<E> temp = first;
- if (first.next == null)// 鏈表中只有一項數據
- last = null;
- else
- first.next.previous = null;// 銷燬原鏈表的頭部
- first = first.next;
- size--;
- return temp;
- }
- // 刪除鏈表的最後一項
- public Link<E> deleteLast() {
- Link<E> temp = last;
- if (first.next == null)// 鏈表中只有一項數據
- first = null;
- else
- last.previous.next = null;// 銷燬原鏈表的尾部
- last = last.previous;
- size--;
- return temp;
- }
- // 判斷鏈表是否爲空
- public boolean isEmpty() {
- return size == 0;
- }
- // 輸出鏈表中的所有數據項
- public void display() {
- Link<E> curr = first;
- while (curr != null) {
- System.out.print(curr.data + " ");
- curr = curr.next;
- }
- System.out.println();
- }
- }
- public class DoubleLinkApp {
- public static void main(String[] args) {
- DoubleLink<Integer> dl = new DoubleLink<Integer>();
- for (int i = 0; i < 5; i++) {
- dl.insertFirst((int) (Math.random() * 100));
- }
- for (int i = 0; i < 5; i++) {
- dl.insertLast((int) (Math.random() * 100));
- }
- dl.display();
- while (!dl.isEmpty()) {
- dl.deleteFirst();
- dl.deleteLast();
- dl.display();
- }
- System.out.println("Ok");
- }
- }