java实现linklist,JAVA 手动实现LinkedList

JAVA 手动实现LinkedList

节点

package com.ghg.data_structure.link;

public class Node {

/**

* 数据

*/

public T data;

/**

* 前一个

*/

public Node pre;

/**

* 后一个

*/

public Node next;

public Node() {

super();

}

/**

*

* @param data 数据

* @param pre 前一个

*/

public Node(T data, Node pre, Node next) {

super();

this.data = data;

this.pre = pre;

this.next = next;

}

public T getData() {

return data;

}

public void setData(T data) {

this.data = data;

}

public Node getPre() {

return pre;

}

public void setPre(Node pre) {

this.pre = pre;

}

public Node getNext() {

return next;

}

public void setNext(Node next) {

this.next = next;

}

@Override

public String toString() {

return data+" ";

}

}

List接口

package com.ghg.data_structure.link;

public interface MyList {

/**

* 是否为空

* @return

*/

public boolean isEmpty();

/**

* 大小

* @return

*/

public int size();

/**

* 添加到第一个

* @param t

* @return

*/

public boolean addFirst(T t);

/**

* 获取第一个

* @return

*/

public T getFirst();

/**

* 添加到最后

* @param t

* @return

*/

public boolean addLast(T t);

/**

* 获取最后一个元素

* @return

*/

public T getLast();

/**

* 添加默认添加到最后

* @param t

* @return

*/

public boolean add(T t);

/**

* 添加到指定位置

* @param index 索引

* @param t 数据

* @return

*/

public boolean add(int index, T t);

/**

* 获取指定位置的元素

* @param index 索引

* @return

*/

public T get(int index);

/**

* 移除指定元素

* @param index

* @return

*/

public T remove(int index);

/**

* 移除第一个

* @return

*/

public boolean removeFirst();

/**

* 移除最后一个

* @return

*/

public boolean removeLast();

/**

* 是否包含

* @param object

* @return

*/

public boolean contains(Object object);

/**

* 获取迭代器

* @return

*/

public MyIterator iterator();

}

迭代器接口

package com.ghg.data_structure.link;

public interface MyIterator {

public boolean hasNext();

public T next();

public boolean hasPrevious();

public T previous();

}

实现

package com.ghg.data_structure.link;

import java.util.NoSuchElementException;

public class MyLinkedList implements MyList {

private int size = 0;

private Node first;

private Node last;

private int position = 0;

public MyLinkedList() {

this.first = null;

this.last = null;

}

public boolean isEmpty() {

return size == 0;

}

public int size() {

return size;

}

public boolean addFirst(T t) {

Node f = first;

Node newNode = new Node(t, null, first);

first = newNode;

if (f == null) {

last = newNode;

} else {

f.pre = newNode;

}

size++;

return true;

}

public T getFirst() {

return first.data;

}

public boolean addLast(T t) {

Node l = last;

Node newNode = new Node(t, l, null);

last = newNode;

if (l == null) {

first = newNode;

} else {

l.next = newNode;

}

size++;

return true;

}

public T getLast() {

return last.data;

}

public boolean add(T t) {

return addLast(t);

}

public boolean add(int index, T t) {

if (index < 0 || index > size) {

throw new IndexOutOfBoundsException("index : " + index + " size : " + size);

}

if (index == size) {

return addLast(t);

}

Node current = first;

for (int i = 0; i < index; i++) {

current = current.next;

}

Node pre = current.pre;

Node newNode = new Node(t, pre, current);

current.pre = newNode;

/**

* 如果没有前置元素说明是第一个

*/

if (pre == null) {

first = newNode;

} else {

pre.next = newNode;

}

size++;

return true;

}

public T get(int index) {

if (index < 0 || index >= size) {

throw new IndexOutOfBoundsException("index : " + index + " size : " + size);

}

Node current = first;

for (int i = 0; i < index; i++) {

current = current.next;

}

return current.data;

}

@Override

public String toString() {

Node current = first;

StringBuilder sb = new StringBuilder();

for (int i = 0; i < size; i++) {

sb.append(current);

current = current.next;

}

return sb.toString();

}

public T remove(int index) {

if (index < 0 || index >= size) {

throw new IndexOutOfBoundsException("index : " + index + " size : " + size);

}

Node current = first;

for (int i = 0; i < index; i++) {

current = current.next;

}

/**

* 获取当前元素的前置也后置元素,将这2个元素相连接

*/

final Node next = current.next;

final Node prev = current.pre;

if (prev == null) {

first = next;

} else {

prev.next = next;

}

if (next == null) {

last = prev;

} else {

next.pre = prev;

}

size--;

return current.data;

}

/**

* 移除第一个

*/

public boolean removeFirst() {

/**

* 获取第一个元素

*/

Node current = first;

/**

* 获取第一个元素的下一个元素

*/

Node next = current.next;

/**

* 赋值

*/

first = next;

/**

* 判断下一个是不是空,如果为空就说明是最后一个,目前只有一个元素

*/

if (next == null) {

last = null;

} else {

// 前置设置为空,因为是第一个了

next.pre = null;

}

size--;

return true;

}

/**

* 移除最后个

*/

public boolean removeLast() {

/**

* 获取最后一个元素

*/

Node current = last;

/**

* 获取最后一个的前一个元素

*/

Node pre = current.pre;

/**

* 赋值

*/

last = pre;

/**

* 判断前置是不是空,如果为空说明第一个为NULL

*/

if (pre == null) {

first = null;

} else {

// 将最后一个元素的,后置设置为空

pre.next = null;

}

size--;

return true;

}

public boolean contains(Object object) {

if (object == null) {

return false;

}

int index = 0;

for (Node current = first; current.next != null; current = current.next) {

if (object.equals(current.data)) {

break;

}

index++;

}

System.out.println("contains index: " + index);

return index > 0;

}

/*

*//**

* 是否有更多

*//*

public boolean hasNext() {

if (position < size) {

return true;

} else {

position = 0;

return false;

}

}

*//**

* 下一个元素

*//*

public T next() {

if (!hasNext()) {

throw new NoSuchElementException();

}

return get(position++);

}*/

public class MyInnerIterator implements MyIterator {

private MyLinkedList myLinkedList;

/**

* 游标

*/

private int cursor = 0;

public MyInnerIterator(MyLinkedList linkedList) {

this.myLinkedList = linkedList;

}

public boolean hasNext() {

return cursor

}

public T next() {

return myLinkedList.get(cursor++);

}

public boolean hasPrevious() {

System.out.println("cursor "+cursor);

return cursor>0;

}

public T previous() {

return myLinkedList.get(--cursor);

}

}

/**

* 获取迭代器

*/

public MyIterator iterator() {

return new MyInnerIterator(this);

}

}

测试

package com.ghg.data_structure.link;

public class MyListTest1 {

public static void main(String[] args) {

MyLinkedList myLinkedList = new MyLinkedList();

myLinkedList.addFirst(3);

myLinkedList.addFirst(5);

System.out.println("\n 全部: " + myLinkedList.toString());

System.out.println("\n 获取第一个: " + myLinkedList.getFirst());

System.out.println("\n 获取最后一个: " + myLinkedList.getLast());

myLinkedList.addLast(9);

myLinkedList.addLast(-1);

System.out.println("\n 全部: " + myLinkedList.toString());

System.out.println("\n size : " + myLinkedList.size());

myLinkedList.addFirst(67);

System.out.println("\n 全部: " + myLinkedList.toString());

System.out.println("\n index : " + myLinkedList.get(1));

myLinkedList.add(45);

myLinkedList.add(-80);

System.out.println("\n 全部: " + myLinkedList.toString());

myLinkedList.add(0, 45);

System.out.println("\n 全部: " + myLinkedList.toString());

myLinkedList.add(8, 43);

System.out.println("\n 全部: " + myLinkedList.toString());

myLinkedList.add(1, 33);

myLinkedList.add(6, 12345);

myLinkedList.add(11, 77);

System.out.println("\n 全部: " + myLinkedList.toString());

myLinkedList.removeFirst();

myLinkedList.removeFirst();

System.out.println("\n 全部: " + myLinkedList.toString());

System.out.println("\n size: " + myLinkedList.size());

myLinkedList.removeLast();

myLinkedList.removeLast();

System.out.println("\n 全部: " + myLinkedList.toString());

System.out.println("\n size: " + myLinkedList.size());

System.out.println("remove :" + myLinkedList.remove(3));

System.out.println("\n 全部: " + myLinkedList.toString());

System.out.println("\n size: " + myLinkedList.size());

System.out.println("\n 全部: " + myLinkedList.toString());

System.out.println("\n contains: " + myLinkedList.contains(3));

System.out.println("get "+myLinkedList.get(6));

/*System.out.println("\n 全部: " + myLinkedList.hasNext());

System.out.println("\n while ");

while(myLinkedList.hasNext()){

System.out.print(myLinkedList.next()+" \t");

}*/

//System.out.println("\n 全部: " + myLinkedList.hasNext());

System.out.println("\n 全部: iterator================ " );

MyIterator iterator = myLinkedList.iterator();

while(iterator.hasNext()){

System.out.print(iterator.next()+" \t");

}

MyIterator iterator1 = myLinkedList.iterator();

//System.out.println("\n hasnext: "+iterator1.hasNext()+" size "+myLinkedList.size());

System.out.println("\n next : "+iterator1.next());

System.out.println("next : "+iterator1.next());

System.out.println("hasPrevious : "+iterator1.hasPrevious());

System.out.println("previous :"+iterator1.previous());

}

}

6f6a28d4c9c8

image.png

6f6a28d4c9c8

image.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值