Question

// 1. Add methods to get and set, Data and Link. Data should be any Comparable...

//  1. Add methods to get and set, Data and Link. Data should be any Comparable object.
class Node {
    Integer data; // Integer is Comparable
    Node link;

    public Node(Integer data, Node link) {
        this.data = data;
        this.link = link;
    }

    public Integer getData() {
        return data;
    }

    public void setData(Integer data) {
        this.data = data;
    }

    public Node getLink() {
        return link;
    }

    public void setLink(Node link) {
        this.link = link;
    }
}

//  b.  Create MyLinkedList class and write methods to do the following:
class MyLinkedList {
    Node head;

    //  Insert a node at the end of the list
    void insertLast(Integer data) {
        Node newNode = new Node(data, null);
        if (head == null) {
            head = newNode;
            return;
        }

        Node last = head;
        while (last.link != null) {
            last = last.link;
        }
        last.link = newNode;
    }

    //  Insert a node at the front of the list
    void insertFirst(Integer data) {
        Node newNode = new Node(data, null);
        if (head == null) {
            head = newNode;
            return;
        }
        newNode.link = head;
        head = newNode;
    }

    //  Display all nodes in the list
    void display() {
        if (head == null) {
            System.out.println("List is empty");
            return;
        }

        Node temp = head;
        while (temp != null) {
            System.out.print(temp.data + "    ");
            temp = temp.link;
        }
        System.out.println();
    }

    //  Method to check of the list is empty - isEmpty() (returns a Boolean)
    Boolean isEmpty() {
        return head == null;
    }

    //  Method to return the size of the list
    int size() {
        int size = 0;
        Node temp = head;
        while (temp != null) {
            size++;
            temp = temp.link;
        }
        return size;
    }

    //  Delete all nodes in the list
    void deleteAllNodes() {
        head = null;
    }

    //  Search List for a node with a certain value (returns the node if found, null if not found)
    Node search(Integer value) {
        Node temp = head;
        while (temp != null) {
            if (temp.data.equals(value))
                return temp;
            temp = temp.link;
        }
        return null;
    }

    // Delete a node with a specific value in the list if found
    void deleteIfFound(Integer value) {
        Node curr = head, prev = head;
        if (head.data.equals(value)) {
            head = head.link;
            return;
        }
        while (curr != null) {
            if (curr.data.equals(value)) {
                prev.link = curr.link;
            }
            prev = curr;
            curr = curr.link;
        }
    }

    //  Method to find the node with the largest value in the list – getLargest()
    void getLargest() {
        Node temp = head, largest = head;
        while (temp != null) {
            if (temp.data.compareTo(largest.data) > 0)
                largest = temp;
            temp = temp.link;
        }
        System.out.println("Largest Value:" + largest.data);
    }

    //  Method to return all the elements less than a certain value as a linked list of type MyLinkedList
    MyLinkedList findLessThan(Integer x) {
        MyLinkedList newList = new MyLinkedList();
        Node temp = head;
        while (temp != null) {
            if (temp.data.compareTo(x) < 0) {
                newList.insertLast(temp.data);
            }
            temp = temp.link;
        }
        return newList;
    }
}

// Main Method to test all Linked list methods
public class NodeTest {
    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.display();

        myLinkedList.insertLast(10);
        myLinkedList.insertLast(15);
        myLinkedList.insertLast(12);
        System.out.println("list size: " + myLinkedList.size());
        myLinkedList.display();

        myLinkedList.insertFirst(53);
        myLinkedList.insertLast(62);
        myLinkedList.insertFirst(82);
        myLinkedList.insertLast(102);
        myLinkedList.insertLast(14);
        System.out.println("list size: " + myLinkedList.size());
        myLinkedList.display();

        System.out.println("20 " + (myLinkedList.search(20) == null ? "Not Found" : "Found"));
        System.out.println("15 " + (myLinkedList.search(62) == null ? "Not Found" : "Found"));

        System.out.println("Delete 10 if found");
        myLinkedList.deleteIfFound(10);
        myLinkedList.display();

        myLinkedList.getLargest();

        System.out.println("Get New List less than 50");
        MyLinkedList newList = myLinkedList.findLessThan(50);
        System.out.println("Printing New List:");
        newList.display();

        System.out.println("Deleting all nodes");
        myLinkedList.deleteAllNodes();
        myLinkedList.display();
    }
}

based on the pervious, please do the following completely or don't since it is one question with several parts, language used is java on netbeans.

Problem 2:

Use MyLinkedList in previous problem to create a TestIntegers method. TestIntegers method is to be called it in the main method to do the following:

  1. Create a new List (call it integerList) of type MyLinkedList

  2. Insert five integer numbers to the end of the list (not ordered). Wrap the numbers in the class

    “Integer”

  3. Display all the items in the List

  4. Insert elements at the front of the List and display it

  5. Display the size of the list

  6. Display the largest element in the list

  7. Search for a particular element in the List by printing true if found or false.

  8. Get and display all elements less than the Integer 20

  9. Delete a specific node in the List and display the list (in the beginning, middle and at the end)

  10. Delete the entire List and display if the list is empty or not

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class Main {
    public static void TestIntegers()
    {
        MyLinkedList myLinkedList = new MyLinkedList();
        
        myLinkedList.insertLast(10);
        myLinkedList.insertLast(15);
        myLinkedList.insertLast(12);
        myLinkedList.insertLast(1);
        myLinkedList.insertLast(25);
        
        System.out.println("List:");
        myLinkedList.display();
        
        System.out.println("Adding New Emement(53) in front of list");
        myLinkedList.insertFirst(53);
        
        System.out.println("list size: " + myLinkedList.size());
        
        myLinkedList.getLargest();
        
        System.out.println("" + (myLinkedList.search(20) == null ? "False" : "True"));
        System.out.println("" + (myLinkedList.search(10) == null ? "False" : "True"));
        
        System.out.println("Elements less than 20");
        MyLinkedList newList = myLinkedList.findLessThan(20);
        newList.display();
        
        myLinkedList.display();
        System.out.println("Delete 53(first node) if found");
        myLinkedList.deleteIfFound(53);
        myLinkedList.display();
        System.out.println("Delete 12(middle node) if found");
        myLinkedList.deleteIfFound(12);
        myLinkedList.display();
        System.out.println("Delete 25(end node) if found");
        myLinkedList.deleteIfFound(25);
        myLinkedList.display();
        
        System.out.println("Deleting all nodes");
        myLinkedList.deleteAllNodes();
        myLinkedList.display();
        
    }
    
    public static void main(String[] args) {
        
        TestIntegers();


        // MyLinkedList myLinkedList = new MyLinkedList();
        // myLinkedList.display();

        // myLinkedList.insertLast(10);
        // myLinkedList.insertLast(15);
        // myLinkedList.insertLast(12);
        // System.out.println("list size: " + myLinkedList.size());
        // myLinkedList.display();

        // myLinkedList.insertFirst(53);
        // myLinkedList.insertLast(62);
        // myLinkedList.insertFirst(82);
        // myLinkedList.insertLast(102);
        // myLinkedList.insertLast(14);
        // System.out.println("list size: " + myLinkedList.size());
        // myLinkedList.display();

        // System.out.println("20 " + (myLinkedList.search(20) == null ? "Not Found" : "Found"));
        // System.out.println("15 " + (myLinkedList.search(62) == null ? "Not Found" : "Found"));

        // System.out.println("Delete 10 if found");
        // myLinkedList.deleteIfFound(10);
        // myLinkedList.display();

        // myLinkedList.getLargest();

        // System.out.println("Get New List less than 50");
        // MyLinkedList newList = myLinkedList.findLessThan(50);
        // System.out.println("Printing New List:");
        // newList.display();

        // System.out.println("Deleting all nodes");
        // myLinkedList.deleteAllNodes();
        // myLinkedList.display();
    }

}

Add a comment
Know the answer?
Add Answer to:
// 1. Add methods to get and set, Data and Link. Data should be any Comparable...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...

    could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...

  • Add the following method to xxxxxp3: // deletes x from sorted list k if exist, k...

    Add the following method to xxxxxp3: // deletes x from sorted list k if exist, k = 0, 1, 2 private void delete(x, k) // returns position of x in sorted list k if exist otherwise, -1. k = 0, 1, 2 private int search(x, k) import java.util.Scanner; class xxxxxp3{ private node[] head = new node[3]; private class node{ int num; node link; node(int x){ num=x; link = null; } } public void prtlst(int k){ System.out.printf("\nContents of List-%d:",k); for(node cur...

  • Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given reference...

    Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable {    // ---------------- nested Node class...

  • **HELP** Write a function that takes a linked list of items and deletes all repetitions from the ...

    **HELP** Write a function that takes a linked list of items and deletes all repetitions from the list. In your implementation, assume that items can be compared for equality using ==, that you used in the lab. The prototype may look like: void delete_repetitions(LinkedList& list); ** Node.h ** #ifndef Node_h #define Node_h class Node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR Node(const value_type& init_data = value_type( ), Node* init_link = NULL) { data_field = init_data; link_field = init_link;...

  • Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual...

    Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual studio 2013 this is my code so far #include <cstdlib> #include <list> #include <iostream> using namespace std; struct node {    int data;    node* next; }; void initialize(node*p); void insert(int data,int n); void remove(int b,int pos); bool empty(node*); void length(); bool valuein(int c); int reportvalue(int value); void ptintlist(); int menu(); int main() {    struct node*head=NULL;    char commands;    int value;   ...

  • Improve the speed of public T get(int i) by having it work backward from the end...

    Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> {    private static class Node<T> {        public Node<T> prev, next;...

  • P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList()...

    P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList() { header = null; } public final Node Search(int key) { Node current = header; while (current != null && current.item != key) { current = current.link; } return current; } public final void Append(int newItem) { Node newNode = new Node(newItem); newNode.link = header; header = newNode; } public final Node Remove() { Node x = header; if (header != null) { header...

  • In def c++ IDE correct mistake and screenshot output ------------------------------------------------------------------------------ #define NULL 0 #include <iostream> /*...

    In def c++ IDE correct mistake and screenshot output ------------------------------------------------------------------------------ #define NULL 0 #include <iostream> /* Name: Rabia Saad Al-wethnani ID: 43503535 Section: 2486 */ using namespace std; struct Node { int item; //data Node *next; //pointer to the next node in the list }; Node* createNode(int); int isPresent(Node*, int); void appendNode(Node*&, int); void displayList(Node*); void printLists(Node*, Node*); void swapNodes(Node* &head, int s); void findIntersection(Node* first, Node* P); void findUnion(Node* first, Node* P); int main() { Node* L = NULL,...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT