我正在做一个我自己的LinkedList类,我试图实现清晰,我不知道该怎么办。我想做的是遍历列表并将每个节点设置为空,但是我引用了名单,我听说我可以简单地做
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;
}
首先,我建议阅读列表并理解它们的概念<链接列表有一个标题。头部指向第一个节点
节点有一个对象和指向下一个节点的指针
您应该考虑删除节点时可能出现的问题<指针会发生什么变化?
这是数据结构的基础
正如我已经提到的,您必须理解列表概念才能实现LinkedList。
回答你的问题:
如果没有必要删除现有的节点,你可以将头部设置为空。
如果有必要删除所有包含的现有节点,你将不得不从后面运行ur列表。一个可能的解决方案,使它更容易运行从后面的ur列表是扩展的节点之前的指针。
但这取决于您当前的impl,有些代码可能会很棒。
你好,1stNox
java prettyprint-override">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
// ...
}
Java环境配备了垃圾收集器,垃圾收集器是一个例程,它销毁未使用的对象并回收它们使用的内存。因此,通常情况下,在单链表中,当您将first
指针置空时,第一个元素将不被引用,并且可以与itpnext
reference一起循环使用。。然后第二个元素变得不被引用。。。以此类推,整个列表都会被销毁。
但这取决于其他细节。例如,如果您的代码查找某个项的列表,然后将引用存储在某个位置,那么它将阻止引用的对象(以及所有其他项)循环使用
本文向大家介绍ThinkPHP实现一键清除缓存方法,包括了ThinkPHP实现一键清除缓存方法的使用技巧和注意事项,需要的朋友参考一下 很多的开源cms系统都有一键清除缓存的功能,缓存是为了减轻服务器的压力而产生的,但是同时有缓存的存在也可能使一些数据不能实时更新,对此,我们就来实现一个ThinkPHP的清理缓存的功能。代码如下: ThinkPHP后台执行的代码: 前台页面部分代码如下:
我一直在研究一种使用LinkedList实现队列的方法。我已经找到了很多例子,它们向我展示了如何通过在类中使用“implements”来做到这一点。但是,我想做的是扩展LinkedList类。例如,我写过这样的东西: 这真的是使用链表类型队列所要做的一切吗?那么,我要如何设置一个头(前面)和一个尾(后面)来像队列一样使用链表呢? 提前谢谢。
问题内容: 这是使用for每个循环从Java中的LinkedList中查找和删除项目的有效方法,是否可能会导致不一致: 问题答案: 其他人提到有效点,通常这不是您如何从集合中获取对象。但是,在这种情况下,因为您一旦退出循环就可以了。 但是,如果要在之后继续迭代,则需要使用迭代器。否则,您将获得,或更普遍的情况是未定义的行为。 所以,是的, 如果您不在别人之后,您会没事的。 对于那些说这将失败的人来
本文向大家介绍IOS 缓存文件的清除实现代码,包括了IOS 缓存文件的清除实现代码的使用技巧和注意事项,需要的朋友参考一下 移动互联网 APP 的应用开发,必须要时刻注意用户体验,以免造成APP 或者手机及其他移动设备的卡死情况,以下是对缓存文件的处理。 移动应用在处理网络资源时,一般都会做离线缓存处理,其中以图片缓存最为典型,其中很流行的离线缓存框架为SDWebImage。 但是,离线缓存会占用
每次我尝试通过清除控制台按钮清除eclipse中的控制台时,都会导致eclipse意外死亡。
本来还对干测开有点犹豫,但是今天面了一个大厂开发被狠狠拷打了(ps:问到很多都不会,感觉面试官都绷不住了),我不配干开发,转测开去