Question

In: Computer Science

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 ordered.

Here is the procedure for testing. Please include this.

  • Add the numbers 2, 3, and 6 to the list
  • Display the list contents
  • Add 5 to the list
  • Display the list contents
  • Delete 3 from the list
  • Display the list contents
  • Delete 2 from the list
  • Display the list contents

THANK YOU!!!

Solutions

Expert Solution

public class LinkedList {
  
IntegerNode head; // head of list
  
// Linked list Node.
// This inner class is made static
// so that main() can access it
public static 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
  
// Method to insert a new node
public static LinkedList insert(LinkedList list, int data)
{
// Create a new node with given data
IntegerNode new_node = new IntegerNode(data);
new_node.next = null;
  
// If the Linked List is empty,
// then make the new node as head
if (list.head == null || list.head.item>=new_node.item) {
list.head = new_node;
}
else {
// Else traverse till the last node
// and insert the new_node there
IntegerNode last = list.head;
  
while (last.next != null && last.next.item<new_node.item) {
last = last.next;
}
  
// Insert the new_node
new_node.next=last.next;
last.next = new_node;
}
  
// Return the list by head
return list;
}
  
void deleteNode(int key)
{
// Store head node
IntegerNode temp = head, prev = null;

// If head node itself holds the key to be deleted
if (temp != null && temp.item == key)
{
head = temp.next; // Changed head
return;
}

// Search for the key to be deleted, keep track of the
// previous node as we need to change temp.next
while (temp != null && temp.item != key)
{
prev = temp;
temp = temp.next;
}

// If key was not present in linked list
if (temp == null) return;

// Unlink the node from linked list
prev.next = temp.next;
}

// Method to print the LinkedList.
public static void printList(LinkedList list)
{
IntegerNode currNode = list.head;

System.out.print("LinkedList: ");

// Traverse through the LinkedList
while (currNode != null) {
// Print the data at current node
System.out.print(currNode.item + " ");

// Go to next node
currNode = currNode.next;
}
}

// Driver code
public static void main(String[] args)
{
/* Start with the empty list. */
LinkedList list = new LinkedList();
  
//
// ******INSERTION******
//
  
// Insert the values
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 6);
  
// Print the LinkedList
printList(list);
  
list = insert(list, 5);
System.out.println("\n");
printList(list);
  
list.deleteNode(3);
System.out.println("\n");
printList(list);
  
list.deleteNode(2);
System.out.println("\n");
printList(list);
  
}
}

Output Window:

This program has been compiled in online java compiler. The input is taken according to the specification in the question.


Related Solutions

Use this implementation of Integer node, public class IntegerNode { public int item; public IntegerNode next;...
Use this implementation of Integer node, 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 ordered. Your code...
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...
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...
Remove the Head element from the code below: public class LinkedList {    class Node{ int...
Remove the Head element from the code below: public class LinkedList {    class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element);    if(first == null) { first = newValueNode; } else { last.nextElement = newValueNode; } last = newValueNode; } public void displayValues() { Node recent = first; if(first ==...
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...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function void list_head_insert(NodePtr& head, int entry); The function should insert a new Node, in which entry is the value of attribute item, in front of the linked list that is pointed by head. 2. Write function void list_head_remove(NodePtr& head); The function will remove the first node from the linked list that is pointed by head. 3. Write function NodePtr list_search(NodePtr head, int target); The function...
Please show it with python class Node {     int data;     Node left, right;    ...
Please show it with python class Node {     int data;     Node left, right;     public Node(int item)     {         data = item;         left = right = null;     } } public class BinaryTree { // Root of the tree implemented in Node class Node root; Node findLowestCommonAncestor(int node1, int node2) {     return findLowestCommonAncestor(root, node1, node2); } // This function returns pointer to LCA of two given // values node1 and node2. This function assumes that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT