Question

In: Computer Science

In java please create a method that will insert a value after a specific node for...

In java please create a method that will insert a value after a specific node for a linked list.

public void insertAfter(Node a, int newData){

}

Solutions

Expert Solution

public class LinkedList {

   Node head;       //head of the list
  
   class Node       //Linked List Node
   {
       int data;
       Node next;
       Node(int d) {
           data=d;       //store value
           next=null;   //stores address of the next value
       }
   }
   // Appends a new node in the linked list similar to incremental fashion.
   public void append(int new_data)
   {
       //Allocate the node and set and data and next as null
       Node new_node = new Node(new_data);
      
       //If the Linked List is empty, then make the new node as head
       if(head == null) {
           head = new Node(new_data);
           return;
       }
      
       // make the next of new node as null since its going to be the last element in the list
       new_node.next=null;
      
       //Else traverse till the last node, and find the last element.
       Node last= head;
       while(last.next != null)
           last=last.next;
      
       // point the last node to new node by changing the next of last node.
       last.next=new_node;
       return;
   }
  
   // Inserts a new node after the given previous node.
   public void insertAfter(Node a, int newData)
   {
       //First check whether given node is null or not
       if(a == null) {
           System.out.println("The given Previous node cannot be null so enter value in the LinkedList");
           return;
       }
      
       //Else we gonna Allocate the Node and insert the data into the linkedlist
       Node new_node= new Node(newData);
      
       // make next of new Node as next of previous node
       new_node.next= a.next;
      
       // make next of previous node as new node
       a.next=new_node;
   }
  
   //This function prints the contents of linked list starting from head
   public void printList()
   {
       Node node= head;
       while(node!=null)   // while linked list has node
       {
           System.out.print(node.data+" ");   //print the data
           node=node.next;                           //increment the next
       }
   }
  
   public static void main(String[] args) {

       // Start with the empty list
LinkedList llist = new LinkedList();
// Insert node 5,3,8,9 into the linkedlist
llist.append(5);
llist.append(3);
llist.append(8);
llist.append(9);
  
System.out.print("Linked List is : ");
llist.printList();
System.out.println();
// Insert 4, after 8. So linked list becomes
// 5->3>8->4->9->Nulllist
llist.insertAfter(llist.head.next.next, 4); //prev node is head->next-next= 8
  
System.out.print("After Inserting New Node After "+llist.head.next.next.data+" : ");
llist.printList();  
   }
}

Output: // Please appreciate my answer if it was helpful. If you have any doubts feel free to add comments. Thanks in advance.


Related Solutions

Insert: this method takes a value as a parameter and adds a node which contains the...
Insert: this method takes a value as a parameter and adds a node which contains the value to the end of the linked list Delete: This method deletes a node from the linked list. If an index is passed as a parameter, then the method should delete the node at this index. If no index is passed, then delete the first item in the list Find: this method takes a value as a parameter, and returns the index of the...
In java please create a class with a method to be able to add ints into...
In java please create a class with a method to be able to add ints into a linked List and to be able to print out all the values at the end. public class Node { int data; Node next;
Write a non recursive method to insert into an AVL tree in Java
Write a non recursive method to insert into an AVL tree in Java
Java programming Create a application with a method void (after main() ) that creates an array...
Java programming Create a application with a method void (after main() ) that creates an array and asks for the user fill it with float numbers. This array have infinite elements until the user decide that it is enough and input a command to stop. Display this array's elements data in another method void. Thanks for your help!
Create a Java method that takes a String as input value and returns the number of...
Create a Java method that takes a String as input value and returns the number of vowels contained in that string.
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node is the node with the next highest value in the tree. The successor of the node with the largest value in a tree, is null. The algorithm to find the successor of a node is straight forward: if the node has a right subtree, the successor is the smallest node in that subtree (for that we use method minNode). Otherwise, we traverse the tree...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with the higher value data. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head; while (tempPtr != null) { tempPtr = tempPtr.nextNode; result =...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node is the node with the next highest value in the tree. The successor of the node with the largest value in a tree, is null. The algorithm to find the successor of a node is straight forward: if the node has a right subtree, the successor is the smallest node in that subtree (for that we use method minNode). Otherwise, we traverse the tree...
write a method in java for a binary search tree that receives a node as input...
write a method in java for a binary search tree that receives a node as input and returns the successor node.
c++ Binary Search Tree: find parent of a node please insert comment to explain what the...
c++ Binary Search Tree: find parent of a node please insert comment to explain what the code do. Use struct Node{ int data; Node* left; Node* right};
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT