MyLinkedList
虽然javaApi中提供了LinkedList类,但是想自定义一个MyLinkedList,可以存Object(除过null)。
定义节点:
public class Node {
public Object data;
public Node next;
}
定义List接口:
public interface List {
/**
* 头插法
* @param obj
*/
public void addFirst(Object obj);
/**
* 尾插法
* @param obj
*/
public void addLast(Object obj);
/**
* 获取链表中元素个数
* @return
*/
public int getSize();
/**
* 删除链表元素
* @param obj
*/
public void romove(Object obj);
/**
* 头删法
*/
public void removeFirst();
/**
* 尾删法
*/
public void removeLast();
/**
* 查找链表中的某个元素
* @return
*/
public Object find(Object obj);
/**
* 打印链表所有元素
*/
public void findAll();
}
MyLinkedList实现类:
public class MyLinkedList implements List {
//定义头节点
public Node Head = null;
//元素个数
private int size = 0;
@Override
public void addFirst(Object obj) {
//如果链表为空
if (Head == null) {
Head = new Node();
Head.data = obj;
size++;
return;
}
Node p = Head;
Head = new Node();
Head.data = obj;
Head.next = p;
size++;
}
/*所有元素后移,时间复杂度过高,摒弃
@Override
public void addFirst(Object obj) {
//如果链表为空
if (Head == null) {
Head = new Node();
Head.data = obj;
size++;
return;
}
Node p = null;
Node q = null;
while (true) {
q = Head;
//当后面的节点找上来等于头节点,跳出
if (Head == p) {
break;
}
while (true) {
//找到最后一个不为空的节点,新建一个节点,将这个不为空的值赋给新建的节点,然后将此节点(找到不为空的节点)的下一个指向新建的节点
if (q.next == null) {
p = q;
Node r = new Node();
r.data = p.data;
size++;
p.next = r;
break;
}
//将前面的节点值赋给后面的节点
if (q.next == p) {
q.next = p;
p.data = q.data;
p = q;
break;
}
q = q.next;
}
}
//将值赋给头节点
Head.data = obj;
}
*/
@Override
public void addLast(Object obj) {
//如果链表为空
if (Head == null) {
Head = new Node();
Head.data = obj;
size++;
return;
}
Node p = Head;
while (true) {
//找到最后一个不为空的节点,新建一个节点,将这个不为空的值赋给新建的节点,然后将此节点(找到不为空的节点)的下一个指向新建的节点
if (p.next == null) {
Node q = p;
p = p.next;
p = new Node();
p.data = obj;
q.next = p;
size++;
return;
}
p = p.next;
}
}
@Override
public int getSize() {
//返回节点数,注意,插入节点++,删除节点--
return size;
}
@Override
public void romove(Object obj) {
if (Head == null){
System.out.println("删除失败,链表为空");
}
Node p = Head;
while (true) {
if (p.next == null) {
System.out.println("删除元素不存在");
return;
}
if(p.next.data.equals(obj)){
p.next = p.next.next;
size--;
return;
}
p = p.next;
}
}
@Override
public void removeFirst() {
if (Head == null){
System.out.println("删除失败,链表为空");
return;
}
if(Head.next == null){
Head = null;
return;
}
Head = Head.next;
size--;
}
@Override
public void removeLast() {
if (Head == null){
System.out.println("删除失败,链表为空");
return;
}
Node p = Head;
while (true){
if (p.next.next == null) {
p.next = null;
size--;
return;
}
p = p.next;
}
}
@Override
public Object find(Object obj) {
//从头节点开始遍历链表,找不到,返回null
Node p = Head;
while (true) {
if (p.next == null) {
return null;
}
if(p.data.equals(obj)){
return p.data;
}
p = p.next;
}
}
@Override
public void findAll() {
Node p = Head;
while (true) {
if (p == null) {
System.out.println("null");
break;
}
System.out.print(p.data + "->");
p = p.next;
}
}
}
测试类:
public class MyLikedListTest {
public static MyLinkedList list = new MyLinkedList();
public static void main(String[] args) {
list.addLast(123);
list.addLast(456);
list.addLast("sdjn");
list.findAll();
System.out.println(list.getSize());
list.addFirst(963);
list.addFirst(true);
list.addFirst(0.56);
list.findAll();
System.out.println(list.getSize());
list.romove(123);
list.findAll();
System.out.println(list.getSize());
list.removeFirst();
list.findAll();
list.removeLast();
list.findAll();
}
}
输出结果:
123->456->sdjn->null
3
0.56->true->963->123->456->sdjn->null
6
0.56->true->963->456->sdjn->null
5
true->963->456->sdjn->null
true->963->456->null