单向链表:
- package LinkedList;
- /**
- * <p><strong>我的Java单链表练习</strong></p>
- * <p>单链表提供了在列表头的高效插入和删除操作,不过在单链表的末尾的插入操作效率很低.</p>
- * <p>单链表指针域保存着下一节点的引用,尾结点的指针域等于null</p>
- * @author baby69yy2000
- */
- public class SingleLinkedList<T> {
- /**
- * 结点类
- */
- private static class Node<T> {
- T nodeValue; // 数据域
- Node<T> next; // 指针域保存着下一节点的引用
- Node(T nodeValue, Node<T> next) {
- this.nodeValue = nodeValue;
- this.next = next;
- }
- Node(T nodeValue) {
- this(nodeValue, null);
- }
- }
- // 下面是SingleLinkedList类的数据成员和方法
- private Node<T> head, tail;
- public SingleLinkedList() {
- head = tail = null;
- }
- /**
- * 判断链表是否为空
- */
- public boolean isEmpty() {
- return head == null;
- }
- /**
- * 创建头指针,该方法只用一次!
- */
- public void addToHead(T item) {
- head = new Node<T>(item);
- if(tail == null) tail = head;
- }
- /**
- * 添加尾指针,该方法使用多次
- */
- public void addToTail(T item) {
- if (!isEmpty()) { // 若链表非空那么将尾指针的next初使化为一个新的元素
- tail.next = new Node<T>(item); // 然后将尾指针指向现在它自己的下一个元素
- tail = tail.next;
- } else { // 如果为空则创建一个新的!并将头尾同时指向它
- head = tail = new Node<T>(item);
- }
- }
- /**
- * 打印列表
- */
- public void printList() {
- if (isEmpty()) {
- System.out.println("null");
- } else {
- for(Node<T> p = head; p != null; p = p.next)
- System.out.println(p.nodeValue);
- }
- }
- /**
- * 在表头插入结点,效率非常高
- */
- public void addFirst(T item) {
- Node<T> newNode = new Node<T>(item);
- newNode.next = head;
- head = newNode;
- }
- /**
- * 在表尾插入结点,效率很低
- */
- public void addLast(T item) {
- Node<T> newNode = new Node<T>(item);
- Node<T> p = head;
- while (p.next != null) p = p.next;
- p.next = newNode;
- newNode.next = null;
- }
- /**
- * 在表头删除结点,效率非常高
- */
- public void removeFirst() {
- if (!isEmpty()) head = head.next;
- else System.out.println("The list have been emptied!");
- }
- /**
- * 在表尾删除结点,效率很低
- */
- public void removeLast() {
- Node<T> prev = null, curr = head;
- while(curr.next != null) {
- prev = curr;
- curr = curr.next;
- if(curr.next == null) prev.next = null;
- }
- }
- /**
- * <p>插入一个新结点</p>
- * <ul>插入操作可能有四种情况:
- * <li>①表为空, 返回false</li>
- * <li>②表非空,指定的数据不存在</li>
- * <li>③指定的数据是表的第一个元素</li>
- * <li>④指定的数据在表的中间</li></ul>
- * @param appointedItem 指定的nodeValue
- * @param item 要插入的结点
- * @return 成功插入返回true;
- */
- public boolean insert(T appointedItem, T item) {
- Node<T> prev = head, curr = head.next, newNode;
- newNode = new Node<T>(item);
- if(!isEmpty()) {
- while((curr != null) && (!appointedItem.equals(curr.nodeValue))) { //两个判断条件不能换
- prev = curr;
- curr = curr.next;
- }
- newNode.next = curr; //②③④
- prev.next = newNode;
- return true;
- }
- return false; //①
- }
- /**
- * <p>移除此列表中首次出现的指定元素</p>
- * <ul>删除操作可能出现的情况:
- * <li>①prev为空,这意味着curr为head. head = curr.next; --> removeFirst();</li>
- * <li>②匹配出现在列表中的某个中间位置,此时执行的操作是 --> prev.next = curr.next;,</li></ul>
- * <p>在列表中定位某个结点需要两个引用:一个对前一结点(prev左)的引用以及一个对当前结点(curr右)的引用.</p>
- * prev = curr;
- * curr = curr.next;
- */
- public void remove(T item) {
- Node<T> curr = head, prev = null;
- boolean found = false;
- while (curr != null && !found) {
- if (item.equals(curr.nodeValue)) {
- if(prev == null) removeFirst();
- else prev.next = curr.next;
- found = true;
- } else {
- prev = curr;
- curr = curr.next;
- }
- }
- }
- /**
- * 返回此列表中首次出现的指定元素的索引,如果列表中不包含此元素,则返回 -1.
- */
- public int indexOf(T item) {
- int index = 0;
- Node<T> p;
- for(p = head; p != null; p = p.next) {
- if(item.equals(p.nodeValue))
- return index;
- index++;
- }
- return -1;
- }
- /**
- * 如果此列表包含指定元素,则返回 true。
- */
- public boolean contains(T item) {
- return indexOf(item) != -1;
- }
- public static void main(String[] args) {
- SingleLinkedList<String> t = new SingleLinkedList<String>();
- t.addToHead("A");
- //t.addFirst("addFirst");
- t.addToTail("B");
- t.addToTail("C");
- System.out.println(t.indexOf("C")); // 2
- System.out.println(t.contains("A")); // true
- //t.addLast("addLast");
- //t.removeLast();
- //t.insert("B", "insert");
- //t.removeFirst();
- //t.remove("B"); // A C
- t.printList(); // A B C
- }
- }
双向链表
package datastruct;
/*
*双向链表的的结点
*/
public class DoubleNode {
private DoubleNode previous;// 指向前一个接点的指针
private int value;// 接点保存的值
private DoubleNode next;// 指向下一个接点的指针
public DoubleNode(int value) {
super();
this.value = value;
this.previous = this;//初始时指向自身
this.next = this;//初始时指向自身
}
public DoubleNode() {
// TODO Auto-generated constructor stub
}
public DoubleNode getPrevious() {
return previous;
}
public void setPrevious(DoubleNode previous) {
this.previous = previous;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public DoubleNode getNext() {
return next;
}
public void setNext(DoubleNode next) {
this.next = next;
}
}
package datastruct;
/**
* 双向链表
* @author dell
*
*/
public class MyDoubleList {
private DoubleNode head;// 头结点,初始值为null
private int length = 0;// 链表的长度
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public MyDoubleList() {
head = new DoubleNode();
}
public DoubleNode getHead() {
return head;
}
public void setHead(DoubleNode head) {
this.head = head;
}
/**
* @param args
*/
public static void main(String[] args) {
MyDoubleList ml = new MyDoubleList();
ml.add(new DoubleNode(1));
ml.add(new DoubleNode(2));
ml.add(new DoubleNode(3));
ml.add(new DoubleNode(4));
ml.add(new DoubleNode(5));
ml.add(new DoubleNode(6));
ml.add(new DoubleNode(7));
ml.remove(new DoubleNode(3));
ml.insert(new DoubleNode(11), 3);
while (ml.head != null) {
System.out.println(ml.head.getValue());
ml.head = ml.head.getPrevious();
}
}
/**
* 添加新节点
*
* @param n要加入的节点
*/
public void add(DoubleNode n) {
head.setNext(n);//头结点的next指针指向n
//n的前驱指针指向head
n.setPrevious(head);
head = n;// 指针移动
this.length++;
}
/**
* 删除某个节点
*
* @param n要删除的节点
*/
public void remove(DoubleNode n) {
if (head == null)
return;
if (n.getValue() == this.head.getValue()) {
head = head.getPrevious();
head.setNext(null);
// head.setp
return;
}
DoubleNode pre = head;// 当前节点,初始值为头结点
DoubleNode cur = head.getPrevious();// 当前节点的前驱节点
while (cur != null) {
if (cur.getValue() == n.getValue()) {
pre.setPrevious(cur.getPrevious());
cur.getPrevious().setNext(pre);
// pre.getNext().setNext(cur.getNext());
return;//
}
pre = pre.getPrevious();// 移动指针
cur = cur.getPrevious();// 移动指针
}
}
/**
*插入节点
*/
public void insert(DoubleNode n, int i) {
if (head == null) {
add(n);
return;
}
DoubleNode pre = head;
DoubleNode cur = head.getPrevious();
int j = 1;
while (cur != null) {
if (j == i-1) {
cur.setNext(n);
n.setPrevious(cur);
n.setNext(pre);
pre.setPrevious(n);
return;
}
pre = pre.getPrevious();// 移动指针
cur = cur.getPrevious();// 移动指针
j++;
}
}
}