Question

In: Computer Science

please answer as text You are given a Node class and a List class: public class...

please answer as text

You are given a Node class and a List class:

public class Node {

       int           data;

       Node       next;

       Node(int d, Node n){

            data = d;

            next = n;

      }

}

public class List{

       Node header;

}

Write a java method insertNodeBefore( ) which takes two integers: int ref which is the data of the node that we need to insert a new node before, and int d which is the data of the new node we want to insert, for the Linked list mylist.

Solutions

Expert Solution

This is the mthod

void insertNodeBefore(int ref,int d)
   {
       if(header==null)
       {
           header=new Node(d,null);
           return;
       }
       if(header.next==null && header.data==ref)
       {
           Node newNode=new Node(d, header);
           header=newNode;
           return;
       }
       if(header.data==ref)
       {
           Node newNode=new Node(d, header);
           header=newNode;
           return;
       }
       Node curr=header;
       Node pre=curr;
       Node newNode=new Node(d, null);
       while(curr!=null && curr.data!=ref)
       {
           pre=curr;
           curr=curr.next;
       }
       if(curr!=null)
       {
           pre.next=newNode;
           newNode.next=curr;
       }
   }

For testing purpose i have created the full prodgrem

Node class

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

List.class

public class List {
   Node header;
  
   public List()
   {
       header=null;
   }
  
   void insertNodeBefore(int ref,int d)
   {
       if(header==null)
       {
           header=new Node(d,null);
           return;
       }
       if(header.next==null && header.data==ref)
       {
           Node newNode=new Node(d, header);
           header=newNode;
           return;
       }
       if(header.data==ref)
       {
           Node newNode=new Node(d, header);
           header=newNode;
           return;
       }
       Node curr=header;
       Node pre=curr;
       Node newNode=new Node(d, null);
       while(curr!=null && curr.data!=ref)
       {
           pre=curr;
           curr=curr.next;
       }
       if(curr!=null)
       {
           pre.next=newNode;
           newNode.next=curr;
       }
   }
  
   void printList()
   {
       Node temp=header;
       while(temp!=null)
       {
           System.out.print(temp.data+" ");
           temp=temp.next;
       }
   }
}

ListTest class with main method


public class ListTest {

   public static void main(String[] args) {
       List lst=new List();
      
       lst.insertNodeBefore(0, 30);
       lst.insertNodeBefore(30, 20);
       lst.insertNodeBefore(20, 10);
       lst.printList();
   }

}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

public class MyLinked {    static class Node {        public Node (double item, Node...
public class MyLinked {    static class Node {        public Node (double item, Node next) { this.item = item; this.next = next; }        public double item;        public Node next;    }    int N;    Node first;     // remove all occurrences of item from the list    public void remove (double item) {        // TODO    } Write the remove function. Do NOT add any fields to the node/list classes, do...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
Using the textbook implementation of integer node given below; public class IntegerNode { public int item;...
Using the textbook implementation of integer node given below; public class IntegerNode { public int item; public IntegerNode next; public IntegerNode(int newItem) { item = newItem; next = null; } // end constructor public IntegerNode(int newItem, IntegerNode nextNode) { item = newItem; next = nextNode; } // end constructor } // end class IntegerNode You need to implement add( ), delete( ), traverse( ) methods for an ordered linked list. And after insertion and deletion, your linked list will remain...
Can you please answer this in its entirety? The text we are using for the class...
Can you please answer this in its entirety? The text we are using for the class is Framework: Marketing Management, Kotler/Keller (14th edition, Prentice Hall). This is not a question from the book but one from the professor. The company I am referring to is Wendy's.Thank you in advance for your response. Each student will select a company to do the following with: Utilize course concepts and materials discussed in the textbook to analyze the current marketing situation of the...
class nodeType                    // class used to implement a node { public:         int data;   &n
class nodeType                    // class used to implement a node { public:         int data;                        // data member of node         nodeType * next;        // pointer member of node }; int main() {         int x;         nodeType * head =________ ;                     // initialize head pointer         nodeType * tail = _______ ;                        // initialize tail pointer _______ * p;                                                 // create an auxiliary pointer to a node         for (x = 0; x < 10; ++x)         {                 p =   _________ nodeType; // create a node ___________ = x + 10;                                // store...
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT