我正在做自己的LinkedList类,我正在尝试实现clear,但我不知道该怎么做。我想做的是遍历列表并将每个节点设置为null,但我引用了列表中的第一个节点,我听说我可以简单地执行
first = null;
这就足够了。是这样吗,还是我需要遍历列表编辑:有人建议我显示代码的其余部分。并不是所有的东西都实现了,但我希望它能有所帮助。
import java.util.Iterator;
public class MyALDAList <E> implements ALDAList<E> {
@Override
public void add(E element) {
if (first == null) {
first = last = new MyNode<E>(element, null);
}
else {
MyNode<E> newNode = new MyNode<E> (element, null);
last.next = newNode;
last = newNode;
}
lSize++;
modCnt++;
}
@Override
public void add(int index, E element) {
if (index > lSize || index < 0) {
throw new IndexOutOfBoundsException();
}
else {
MyNode<E> newNode = new MyNode<E>(element, null);
if (first == null) {
first = last = newNode;
}
else if (index == 0) {
newNode.next = first;
first = newNode;
}
else {
MyNode<E> indexNode = first;
MyNode<E> prevNode = null;
for (int i = 0; i < index; i++) {
prevNode = indexNode;
indexNode = indexNode.next;
}
prevNode.next = newNode;
newNode.next = indexNode;
}
lSize++;
modCnt++;
}
}
@Override
public E remove(int index) {
E returnData = null;
if (index > lSize || index < 0) {
throw new IndexOutOfBoundsException();
}
else {
if (index == 0) {
MyNode<E> tempHolder = first;
first = first.next;
returnData = tempHolder.data;
tempHolder = null;
}
else {
MyNode<E> indexNode = first;
MyNode<E> prevNode = null;
for (int i = 0; i < index; i++) {
prevNode = indexNode;
indexNode = indexNode.next;
}
returnData = indexNode.data;
prevNode.next = indexNode.next;
indexNode = null;
}
lSize--;
modCnt++;
}
return returnData;
}
@Override
public boolean remove(E element) {
Boolean flag = false;
MyNode<E> indexNode = first;
MyNode<E> prevNode = null;
for (int i = 0; i < lSize; i++) {
if (indexNode.data.equals(element)) {
prevNode.next = indexNode.next;
indexNode = null;
lSize--;
modCnt++;
flag = true;
}
prevNode = indexNode;
indexNode = indexNode.next;
}
return flag;
}
@Override
public E get(int index) {
return null;
}
@Override
public boolean contains(E element) {
return false;
}
@Override
public int indexOf(E element) {
int indexCount = 0;
return 0;
}
@Override
public void clear() {
first = null;
}
@Override
public int size() {
return lSize;
}
@Override
public java.util.Iterator<E> iterator() {
return new MyIterator();
}
private class MyIterator implements java.util.Iterator<E> {
private MyNode<E> current = first;
private int acceptableModCnt = modCnt;
@Override
public boolean hasNext() {
return current.next != null;
}
@Override
public E next() {
if (modCnt != acceptableModCnt) {
throw new java.util.ConcurrentModificationException( );
}
if (!hasNext()) {
throw new java.util.NoSuchElementException( );
}
E returnItem = current.data;
current = current.next;
return returnItem;
}
}
private static class MyNode<E> {
public MyNode(E val, MyNode<E> nextNode) {
data = val;
next = nextNode;
}
private E data;
private MyNode<E> next;
}
int lSize = 0;
int modCnt = 0;
MyNode<E> first = null;
MyNode<E> last = null;
}
import java.util.Iterator;
public class MyALDAList <E> implements ALDAList<E> {
@Override
public void add(E element) {
if (first == null) {
first = last = new MyNode<E>(element, null);
}
else {
MyNode<E> newNode = new MyNode<E> (element, null);
last.next = newNode;
last = newNode;
}
lSize++;
modCnt++;
}
@Override
public void add(int index, E element) {
if (index > lSize || index < 0) {
throw new IndexOutOfBoundsException();
}
else {
MyNode<E> newNode = new MyNode<E>(element, null);
if (first == null) {
first = last = newNode;
}
else if (index == 0) {
newNode.next = first;
first = newNode;
}
else {
MyNode<E> indexNode = first;
MyNode<E> prevNode = null;
for (int i = 0; i < index; i++) {
prevNode = indexNode;
indexNode = indexNode.next;
}
prevNode.next = newNode;
newNode.next = indexNode;
}
lSize++;
modCnt++;
}
}
@Override
public E remove(int index) {
E returnData = null;
if (index > lSize || index < 0) {
throw new IndexOutOfBoundsException();
}
else {
if (index == 0) {
MyNode<E> tempHolder = first;
first = first.next;
returnData = tempHolder.data;
tempHolder = null;
}
else {
MyNode<E> indexNode = first;
MyNode<E> prevNode = null;
for (int i = 0; i < index; i++) {
prevNode = indexNode;
indexNode = indexNode.next;
}
returnData = indexNode.data;
prevNode.next = indexNode.next;
indexNode = null;
}
lSize--;
modCnt++;
}
return returnData;
}
@Override
public boolean remove(E element) {
Boolean flag = false;
MyNode<E> indexNode = first;
MyNode<E> prevNode = null;
for (int i = 0; i < lSize; i++) {
if (indexNode.data.equals(element)) {
prevNode.next = indexNode.next;
indexNode = null;
lSize--;
modCnt++;
flag = true;
}
prevNode = indexNode;
indexNode = indexNode.next;
}
return flag;
}
@Override
public E get(int index) {
return null;
}
@Override
public boolean contains(E element) {
return false;
}
@Override
public int indexOf(E element) {
int indexCount = 0;
return 0;
}
@Override
public void clear() {
first = null;
}
@Override
public int size() {
return lSize;
}
@Override
public java.util.Iterator<E> iterator() {
return new MyIterator();
}
private class MyIterator implements java.util.Iterator<E> {
private MyNode<E> current = first;
private int acceptableModCnt = modCnt;
@Override
public boolean hasNext() {
return current.next != null;
}
@Override
public E next() {
if (modCnt != acceptableModCnt) {
throw new java.util.ConcurrentModificationException( );
}
if (!hasNext()) {
throw new java.util.NoSuchElementException( );
}
E returnItem = current.data;
current = current.next;
return returnItem;
}
}
private static class MyNode<E> {
public MyNode(E val, MyNode<E> nextNode) {
data = val;
next = nextNode;
}
private E data;
private MyNode<E> next;
}
int lSize = 0;
int modCnt = 0;
MyNode<E> first = null;
MyNode<E> last = null;
}
import java.util.Iterator;
public class MyALDAList <E> implements ALDAList<E> {
@Override
public void add(E element) {
if (first == null) {
first = last = new MyNode<E>(element, null);
}
else {
MyNode<E> newNode = new MyNode<E> (element, null);
last.next = newNode;
last = newNode;
}
lSize++;
modCnt++;
}
@Override
public void add(int index, E element) {
if (index > lSize || index < 0) {
throw new IndexOutOfBoundsException();
}
else {
MyNode<E> newNode = new MyNode<E>(element, null);
if (first == null) {
first = last = newNode;
}
else if (index == 0) {
newNode.next = first;
first = newNode;
}
else {
MyNode<E> indexNode = first;
MyNode<E> prevNode = null;
for (int i = 0; i < index; i++) {
prevNode = indexNode;
indexNode = indexNode.next;
}
prevNode.next = newNode;
newNode.next = indexNode;
}
lSize++;
modCnt++;
}
}
@Override
public E remove(int index) {
E returnData = null;
if (index > lSize || index < 0) {
throw new IndexOutOfBoundsException();
}
else {
if (index == 0) {
MyNode<E> tempHolder = first;
first = first.next;
returnData = tempHolder.data;
tempHolder = null;
}
else {
MyNode<E> indexNode = first;
MyNode<E> prevNode = null;
for (int i = 0; i < index; i++) {
prevNode = indexNode;
indexNode = indexNode.next;
}
returnData = indexNode.data;
prevNode.next = indexNode.next;
indexNode = null;
}
lSize--;
modCnt++;
}
return returnData;
}
@Override
public boolean remove(E element) {
Boolean flag = false;
MyNode<E> indexNode = first;
MyNode<E> prevNode = null;
for (int i = 0; i < lSize; i++) {
if (indexNode.data.equals(element)) {
prevNode.next = indexNode.next;
indexNode = null;
lSize--;
modCnt++;
flag = true;
}
prevNode = indexNode;
indexNode = indexNode.next;
}
return flag;
}
@Override
public E get(int index) {
return null;
}
@Override
public boolean contains(E element) {
return false;
}
@Override
public int indexOf(E element) {
int indexCount = 0;
return 0;
}
@Override
public void clear() {
first = null;
}
@Override
public int size() {
return lSize;
}
@Override
public java.util.Iterator<E> iterator() {
return new MyIterator();
}
private class MyIterator implements java.util.Iterator<E> {
private MyNode<E> current = first;
private int acceptableModCnt = modCnt;
@Override
public boolean hasNext() {
return current.next != null;
}
@Override
public E next() {
if (modCnt != acceptableModCnt) {
throw new java.util.ConcurrentModificationException( );
}
if (!hasNext()) {
throw new java.util.NoSuchElementException( );
}
E returnItem = current.data;
current = current.next;
return returnItem;
}
}
private static class MyNode<E> {
public MyNode(E val, MyNode<E> nextNode) {
data = val;
next = nextNode;
}
private E data;
private MyNode<E> next;
}
int lSize = 0;
int modCnt = 0;
MyNode<E> first = null;
MyNode<E> last = null;
}
import java.util.Iterator;
public class MyALDAList <E> implements ALDAList<E> {
@Override
public void add(E element) {
if (first == null) {
first = last = new MyNode<E>(element, null);
}
else {
MyNode<E> newNode = new MyNode<E> (element, null);
last.next = newNode;
last = newNode;
}
lSize++;
modCnt++;
}
@Override
public void add(int index, E element) {
if (index > lSize || index < 0) {
throw new IndexOutOfBoundsException();
}
else {
MyNode<E> newNode = new MyNode<E>(element, null);
if (first == null) {
first = last = newNode;
}
else if (index == 0) {
newNode.next = first;
first = newNode;
}
else {
MyNode<E> indexNode = first;
MyNode<E> prevNode = null;
for (int i = 0; i < index; i++) {
prevNode = indexNode;
indexNode = indexNode.next;
}
prevNode.next = newNode;
newNode.next = indexNode;
}
lSize++;
modCnt++;
}
}
@Override
public E remove(int index) {
E returnData = null;
if (index > lSize || index < 0) {
throw new IndexOutOfBoundsException();
}
else {
if (index == 0) {
MyNode<E> tempHolder = first;
first = first.next;
returnData = tempHolder.data;
tempHolder = null;
}
else {
MyNode<E> indexNode = first;
MyNode<E> prevNode = null;
for (int i = 0; i < index; i++) {
prevNode = indexNode;
indexNode = indexNode.next;
}
returnData = indexNode.data;
prevNode.next = indexNode.next;
indexNode = null;
}
lSize--;
modCnt++;
}
return returnData;
}
@Override
public boolean remove(E element) {
Boolean flag = false;
MyNode<E> indexNode = first;
MyNode<E> prevNode = null;
for (int i = 0; i < lSize; i++) {
if (indexNode.data.equals(element)) {
prevNode.next = indexNode.next;
indexNode = null;
lSize--;
modCnt++;
flag = true;
}
prevNode = indexNode;
indexNode = indexNode.next;
}
return flag;
}
@Override
public E get(int index) {
return null;
}
@Override
public boolean contains(E element) {
return false;
}
@Override
public int indexOf(E element) {
int indexCount = 0;
return 0;
}
@Override
public void clear() {
first = null;
}
@Override
public int size() {
return lSize;
}
@Override
public java.util.Iterator<E> iterator() {
return new MyIterator();
}
private class MyIterator implements java.util.Iterator<E> {
private MyNode<E> current = first;
private int acceptableModCnt = modCnt;
@Override
public boolean hasNext() {
return current.next != null;
}
@Override
public E next() {
if (modCnt != acceptableModCnt) {
throw new java.util.ConcurrentModificationException( );
}
if (!hasNext()) {
throw new java.util.NoSuchElementException( );
}
E returnItem = current.data;
current = current.next;
return returnItem;
}
}
private static class MyNode<E> {
public MyNode(E val, MyNode<E> nextNode) {
data = val;
next = nextNode;
}
private E data;
private MyNode<E> next;
}
int lSize = 0;
int modCnt = 0;
MyNode<E> first = null;
MyNode<E> last = null;
}
Java环境配备了一个垃圾收集器,这是一个销毁未使用对象并回收它们使用的内存的例程。因此,通常,在单链表中,当您取消first
指针时,第一个元素将不被引用,并且它可以与其next
引用一起回收。然后第二个元素变得不被引用。。。等等,所以整个列表都会被销毁。
但这取决于其他细节。例如,如果您的代码查找某个项目的列表,然后将引用存储在某个地方,它将阻止该引用对象(以及所有其他项目(回收
首先,我建议阅读有关列表的内容并理解它们的概念
LinkedList拥有一个头。头指向第一个节点
一个节点有一个对象和指向下一个节点的指针
您应该考虑删除节点时可能出现的问题
指针会发生什么情况?
这是数据结构的基础
正如我已经提到的,您必须了解List的概念才能实现LinkedList。
你的问题的答案是:
如果没有必要删除现有节点,你可以将头设置为null
如果有必要删除包含的所有现有节点,则u必须从后面运行ur列表。一个可能的解决方案是使用before指针扩展Node,这样可以更容易地从后面运行ur列表。
但这取决于你当前的impl,有些代码会很棒。
问候,1stNox
public class LinkedList<T> {
private Node<T> head;
public LinkedList(Node<T> head) {
this.head = head;
}
public Node<T> getHead() {
return this.head;
}
public void setHead(Node<T> head) {
this.head = head;
}
}
public class Node<T> {
private T value;
private Node next;
public Node(T value, Node next) {
this.value = value;
this.next = next;
}
// Getter & Setter
// ...
}