iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 12
0
自我挑戰組

練習程式系列 第 12

java,雙向連結串列

  • 分享至 

  • xImage
  •  

看網路上大大們的文章和影片,做些紀錄。
以下內容來自網路上大大們的文章。
紀錄雙向連結串列的程式來源。 稍微修改,print 觀察

教學來源:Data structures: Introduction to Doubly Linked List

程式來源:連結串列(Linked List)

abstract class LinkedList < T > {
 protected int count;
 protected Node < T > first;
 protected Node < T > last;
 public Node < T > getFirst() {
  return first;
 }
 public Node < T > getLast() {
  return last;
 }
 public int size() {
  return count;
 }
 abstract public void addAfter(Node < T > node, T value);
 abstract public void removeFirst();
 abstract public void removeLast();
 abstract public void remove(Node < T > node);
 abstract public void print();
 abstract public void rev();
}

class Node < T > {
 private Node < T > next;
 private Node < T > previous;
 private T value;

 public Node < T > getPrevious() {
  return previous;
 }

 void setPrevious(Node < T > previous) {
  this.previous = previous;
 }

 public Node < T > getNext() {
  return next;
 }

 void setNext(Node < T > next) {
  this.next = next;
 }

 public T getValue() {
  return value;
 }

 void setValue(T value) {
  this.value = value;
 }

 public Node(T value) {
  this.value = value;
 }
}

class DoublyLinkedList < T > extends LinkedList < T > {
 public void addFirst(T value) {
  // TODO Auto-generated method stub
  Node < T > node = new Node < T > (value);
  if (count == 0)
   last = node;
  else {
   node.setNext(first);
   first.setPrevious(node);
  }
  first = node;
  ++count;
 }

 @Override
 public void addAfter(Node < T > node, T value) {
  // TODO Auto-generated method stub
  Node < T > newNode = new Node < T > (value);
  //要注意順序,畫連結線,確認可以連好
  newNode.setNext(node.getNext());
  node.getNext().setPrevious(newNode);

  node.setNext(newNode);
  newNode.setPrevious(node);

  if (node == last) {
   last = newNode;
  }
  ++count;
 }

 @Override
 public void removeFirst() {
  // TODO Auto-generated method stub
  if (count == 0)
   throw new ArrayIndexOutOfBoundsException();
  else if (count == 1) {
   first = null;
   last = null;
  } else {
   Node < T > node = first.getNext();
   first.setNext(null);
   node.setPrevious(null);
   first = node;
  }
  --count;
 }

 @Override
 public void removeLast() {
  // TODO Auto-generated method stub
  if (count == 0)
   throw new ArrayIndexOutOfBoundsException();
  else if (count == 1) {
   first = null;
   last = null;
  } else {
   Node < T > node = last.getPrevious();
   last.setPrevious(null);
   node.setNext(null);
   last = node;
  }
  --count;
 }

 @Override
 public void remove(Node < T > node) {
  // TODO Auto-generated method stub
  if (node == first)
   removeFirst();
  else if (node == last)
   removeLast();
  else {
   node.getPrevious().setNext(node.getNext());
   node.getNext().setPrevious(node.getPrevious());
   node.setNext(null);
   node.setPrevious(null);
   --count;
  }
 }

 public void print() {
  Node printTemp = first;
  if (printTemp != null) {
   while (printTemp != null) {
    System.out.print(printTemp.getValue());
    printTemp = printTemp.getNext();
    if (printTemp != null) System.out.print(" --> ");
    else System.out.println("");
   }
  } else {
   System.out.println("");
  }

 }

 public void rev() {
  Node a = null, b = null, c = null, d = null;
  while (first != null) {
   a = b;
   c = d;
   b = first;
   d = last;
   first = first.getNext();
   last = last.getPrevious();
   b.setNext(a);
   d.setPrevious(c);
   //print();
   if (first == null) {
    first = b;
    last = d;
    //print();
    break;
   }

  }
 }

}

class Main {
 public static void main(String[] args) {
  DoublyLinkedList doublyLinkedList = new DoublyLinkedList();
  doublyLinkedList.addFirst(1);
  doublyLinkedList.addFirst(12.34);
  doublyLinkedList.addFirst("ABCD");

  System.out.println("doublyLinkedList.size:" + doublyLinkedList.size()); //doublyLinkedList.size:3

  doublyLinkedList.print(); //ABCD --> 12.34 --> 1

  doublyLinkedList.rev();
  doublyLinkedList.print(); //1 --> 12.34 --> ABCD

  doublyLinkedList.remove(doublyLinkedList.getFirst().getNext().getPrevious());
  doublyLinkedList.print(); //12.34 --> ABCD

 }
}
ABCD --> 12.34 --> 1
1 --> 12.34 --> ABCD
12.34 --> ABCD

上一篇
java,單向連結串列
下一篇
java,Bubble sort algorithm 和c++, Insertion Sort 、Selection Sort
系列文
練習程式37
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言