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 Node<T> { Public T Item { get; set; } Public Node<T> Next; { get;...
public class Node<T> { Public T Item { get; set; } Public Node<T> Next; { get; set; } public Node (T item, Node<T> next) { … } } public class Polynomial { // A reference to the first node of a singly linked list private Node<Term> front; // Creates the polynomial 0 public Polynomial ( ) { } // Inserts term t into the current polynomial in its proper order // If a term with the same exponent already exists...
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...
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;       ...
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 ==...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT