Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

--------------------------------------------------- IntLinkList : /** * A class

ID: 3918132 • Letter: #

Question

---------------------------------------------------

IntLinkList :

/**
* A class to hold a list of int data, by
* storing them as a linked list.
*/
public class IntLinkList {
  
private IntNode top; //The reference to the first Node
  
//=========== Solution code =============================
  
//Your code here
  
private static boolean equals(IntNode top1, IntNode top2){
//Your code here
return false; //Dummy statement for testing - remove it.
}

//=========== Supplied code =============================
  
public IntLinkList() {
//A constructor that creates an empty list.
top = null;
}
  
public void add(int newItem) {
//Add the newItem at the FRONT of the list.
top = new IntNode(newItem,top);
}//add
  
public String toString() {
String answer = "<<";
IntNode next = top;
while(next!=null){
answer += next.getData()+" ";
next = next.getLink();
}
return answer+">>";
}
  
public void ordInsert(int newItem) {
//Add the newItem so that the list remains sorted into
//ascending order. This will not work unless the list
//is currently in ascending order.
IntNode prev = null;
IntNode next = top;
while(next!=null && next.getData() prev = next;
next = next.getLink();
}
//This item belongs between prev and next
IntNode newNode = new IntNode(newItem,next);
if(prev==null)
top = newNode;
else
prev.setLink(newNode);
}
  
public boolean equals(IntLinkList other){
return equals(top,other.top);
}
  
}

---------------------------------

IntNode:

/**
* One particular node in a linked list of nodes containing int data.
*/
public class IntNode {
  
private int data; //The data in this Node
private IntNode link; //The link to the next Node
  
public IntNode(int initData, IntNode initLink){
data = initData;
link = initLink;
}
  
public int getData() {return data;}
public IntNode getLink() {return link;}
public void setData(int o) {data = o;}
public void setLink(IntNode n) {link = n;}
  
  
}

QUESTION 1: SIMPLE LINKED LIST OPERATIONS. Start with the supplied files IntNode and IntLinkList which are almost the same as the Node and LinkedList classes discussed in class, but they implement a linked list of int data instead of Object data. Add the following methods to the IntLinkList class. public IntLinkList (int] data) - this constructor should create a linked list containing the same data as the supplied int[] array, and in the same order. Example: > new IntLinkList (new int [111,4,8,2})- ??1 4 8 2 >> public boolean empty)-this method should return true if the linked list is empty. new IntlinkList).empty) truev public int first) -this method should return the first int in the linked list. It may assume that the linked list is not empty. new IntLinkList (new int[11,4,8,2)).first) 1

Explanation / Answer

public class IntLinkList {

private IntNode top;

public IntLinkList(int[] data){

   for (int i = 0; i<data.length; i++){
       add(data[i]);
   }
}

public boolean empty(){

   if (top == null)
      return true;
   return false;
}

public int first(){

       return top.getData();

}

public void removeFirst(){
   if (top != null)
       top = top.getLink();
}

public IntLinkList clone(){
    IntLinkList a = new IntLinkList();
    if (top != null){
        IntNode p = top;
        while (p != null){
            a.add(p.getData());
            p = p.getLink();
        }
    }
    return a;
}
private static boolean(IntNode top1, IntNode top2){
     if (top1.getData() == top2.getData())
        return true;
     else
        return false;
}

//=========== Supplied code =============================

public IntLinkList() {
//A constructor that creates an empty list.
top = null;
}

public void add(int newItem) {
//Add the newItem at the FRONT of the list.
top = new IntNode(newItem,top);
}//add

public String toString() {
String answer = "<<";
IntNode next = top;
while(next!=null){
answer += next.getData()+" ";
next = next.getLink();
}
return answer+">>";
}

public void ordInsert(int newItem) {
//Add the newItem so that the list remains sorted into
//ascending order. This will not work unless the list
//is currently in ascending order.
IntNode prev = null;
IntNode next = top;
while(next!=null && next.getData() prev = next;
next = next.getLink();
}
//This item belongs between prev and next
IntNode newNode = new IntNode(newItem,next);
if(prev==null)
top = newNode;
else
prev.setLink(newNode);
}

public boolean equals(IntLinkList other){
return equals(top,other.top);
}

}