両端チェーンテーブル、双方向チェーンテーブル(java)
33243 ワード
1、両端チェーンテーブル2、試験両端チェーンテーブル3、双方向チェーンテーブル4、試験双方向チェーンテーブル
1、両端チェーンテーブル
2、テスト両端チェーンテーブル
3、双方向チェーンテーブル
4、テスト双方向チェーンテーブル
1、両端チェーンテーブル
package com.cwq.ch05;
import com.cwq.ch04.Node;
/**
* ,
* @author Carpoor
* @date 2019 1 29
*/
public class MyFirstLastLinkedList {
private Node first;
private Node last;
public MyFirstLastLinkedList() {
first = null;
}
/**
* ,
*/
public void insertFirst(long value) {
Node node = new Node(value);
if (isEmpty()) {
last = node;
}
node.next = first;
first = node;
}
/**
* ,
*/
public void insertLast(long value) {
Node node = new Node(value);
if (isEmpty()) {
first = node;
} else {
last.next = node;
}
last = node;
}
/**
* ,
*/
public Node deleteFirst() {
Node tmp = first;
if (tmp.next == null) {
last = null;
}
first = first.next;
return tmp;
}
/**
*
*/
public void display() {
Node current = first;
while (current != null) {
current.display();
current = current.next;
}
System.out.println();
}
/**
*
*/
public Node find(long value) {
Node current = first;
while (current.data != value) {
if (current.next == null) {
return null;
}
current = current.next;
}
return current;
}
/**
*
*/
public Node delete(long value) {
Node current = first;
Node prev = first;
while (current.data != value) {
if (current.next == null) {
return null;
}
prev = current;
current = current.next;
}
if (current == first) {
first = first.next;
} else {
prev.next = current.next;
}
return current;
}
public boolean isEmpty() {
return first == null;
}
}
2、テスト両端チェーンテーブル
package com.cwq.ch05;
/**
* @author Carpoor
* @date 2019 1 30
*/
public class TestFirstLastMyLinkedList {
public static void main(String[] args) {
MyFirstLastLinkedList list = new MyFirstLastLinkedList();
list.insertFirst(50);
list.insertFirst(40);
list.insertFirst(30);
list.insertLast(50);
list.insertLast(40);
list.insertLast(30);
list.display();
while(!list.isEmpty()) {
list.deleteFirst();
list.display();
}
}
}
3、双方向チェーンテーブル
package com.cwq.ch05;
/**
* ,
* @author Carpoor
* @date 2019 1 29
*/
public class MyDoubleLinkedList {
private Node first;
private Node last;
public MyDoubleLinkedList() {
first = null;
}
/**
* ,
*/
public void insertFirst(long value) {
Node node = new Node(value);
if (isEmpty()) {
last = node;
}else {
first.prev = node;
}
node.next = first;
first = node;
}
/**
* ,
*/
public void insertLast(long value) {
Node node = new Node(value);
if (isEmpty()) {
first = node;
} else {
last.next = node;
node.prev = last;
}
last = node;
}
/**
* ,
*/
public Node deleteFirst() {
Node tmp = first;
if (tmp.next == null) {
last = null;
}else {
first.next.prev = null;
}
first = first.next;
return tmp;
}
/**
* ,
*/
public Node deleteLast() {
Node tmp = last;
if (tmp.prev == null) {
first = null;
}else {
tmp.prev.next = null;
}
last = last.prev;
return tmp;
}
/**
*
*/
public void display() {
Node current = first;
while (current != null) {
current.display();
current = current.next;
}
System.out.println();
}
/**
*
*/
public Node find(long value) {
Node current = first;
while (current.data != value) {
if (current.next == null) {
return null;
}
current = current.next;
}
return current;
}
/**
*
*/
public Node delete(long value) {
Node current = first;
Node prev = first;
while (current.data != value) {
if (current.next == null) {
return null;
}
prev = current;
current = current.next;
}
if (current == first) {
first = first.next;
} else {
prev.next = current.next;
}
return current;
}
public boolean isEmpty() {
return first == null;
}
}
4、テスト双方向チェーンテーブル
package com.cwq.ch05;
/**
* @author Carpoor
* @date 2019 1 31
*/
public class TestMyDoubleLinkedList {
public static void main(String[] args) {
MyDoubleLinkedList list = new MyDoubleLinkedList();
list.insertFirst(50);
list.insertFirst(40);
list.insertFirst(30);
list.insertLast(50);
list.insertLast(40);
list.insertLast(30);
list.display();
while(!list.isEmpty()) {
list.deleteFirst();
list.display();
}
System.out.println("------------------------------------");
list.insertFirst(50);
list.insertFirst(40);
list.insertFirst(30);
list.insertLast(50);
list.insertLast(40);
list.insertLast(30);
list.display();
while(!list.isEmpty()) {
list.deleteLast();
list.display();
}
}
}