java 将对象写入链表_java – 如何将对象添加到链表中?

我一直致力于一个项目,我必须实现一个实现双链表使用的

java类.我用我的所有方法完成了LinkedList类.我只是不确定如何将节点对象实际添加到列表中.到目前为止,这是我的代码,底部是测试.任何帮助,将不胜感激.谢谢

public class LinkedList {

private Node first;

private Node current;

private Node last;

private int currentIndex;

private int numElements;

public LinkedList() {

this.first = null;

this.last = null;

this.numElements = 0;

this.current = null;

this.currentIndex = -1;

}

private class Node {

Node next;

Node previous;

Object data;

}

public boolean hasNext() {

return (current != null && current.next != null);

}

public Object next() {

if (!this.hasNext()) {

throw new IllegalStateException("No next");

}

current = current.next;

return current.data;

}

public boolean hasPrevious() {

return (current != null && current.previous != null);

}

public Object previous() {

if (!this.hasPrevious()) {

throw new IllegalStateException("No previous");

}

current = current.previous;

return current.data;

}

int nextIndex() {

int index = numElements;

if (hasNext()) {

index = this.currentIndex + 1;

}

System.out.println(index + "The current index is " + current);

return index;

}

int previousIndex() {

int index = -1;

if (hasPrevious()) {

index = this.currentIndex - 1;

}

System.out.println(index + "The current index is " + current);

return index;

}

public void set(Object o) {

if (this.current == null) {

throw new IllegalStateException("No node found, cannot set.");

}

current.data = o;

}

public int size() {

return numElements;

}

public void add(Object o) {

Node newNode = new Node();

newNode.data = o;

if (first == null) {

first = newNode;

last = newNode;

newNode.next = null;

} else if (first != null) {

if (current == null) {

newNode.previous = null;

newNode.next = first;

first.previous = newNode;

first = newNode;

} else if (current == last) {

newNode.previous = current;

newNode.next = null;

current.next = newNode;

last = newNode;

} else {

newNode.previous = current;

newNode.next = current.next;

current.next.previous = newNode;

current.next = newNode;

}

}

current = newNode;

numElements++;

currentIndex++;

}

public void remove() {

if (current != null) {

if (current == first && current == last) {

first = null;

last = null;

} else if (current == last) {

current.previous = null;

last = current.previous;

} else if (current == last) {

current.previous.next = null;

last = current.previous;

} else {

current.previous.next = current.next;

current.next.previous = current.previous;

}

current = current.next;

numElements--;

}

}

}

import java.util.Scanner;

public class LinkedListTest {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

String name;

int index;

LinkedList listOne = new LinkedList();

listOne.add(object o);

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值