Question

In: Computer Science

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 should include comments and documentation.

Testing

Here is the procedure for testing, which must be documented (a Word document is preferred, as is the use of screenshots).

  • 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

Solutions

Expert Solution

I believe ordered linked list is sorted one.

So i will help you out with all three methods, on behalf of sorted linked list.

Lets take a look at these methods,

class IntegerNode {

        int data; // or you can use item as written in your question, its just a variable name

IntegerNode next;

IntegerNode(int d)

        {

            data = d;

            next = null;

        }

    }

    /* function to insert a

new_node in a list. */

    void sortedInsert(IntegerNode new_node)

    {

IntegerNode current;

        /* Special case for head node */

        if (head == null || head.data

>= new_node.data) {

            new_node.next = head;

            head = new_node;

        }

        else {

            /* Locate the node before point of insertion. */

            current = head;

            while (current.next != null

&& current.next.data < new_node.data)

                current = current.next;

            new_node.next = current.next;

            current.next = new_node;

        }

    }

    /*Utility functions*/

    /* Function to create a node */

IntegerNode newNode(int data)

    {

IntegerNode newNode = new Node(data);

        return newNode;

    }

Just take a look at above function, it inserts the data into a give sorted list, and after insertion order remains the same

​​​​​​DELETION FUNCTION

void deleteNode(int value)

    {

        // Store head node

IntegerNode temp = head, prev = null;

        // If head node itself holds the key to be deleted

        if (temp != null && temp.data == value)

        {

            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.data != value)

        {

            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;

    }

NOTE : deletion function is same for all the linked list, if some node of particular value needs to be deleted.

TRAVERSING FUNCTION

public void printList()

    {

IntegerNode myHead = head;

        while (myHead != null)

        {

            System.out.print(myHead.data+" ");

MyHead = myHead.next;

        }

    }

Now, you just put all these functions, together and have fun

Also, try to name variables according to your needs, and give them meaning full names, it will help you out later.

Hope i helped you.

Happy Learning.

​​​​

​​


Related Solutions

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