您的位置:首页 > 其它

单链表的简单实现

2014-12-03 15:43 267 查看
节点类:

package net.hkh.classic.list;

import java.io.Serializable;

/**
* @desc: 节点类
* @author: houkh
* @date: Dec 3, 2014
*/
public class Node implements Comparable<Object>, Serializable {

private static final long serialVersionUID = 1L;

private Node next;
private Object value;

public Node() {
next = null;
value = null;
}

public Node(Object value) {
next = null;
this.value = value;
}

public Node(Node next, Object value) {
this.next = next;
this.value = value;
}

public Node getNext() {
return next;
}

public void setNext(Node next) {
this.next = next;
}

public Object getValue() {
return value;
}

public void setValue(Object value) {
this.value = value;
}

public int compareTo(Object node) {
if (node instanceof Node) {
Node nodeTwo = (Node) node;
String valueTwo = nodeTwo.getValue().toString();
String valueOne = value.toString();
try {
Integer intOne = Integer.valueOf(valueOne);
Integer intTwo = Integer.valueOf(valueTwo);
return intOne - intTwo;
} catch (Exception e) {

}
return valueOne.compareTo(valueTwo);
} else {
System.out.println("Must be a Node");
return 0;
}
}

public boolean equals(Object value) {
if (value.toString().equals(this.value.toString())) {
return true;
}
return false;
}

public String toString() {
return value.toString();
}
}


单链表类:
package net.hkh.classic.list;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import net.hkh.util.BeanUtils;
import net.hkh.util.SortUtils;

/**
* @desc:单链表
* @author: houkh
* @date: Dec 3, 2014
*/
public class SingleLinkedList implements Serializable {
private static final long serialVersionUID = 1L;

private Node head;// 头结点
private Node tail;// 尾结点
private int length;// 长度

public SingleLinkedList() {
this.head = null;
this.tail = null;
this.length = 0;
}

public void addHead(Node node) {
node.setNext(head);
head = node;
length++;
}

public void addTail(Node node) {
if (tail != null) {
tail.setNext(node);
tail = node;
} else {
head = tail = node;
}
length++;
}

public void addNode(int index, Node node) {
if (index >= length) {
System.out.println("Out of index");
return;
} else {
if (index == 0) {
addHead(node);
} else if (index == length - 1) {
addTail(node);
} else {
Node curNode = head;
while (index > 0) {
curNode = curNode.getNext();
index--;
}
node.setNext(curNode.getNext());
curNode.setNext(node);
length++;
}
}
}

public void delHead() {
if (head == null) {
System.out.println("Null Pointer");
return;
} else if (length == 1) {
head = tail = null;
length = 0;
return;
}
head = head.getNext();
length--;
}

public void delTail() {
if (head == null) {
System.out.println("Null Pointer");
return;
} else if (length == 1) {
head = tail = null;
length = 0;
return;
} else {
Node curNode = head;
while (curNode.getNext().getNext() != null) {
curNode = curNode.getNext();
}
curNode.setNext(null);
tail = curNode;
length--;
}
}

public void delNode(int index) {
if (index >= length) {
System.out.println("Out of index");
return;
}
if (index == 0) {
delHead();
return;
}
if (index == length - 1) {
delTail();
return;
}
Node curNode = head;
while (index > 1) {
curNode = curNode.getNext();
index--;
}
Node next = curNode.getNext().getNext();
curNode.getNext().setNext(null);
curNode.setNext(next);
length--;
}

public void setHead(Object value) {
head.setValue(value);
}

public void setTail(Object value) {
tail.setValue(value);
}

public void setNode(int index, Object value) {
Node curNode = getNode(index);
curNode.setValue(value);
}

public Node getHead() {
return head;
}

public Node getTail() {
return tail;
}

public Node getNode(int index) {
if (index >= length) {
System.out.println("Out of index");
return null;
}
if (index == 0) {
return getHead();
}
if (index == length - 1) {
return getTail();
}
Node curNode = head;
while (index > 0) {
curNode = curNode.getNext();
index--;
}
return curNode;
}

public int getLength() {
return length;
}

/**
* @desc 检查某值是否在链表中
* @param value
* @return
*
*/
public boolean contains(Object value) {
Node curNode = head;
while (curNode.getNext() != null) {
if (curNode.getValue().equals(value)) {
return true;
}
curNode = curNode.getNext();
}
return false;
}

/**
* @desc 如果node在list中存在,则返回第一次出现的位置,否则返回-1
* @param node
* @return
*
*/
public int indexOf(Object value) {
Node curNode = head;
int index = 0;
while (curNode.getNext() != null) {
if (curNode.getValue().equals(value)) {
return index;
}
curNode = curNode.getNext();
index++;
}
return -1;
}

/**
* @desc 交换两个节点的值
* @param one
* @param another
*
*/
public void swap(int one, int another) {
if (one >= length || another >= length) {
System.out.println("Out of index");
return;
}
Node oneNode = head;
Node anotherNode = head;
while (one > 0) {
one--;
oneNode = oneNode.getNext();
}
while (another > 0) {
another--;
anotherNode = anotherNode.getNext();
}
Object tmp = oneNode.getValue();
oneNode.setValue(anotherNode.getValue());
anotherNode.setValue(tmp);
}

/**
* @desc 正序排序
*
*/
public void sort() {
List<Node> list = new ArrayList<Node>();
Node curNode = head;
while (curNode.getNext() != null) {
list.add(curNode);
curNode = curNode.getNext();
}
list.add(curNode);
SortUtils.sort(list);
for (int i = 0; i < list.size(); i++) {
this.setNode(i, list.get(i).getValue());
}
}

/**
* @desc 逆序排序
*
*/
public void reverseSort() {
List<Node> list = new ArrayList<Node>();
Node curNode = head;
while (curNode.getNext() != null) {
list.add(curNode);
curNode = curNode.getNext();
}
list.add(curNode);
SortUtils.reverseSort(list);
for (int i = 0; i < list.size(); i++) {
this.setNode(i, list.get(i).getValue());
}
}

public void print() {
Node curNode = head;
while (curNode.getNext() != null) {
System.out.print(curNode.getValue() + "->");
curNode = curNode.getNext();
}
System.out.print(curNode.getValue() + "\n");
}

public boolean equals(Object value) {
if (value instanceof SingleLinkedList) {
SingleLinkedList list = (SingleLinkedList) value;
int len = list.length > this.length ? this.length : list.length;
for (int i = 0; i < len; i++) {
Node one = this.getNode(i);
Node two = list.getNode(i);
if (!one.equals(two)) {
return false;
}
}
if (this.length == list.length) {
return true;
} else {
return false;
}
} else {
return false;
}
}

public static void main(String[] args) throws Exception {
SingleLinkedList list = new SingleLinkedList();
list.addTail(new Node(1));
list.addTail(new Node(2));
list.addTail(new Node(3));
list.addHead(new Node(4));
list.addHead(new Node(5));
list.addNode(0, new Node(6));
list.addNode(5, new Node(7));
list.addNode(3, new Node(8));
list.addNode(5, new Node(9));
list.print();
System.out.println(list.getLength());
SingleLinkedList anotherList = (SingleLinkedList) BeanUtils
.cloneTo(list);
System.out.println(list == anotherList);
System.out.println(list.equals(anotherList));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  单链表