Question

Write a method that inserts a node in a sorted linked list. HINTS: Traverse the list...

Write a method that inserts a node in a sorted linked list. HINTS: Traverse the list and find a position for the element and insert it.

Homework Answers

Answer #1

Node.java

public class Node {
   int data;
Node next;
Node(int data) {
   this.data = data;
   this.next = null;
}
}

SortedLinkedList.java

public class SortedLinkedList {
  
   Node head;

   //method to insert a value in the sorted linked list, such list remains sorted after the insertion.
   public void insert(int data) {
       //creating a new Node with passed argument
       Node newNode = new Node(data);
       System.out.println("Inserting: " + data);
       //checking for head
       //if the list is empty or
       //we need to change the head that is new Node is smaller than the head node.
       if(head == null || head.data >= newNode.data) {
           newNode.next = head;
           head = newNode;
       }
       else {
           //else
           Node currNode = head;
          
           //locate the appropriate position in the list, where we need to insert the
           //new node before actual insertion.
           while(currNode.next != null && currNode.next.data < newNode.data) {
               currNode = currNode.next;
           }
          
           //when position found, insert the node.
           newNode.next = currNode.next;
           currNode.next = newNode;
       }
   }
  
   //a helper method to print the list
   public void printList() {
       Node ptr = this.head;
      
       System.out.print("List: ");
       while(ptr != null) {
           System.out.print(ptr.data + " ");
           ptr = ptr.next;
       }
       System.out.println();
   }
  
   public static void main(String[] args) {
      
       SortedLinkedList list = new SortedLinkedList();
       list.insert(10);
       list.printList();
       list.insert(20);
       list.printList();
       list.insert(30);
       list.printList();
       list.insert(5);
       list.printList();
       list.insert(14);
       list.printList();
       list.insert(22);
       list.printList();
       list.insert(11);
       list.printList();
   }

}

IF THERE IS ANYTHING THAT YOU DO NOT UNDERSTAND, OR NEED MORE HELP THEN PLEASE MENTION IT IN THE COMMENTS SECTION.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In C++ please Implement a class for doubly linked list. Your doubly linked list should have...
In C++ please Implement a class for doubly linked list. Your doubly linked list should have following members: - A head pointer - A tail pointer - Function: insert at head -Function: insert at tail - Function: delete a node - Function: search a node - Function: traverse - Function: reverse traverse Test your implementation by calling member functions inside main.
write a function to delete the largest item from a sorted (asc) Circular Linked List (a...
write a function to delete the largest item from a sorted (asc) Circular Linked List (a CLLNode is list where the last element is pointing to head).
public class LinkedList {       private Node list;       public LinkedList()       {           list =...
public class LinkedList {       private Node list;       public LinkedList()       {           list = null;       }       public Node getList()       {           return list;       }       . . .//other methods       // insert method printHighEarners here       // insert method lowestPaidEmployeeRec       private class Node       {           public Employee data;           public Node next;           public Node(Employee emp)           {               data = emp;               next = null;           }           public String toString()...
1. Build a double linked list with 5 in the first node following the instructions: Insert...
1. Build a double linked list with 5 in the first node following the instructions: Insert a node with 3 at the top of the list Insert a node with 10 at the bottom of the list Insert a node with 7 between nodes with 5 and 10 2. Start deleting nodes from the list in the following order; Delete the node with 7 Delete the node with 3 Delete the node with 10 3. Print the resulting list forward...
Write Pseudocode to remove the node at position 1 in a doubly-linked list. Assume position follows...
Write Pseudocode to remove the node at position 1 in a doubly-linked list. Assume position follows classic indexing from 0 to item_count - 1, and there is a node at position 2.
C++ When the sorted list inherits the linked list it must: a ) More than one...
C++ When the sorted list inherits the linked list it must: a ) More than one answer is correct b ) Put in insertSorted and removeSorted methods and remove insert method c ) Change getEntry d) Put in removeSorted and remove the remove method
C++ pls finish code! Lab: Singly-Linked List (Student class) Review and finish the following files (read...
C++ pls finish code! Lab: Singly-Linked List (Student class) Review and finish the following files (read code and all comments carefully): Student.h StudentList.h StudentList.cpp main.cpp This program: Creates a sorted linked list (student name and gpa) . The list is sorted in ascending order by name. Displays the list Read and understand this program, then do the following: finish writing Student.h and other files fix errors in StudentList.cpp Student.h: #ifndef STUDENT_H #define STUDENT_H //using namespace std; //<==== This statement //...
Write a member method remove( ) which randomly generate a position and deletes the node at...
Write a member method remove( ) which randomly generate a position and deletes the node at that position from the linked list. Node: Please use C++ language, only need function.
) IN JAVA: Write a recursive method lowestPaidEmployeeRec that is placed in the linked list after...
) IN JAVA: Write a recursive method lowestPaidEmployeeRec that is placed in the linked list after the method countHighEarners. Method returns employee with lowest salary in entire linked lists of employees. Assume the same LinkedList class as is given as in question 10 above. // PRECONDITION: Linked list is not empty. public Employee lowestPaidEmployeeRec(Node first) // first is reference to the beginning of linked list. { }
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...