Purpose of this lab is to understanding the fundamentals of the Linked Lists. Pl
ID: 3675649 • Letter: P
Question
Purpose of this lab is to understanding the fundamentals of the Linked Lists. Please check the attached code and implement the ’iterate’ method that will traverse the linked list and print out the list elements. It’s a simple method but importance of this lab lies in understanding how linked list structure works. Change the elements added or removed, think about how the code works.
package com.list;
public class Main {
public static void main(String[] args) {
NodeList list = new NodeList();
list.add("Book");
list.add("Lappy");
System.out.println("Length : "+list.size());
list.remove("Book");
System.out.println("Length : "+list.size());
list.remove("Lappy");
System.out.println("Length : "+list.size());
list.add("Book");
list.add("Lappy");
list.add("Glass");
System.out.println("Length : "+list.size());
list.iterate();
}
}
package com.list;
public class Node {
private Object data;
private Node next;
public Node(Object data) {
this.data = data;
this.next = null;
}
public Node(Object data, Node next){
this.data = data;
this.next = next;
}
/**
* @return data
*/
public Object getData() {
return data;
}
/**
* @param data :the data to set
*/
public void setData(Object data) {
this.data = data;
}
/**
* @return String of the data
*/
public String toString() {
return "Data: "+ data.toString();
}
/**
* @return the next
*/
public Node getNext() {
return next;
}
/**
* @param next :the next to set
*/
public void setNext(Node next) {
this.next = next;
}
}
package com.list;
public class NodeList {
private int size = 0;
private Node root = null;
/*
* It has to take a new Node and add that to the next address of previous Node.
*
* @Param - Node
*/
public void add(Object data) {
if (data==null) return;
if (root == null) {
Node newNode = new Node(data, null);
root = newNode;
}
else {
Node currentNode = root;
while(currentNode.getNext()!=null){
currentNode = currentNode.getNext();
}
Node newNode = new Node(data, null);
currentNode.setNext(newNode);
}
size++;
}
/*
* It has to return the size of the NodeList
*
* @return size
*/
public int size() {
return size;
}
/*
* It has to take a Node and remove that node if you find it in the list
* from the existing nodes otherwise dont do anything.
*
* @Param - Node
*/
public void remove(Object data) {
if(data==null) return;
if(root != null) {
if(root.getData() == data) {
if(root.getNext()==null)
root = null;
else
{
root = root.getNext();
}
} else {
Node currentNode = root;
while(currentNode.getNext()!= null){
Node temp = currentNode.getNext();
if(temp.getData() == data)
{
Node tempsNext = temp.getNext();
currentNode.setNext(tempsNext);
temp = null;
}
}
}
size--;
}
}
/**
* Start with the head and traverse till you reach null.
*/
public void iterate(){
}
}
Explanation / Answer
package com.list;
public class Main {
public static void main(String[] args) {
NodeList list = new NodeList();
list.add("Book");
list.add("Lappy");
System.out.println("Length : " + list.size());
list.remove("Book");
System.out.println("Length : " + list.size());
list.remove("Lappy");
System.out.println("Length : " + list.size());
list.iterate();
list.add("Book");
list.add("Lappy");
list.add("Glass");
System.out.println("Length : " + list.size());
list.iterate();
}
}
package com.list;
public class Main {
public static void main(String[] args) {
NodeList list = new NodeList();
list.add("Book");
list.add("Lappy");
System.out.println("Length : " + list.size());
list.remove("Book");
System.out.println("Length : " + list.size());
list.remove("Lappy");
System.out.println("Length : " + list.size());
list.iterate();
list.add("Book");
list.add("Lappy");
list.add("Glass");
System.out.println("Length : " + list.size());
list.iterate();
}
}
package com.list;
public class NodeList {
private int size = 0;
private Node root = null;
/*
* It has to take a new Node and add that to the next address of previous
* Node.
*
* @Param - Node
*/
public void add(Object data) {
if (data == null)
return;
if (root == null) {
Node newNode = new Node(data, null);
root = newNode;
} else {
Node currentNode = root;
while (currentNode.getNext() != null) {
currentNode = currentNode.getNext();
}
Node newNode = new Node(data, null);
currentNode.setNext(newNode);
}
size++;
}
/*
* It has to return the size of the NodeList
*
* @return size
*/
public int size() {
return size;
}
/*
* It has to take a Node and remove that node if you find it in the list
* from the existing nodes otherwise dont do anything.
*
* @Param - Node
*/
public void remove(Object data) {
if (data == null)
return;
if (root != null) {
if (root.getData() == data) {
if (root.getNext() == null)
root = null;
else {
root = root.getNext();
}
} else {
Node currentNode = root;
while (currentNode.getNext() != null) {
Node temp = currentNode.getNext();
if (temp.getData() == data) {
Node tempsNext = temp.getNext();
currentNode.setNext(tempsNext);
temp = null;
}
}
}
size--;
}
}
/**
* Start with the head and traverse till you reach null.
*/
public void iterate() {
if (root == null) {
System.out.println("No data Found");
return;
} else {
Node currentNode = root;
while (currentNode != null) {
System.out.print(currentNode.getData() + "->");
currentNode = currentNode.getNext();
}
}
}
}
OUTPUT:
Length : 2
Length : 1
Length : 0
No data Found
Length : 3
Book->Lappy->Glass->
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.