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

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...
Please implement the java method addInOrder() that allows you to create and maintain the lists in...
Please implement the java method addInOrder() that allows you to create and maintain the lists in the order required. The addInOrder method must work with different external Comparator objects. (Hint: Consider creating and using a private compare method and a private Comparator reference as members of your SLL class. If your SLL is constructed without any parameter, then you should initialize the internal Comparator object reference to null. Otherwise, you should initialize it to the external Comparator object passed as...
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};
JAVA PLEASE!! Write a value-returning method, isVowel, that returns the value true if a given character...
JAVA PLEASE!! Write a value-returning method, isVowel, that returns the value true if a given character is a vowel, and otherwise returns false. Also write a program to test your method. 2) Write a program that prompts the user to input a sequence of characters and outputs the number of vowels. (Use the method isVowel written in Programming Exercise 1.)
Write a JAVA program which prints the root node after the "remove" procedure the the splay...
Write a JAVA program which prints the root node after the "remove" procedure the the splay tree.
Python linked lists ● Insert: this method takes a value as a parameter, and adds a...
Python linked lists ● 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,...
a. You have to write the steps that we need to insert a new node as...
a. You have to write the steps that we need to insert a new node as the head of an existing linked list. b.You have to write the code in c++ programming language of the function that we need to insert a new node in the end of an existing node. c.Suppose that the below main function is executed correctly and all the functions that are invoked are imported from functions.h file. Explain. int main(){ Node *head=NULL; insertEnd(&head,"John");//inserts a new...
Question 3: Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...
Question 3: Create a method for the Binary Search Tree (deleteNode) that deletes a specified node identified by its value, and rearranges the descendants of the deleted node to ensure the resulting Tree meets the requirements of a Binary Search Tree. a) Discuss and justify your approach to address each possible case. b) Is the new tree (with the deleted node removed) unique? Discuss your answer. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node identified by its value, and rearranges the descendants of the deleted node to ensure the resulting Tree meets the requirements of a Binary Search Tree. a) Discuss and justify your approach to address each possible case. b) Is the new tree (with the deleted node removed) unique? Discuss your answer. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node identified by its value, and rearranges the descendants of the deleted node to ensure the resulting Tree meets the requirements of a Binary Search Tree. a) Discuss and justify your approach to address each possible case. b) Is the new tree (with the deleted node removed) unique? Discuss your answer. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT