GIVEN______________ NumList.java public class NumList{ Node front; public NumLis
ID: 3903365 • Letter: G
Question
GIVEN______________
NumList.java
public class NumList{
Node front;
public NumList(){
front=null;
}
public void add(int val){
Node node=new Node(val);
if(front==null)
front=node;
else{
Node tmp=front;
while(tmp.getNext() !=null)
tmp=tmp.getNext();
tmp.setNext(node);
}
}
public int size(){
int count=0;
Node tmp=front;
while(tmp!=null){
tmp=tmp.getNext();
count++;
}
return count;
}
public boolean isEmpty(){
return front == null;
}
public void remove(int index){
if(index<size() && index>=0){
if(size()==1)
front=null;
else{
int loc=0;
Node tmp=front;
while(loc<index-1){
loc++;
tmp=tmp.getNext();
}
tmp.setNext(tmp.getNext().getNext());
}
}
}
public void remove(Node node){}
public boolean contains(int val){}
public Node elementAt(int index){}
public int indexOf(Node node){}
public int indexOf(int val){}
public String toString(){}
}
Node.java
public class Node{
private int val;
private Node next;
public Node(int val){
setVal(val);
next=null;
}
public int getVal(){
return val;
}
public Node getNext(){
return next;
}
public void setVal(int val){
this.val = val;
}
public void setNext(Node next){
this.next = next;
}
}
Explanation / Answer
Given below is the completed code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
public class NumList{
Node front;
public NumList(){
front=null;
}
public void add(int val){
Node node=new Node(val);
if(front==null)
front=node;
else{
Node tmp=front;
while(tmp.getNext() !=null)
tmp=tmp.getNext();
tmp.setNext(node);
}
}
public int size(){
int count=0;
Node tmp=front;
while(tmp!=null){
tmp=tmp.getNext();
count++;
}
return count;
}
public boolean isEmpty(){
return front == null;
}
public void remove(int index){
if(index<size() && index>=0){
if(size()==1)
front=null;
else{
int loc=0;
Node tmp=front;
while(loc<index-1){
loc++;
tmp=tmp.getNext();
}
tmp.setNext(tmp.getNext().getNext());
}
}
}
public void remove(Node node){
Node curr = front;
Node prev = null;
while(curr != null && curr != node)
{
prev = curr;
curr = curr.getNext();
}
if(curr == null)
return;
if(curr == front) //remove first node
front = curr.getNext();
else
prev.setNext(curr.getNext());
}
public boolean contains(int val){
Node tmp = front;
while(tmp != null)
{
if(tmp.getVal() == val)
return true;
tmp = tmp.getNext();
}
return false;
}
public Node elementAt(int index){
Node tmp = front;
int i = 0;
while(tmp != null && i < index)
{
tmp = tmp.getNext();
i++;
}
return tmp;
}
public int indexOf(Node node){
int index = 0;
Node tmp = front;
while(tmp != null)
{
if(tmp == node)
return index;
tmp = tmp.getNext();
index++;
}
return -1;
}
public int indexOf(int val){
int index = 0;
Node tmp = front;
while(tmp != null)
{
if(tmp.getVal() == val)
return index;
tmp = tmp.getNext();
index++;
}
return -1;
}
public String toString(){
String s = "[";
if(!isEmpty())
{
s += front.getVal();
Node tmp = front.getNext();
while(tmp != null)
{
s += ", " + tmp.getVal();
tmp = tmp.getNext();
}
}
s += "0";
return s;
}
}
class Node{
private int val;
private Node next;
public Node(int val){
setVal(val);
next=null;
}
public int getVal(){
return val;
}
public Node getNext(){
return next;
}
public void setVal(int val){
this.val = val;
}
public void setNext(Node next){
this.next = next;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.