LinkedList: implement clear [closed]
I'm doing a LinkedList class of my own, and I'm trying to implement clear, and I'm not sure what to do. what I was thinking about doing is traversing the list and setting every node to null, but I have a reference to the first node in the list, and I've heard that I can simply do
first = null;
and that will be enough. Is that right, or do I need to traverse the list EDIT: I'd been advised to show the rest of my code. Not everything is implemented yet, but I hope it helps.
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;
}
The Java environment is equipped with a garbage collector, that is a routine which destroys unused objects and recycles memory used by them. So, generally, in a singly linked list, when you nullify the first
pointer, the first element becomes unreferenced and it can be recycled, together with its next
reference. Then the second element becomes unreferenced ...and so on, so the whole list will get destroyed.
But that depends on additional details. For example, if your code seeks a list for some item and then stores the reference somewhere, it will prevent that referenced object (and all further items, too) from recycling