节点类:
/**
* @desc 节点实现
* @author houkh
*/
public class Node {
private Object data;
private Node next;
public Node() {
this.data = null;
this.next = null;
}
public Node(Object data, Node next) {
this.data = data;
this.next = next;
}
public Object getData() {
return data;
}
public void setData(Object value) {
this.data = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
@Override
public String toString() {
return ("[" + data + "," + next + "]");
}
}
链表类:
package prac;
/**
* desc 简单单链表实现
* @author houkh
*
*/
public class SingleLinkedList {
private Node head;
private Node tail;
private int length;
public SingleLinkedList() {
head = tail = null;
length = 0;
}
/**
* @desc 设置头结点
* @param head
*/
public void setHead(Node head) {
this.head = head;
}
/**
* @desc 设置尾结点
* @param tail
*/
public void setTail(Node tail) {
this.tail = tail;
}
/**
* @desc 设置单链表长度
* @param length
*/
public void setLength(int length) {
this.length = length;
}
/**
* @desc 获取头结点
* @return
*/
public Node getHead() {
return this.head;
}
/**
* @desc 获取尾结点
* @return
*/
public Node getTail() {
return this.tail;
}
/**
* @desc 获取单链表长度
* @return
*/
public int getLength() {
return this.length;
}
/**
* @desc 在开始插入节点
* @param obj
*/
public void addHead(Object obj) {
Node node = new Node();
node.setData(obj);
if (head == null) {
head = tail = node;
length++;
} else {
Node tmp = head;
head = node;
head.setNext(tmp);
length++;
}
}
/**
* @desc 在结尾插入节点
* @param obj
*/
public void addTail(Object obj) {
Node node = new Node();
node.setData(obj);
if (head == null) {
head = node;
tail = node;
length++;
} else {
tail.setNext(node);
tail = node;
length++;
}
}
/**
* @desc 在index插入节点
* @param index
* @param obj
*/
public void add(int index, Object obj) {
if (index > length) {
System.out.println("Out of bounds");
return;
}
Node node = new Node();
node.setData(obj);
Node tmp = head;
if (index == 0) {
addHead(obj);
return;
}
for (int i = 0; i < index - 1; i++) {
tmp = tmp.getNext();
}
if (tmp.getNext() == null) {
tmp.setNext(node);
length++;
} else {
node.setNext(tmp.getNext());
tmp.setNext(node);
length++;
}
}
/**
* @desc 删除头结点
*/
public void delHead() {
if (head == null) {
System.out.println("单链表为空");
return;
}
Node tmp = head.getNext();
head = tmp;
length--;
}
/**
* @desc 删除尾结点
*/
public void delTail() {
if (head == null) {
System.out.println("单链表为空");
return;
}
Node tmp = head;
while (tmp.getNext() != tail) {
tmp = tmp.getNext();
}
tail = tmp;
tail.setNext(null);
length--;
}
/**
* @desc 删除index结点
* @param index
*/
public void del(int index) {
if (head == null) {
System.out.println("单链表为空");
return;
}
if (index > length - 1) {
System.out.println("Out of bounds");
return;
}
if (index == 0) {
delHead();
return;
}
Node tmp = head;
for (int i = 0; i < index - 1; i++) {
tmp = tmp.getNext();
}
if (tmp.getNext().getNext() != null) {
tmp.setNext(tmp.getNext().getNext());
} else {
tmp.setNext(null);
}
length--;
}
/**
* @desc 设置index
* @param index
* @param obj
*/
public void set(int index, Object obj) {
if (head == null) {
System.out.println("单链表为空");
return;
}
if (index >= length) {
System.out.println("Out of bounds");
return;
}
Node tmp = head;
for (int i = 0; i < index; i++) {
tmp = tmp.getNext();
}
tmp.setData(obj);
}
/**
* @desc 检测单链表是否包含某个值
* @param obj
* @return
*/
public boolean contains(Object obj) {
if (head == null) {
System.out.println("单链表为空");
return false;
}
Node tmp = head;
while (tmp.getNext() != null) {
if (obj.equals(tmp.getData())) {
return true;
}
tmp = tmp.getNext();
}
return false;
}
/**
* @desc 获取index
* @param index
* @return
*/
public Object get(int index) {
if (head == null) {
System.out.println("单链表为空");
return null;
}
if (index >= length) {
System.out.println("Out of bounds");
return null;
}
Node tmp = head;
for (int i = 0; i < index; i++) {
tmp = tmp.getNext();
}
return tmp.getData();
}
/**
* 打印单链表
*/
public void print() {
if (head == null) {
System.out.println("单链表为空");
return;
}
Node tmp = head;
while (tmp.getNext() != null) {
System.out.print(tmp.getData().toString() + "->");
tmp = tmp.getNext();
}
System.out.print(tmp.getData().toString() + "\n");
}
public static void main(String[] args) {
SingleLinkedList list = new SingleLinkedList();
list.addHead(1);
list.addHead(2);
list.addHead(3);
list.add(1, 4);
list.add(2, 5);
list.add(3, 6);
list.add(0, 7);
list.add(0, 8);
list.print();
}
}