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

Question 1: Linked Lists Create an IntLinkedList class, much like the one presen

ID: 3700728 • Letter: Q

Question

Question 1: Linked Lists Create an IntLinkedList class, much like the one presented in class. It should implement a linked list that will hold values of type int. It should use an IntNode class to implement the nodes in the linked list. The linked list must have the following methods: A constructor with no parameters which creates an empty list void add (int data) adds data to the front of the list. A constructor IntLinkedList(intl ]) which will create a linked list containing the same values as the int ] array, and in the same order. void remove(int index)-remove one node from the list, at the specified index, starting at 0. If the index is invalid, remove nothing. .String toString-ren a String containing the integer values, in the order they appear in the list, with surrounding them, and commas separating them. There should not be a comma after the last integer. For example, not

Explanation / Answer

class IntLinkedList

{

IntNode head; // head of list

/* Linked list Node*/

class IntNode

{

int data;

IntNode next;

IntNode(int d) {data = d; next = null; }

}

//constructor to create empty linked list

IntLinkedList(){

IntNode new_node = new IntNode(0);

}

//constructor to initialize list with elements of array in same order

IntLinkedList(int arr[]){

int length = arr.length();

IntLinkedList llist = new IntLinkedList();

for (int i=0; i< length; i++){

llist.append(arr[i]);

}

}

// Inserts a new Node at front of the list.

public void add(int new_data)

{

IntNode new_node = new IntNode(new_data);

new_node.next = head;

head = new_node;

}

//append the node in the end

public void append(int new_data)

{

IntNode new_node = new IntNode(new_data);

if (head == null)

{

head = new IntNode(new_data);

return;

}

new_node.next = null;

// Else traverse till the last node

IntNode last = head;

while (last.next != null)

last = last.next;

// Change the next of last node

last.next = new_node;

return;

}

void remove(int position)

{

// If linked list is empty

if (empty())

return;

// Store head node

IntNode temp = head;

// If head needs to be removed

if (position == 0)

{

head = temp.next; // Change head

return;

}

// Find previous node of the node to be deleted

for (int i=0; temp!=null && i<position-1; i++)

temp = temp.next;

// If position is more than number of ndoes

if (temp == null || temp.next == null)

return;

IntNode next = temp.next.next;

temp.next = next; // Unlink the deleted node from list

}

public String toString()

{

String list;

IntNode tnode = head;

list.append("<<");

while (tnode != null)

{

String s = Integer.toString(tnode.data);

list.append(s);

tnode = tnode.next;

if(tnode != null) list.append(",");

}

list.append(">>");

return list;

}

public IntLinkedList clone()

{

IntLinkedList newlist = new IntLinkedList();

IntNode tnode = head;

while (tnode != null)

{

newlist.append(tnode.data);

tnode = tnode.next;

}

return newlist;

}

public bool empty(){

if(head == null)

return true;

else return false;

}

public static void main(String[] args)

{

/* Start with the empty list */

IntLinkedList llist = new IntLinkedList();

int[] array = {1,2,3,4};

IntLinkedList llist1 = new IntLinkedList(array);

llist.add(3);

llist.empty()

llist.remove(1);

  

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote