package com.dby.link;
/** * Created by suzunshou on 2016/4/11. * 链表的元素不能随机访问 * 元素前面和后面不会出现多个元素相连的情况 * 元素相互依赖,串联而成 * 链表只有一个表头 */
public class LinkedList {
private class Node {
private Object data;
private Node next = null;
public Node(Object data) {
this.data = data;
}
}
private Node first = null;
private Node last = null;
private int size;
public void insertFirst(Object data) {
Node newNode = new Node(data);
newNode.next = first;
first = newNode;
Node p = first;
Node q = null;
while (p != null) {
q = p;
p = p.next;
}
last = q;
size++;
}
public void insertLast(Object data) {
Node newNode = new Node(data);
if (first == null) {
first = newNode;
} else {
Node p = first;
Node q = null;
while (p != null) {
q = p;
p = p.next;
}
q.next = newNode;
last = newNode;
}
size++;
}
public void insert(Object data, int index) {
Node newNode = new Node(data);
if (first == null) {
first = newNode;
return;
}
if (index - 1 == 0) {
newNode.next = first;
first = newNode;
return;
}
Node curr = first;
int count = 0;
while (curr.next != null && count < index - 2) {
curr = curr.next;
count++;
}
if (count == index - 2) {
newNode.next = curr.next;
curr.next = newNode;
}
}
public void findAll() {
if (first == null)
return;
Node cur = first;
while (cur != null) {
System.out.print(cur.data + "->");
cur = cur.next;
}
System.out.println("\n");
}
public void reverse() {
if (first == null) {
return;
}
Node next_node, curr_node;
curr_node = first.next;
first.next = null;
while (curr_node != null) {
next_node = curr_node.next;
curr_node.next = first;
first = curr_node;
curr_node = next_node;
}
}
public void deleteFirst() {
if (first == null)
return;
first = first.next;
size--;
}
public void deleteLast() {
if (first == null)
return;
if (first.next == null) {
first = null;
size--;
} else {
Node p = first;
Node q = null;
while (p.next != null) {
q = p;
p = p.next;
}
q.next = null;
size--;
}
}
public Node findOne(Object data) {
if (first == null)
return null;
Node cur = first;
while (cur != null) {
if (cur.data.equals(data)) {
return cur;
} else {
cur = cur.next;
}
}
return null;
}
public Node findByIndex(int index) {
if (index - 1 > size) {
} else {
Node curr = first;
for (int i = 1; i < index; i++) {
curr = curr.next;
}
return curr;
}
return null;
}
public boolean isEmpty() {
return (size == 0);
}
public void remove(Object data) {
if (first == null) {
return;
}
if (first.data.equals(data)) {
first = first.next;
size--;
} else {
Node prev = first;
Node cur = first.next;
while (cur != null) {
if (cur.data.equals(data)) {
prev.next = cur.next;
size--;
}
prev = cur;
cur = cur.next;
}
}
}
public void removeByIndex(int index) {
if (first == null) {
return;
}
Node curr = first;
int count = 0;
if (index - 1 == 0) {
first = first.next;
return;
}
while (curr.next != null && count < index - 2) {
curr = curr.next;
count++;
}
if (count == index - 2 && curr.next != null) {
Node delete_node = curr.next;
curr.next = delete_node.next;
delete_node = null;
}
size--;
}
public static void main(String[] args) {
LinkedList ll = new LinkedList();
for (int i = 1; i < 8; i++) {
ll.insertLast(i);
}
ll.removeByIndex(2);
ll.removeByIndex(4);
ll.findAll();
System.out.println(ll.findByIndex((ll.size + 1) / 2).data);
ll.insert(2,2);
ll.insertFirst(0);
System.out.println(ll.findOne(2).data);
ll.findAll();
}
}