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...
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 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...
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...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;   ...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;    Node(int v, Node n){        value = v;        nextNode = n;    }    Node (int v){        this(v,null);    } } public class Stack {    protected Node top;    Stack(){        top = null;    }    boolean isEmpty(){        return( top == null);    }    void push(int v){        Node tempPointer;       ...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT