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

How to add a node to the end of the linked list? import java.util.Scanner; class

ID: 3568631 • Letter: H

Question

How to add a node to the end of the linked list?

import java.util.Scanner;

class LinkedList {

Node head;
Node tail;

/*
* Adds Node to end of list with given `num` as data
* Make sure it correctly sets the head (and tail)
* when adding the first item to the list
*/
public void add(int num){
//add code here
}

/*
* Returns value of node at given index
*/
public int get(int index){
//add code here
}
}

class Node{
int data;
Node next;
public Node(int d){
data = d;
}

public Node(int d, Node n){
data = d;
next = n;
}
}

public class Main {

public static void main(String[] args) {
LinkedList list = new LinkedList();
Scanner in = new Scanner(System.in);

int n = in.nextInt();
for(int i=0; i<n; i++){
int a = in.nextInt();
int b= in.nextInt();
if(a == -6){
System.out.println(list.get(b));
}
else if(a == -9){
list.add(b);
}
}
}
}

Explanation / Answer

void insert_at_end() { struct node *new_node,*current; new_node=(struct node *)malloc(sizeof(struct node)); if(new_node == NULL) printf("nFailed to Allocate Memory"); printf("nEnter the data : "); scanf("%d",&new_node->data); new_node->next=NULL; if(start==NULL) { start=new_node; current=new_node; } else { temp = start; while(temp->next!=NULL) { temp = temp->next; } temp->next = new_node; } }

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