Question

Q2 [15 pts] Linked list: For this question use class Node.java Assume this file implements a...

Q2 [15 pts] Linked list: For this question use class Node.java

Assume this file implements a node in a linked list. It has the following public data members:

➢ int data

➢ Node next

Write class Node and Class single linked list then write the following functions:

2.2 Remove duplicates from an unsorted linked list Write a Java method removeDuplicates()

which takes a list and deletes any duplicate nodes from the list. The list is not sorted.

Example:

if the linked list is 12->11->12->21->41->43->21 then removeDuplicates() should convert the list to 12->11->21->41->43.

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

Here is your code. I created some extra method for checking list easily. You can remove it, if you don't need it really.

Node.java

//Created node class with two data member.
public class Node {
    int data;
    Node next;
}
SingleLinkedList.java


import java.util.HashSet;

//Created SingleLinkedList with a constructor and
//and one few method extra for adding and printing list.
public class SingleLinkedList {
    //Start node, where list get started.
    private Node head;
    //The current node, which hold the current reference.
    private Node currenNode;

    //One initialize it takes data and put in head node.
    //and this head node is our current node too.
    public SingleLinkedList(int data){
        head = new Node();
        head.data = data;
        head.next = null;
        currenNode = head;
    }

    //This is optional method, not asked in question.
    //It is for adding new item to list.
    public void addItem(int data){
        //Create a node for this item.
        Node newNode = new Node();
        //put the data.
        newNode.data = data;
        //now point our current's next node to this node.
        currenNode.next = newNode;
        //and finally make this node as a current node.
        currenNode = newNode;
    }

    //Optional method that will return the reference for this
    //single linked list. Remember, only head node stores
    //start reference of our list.
    public Node getSingleLinkedList(){
        return head;
    }

    //This method remove the duplicated.
    public void removeDuplicates(Node node){
        //First create a temp node i.e. current
        Node current = node;
        //Create hashSet of integer to store the node's data.
        //In which we will look every time for duplicate data.
        HashSet<Integer> integers = new HashSet<>();

        //Now put the current node data to hashSet.
        integers.add(current.data);

        //Iterate through every node. until current.next not equals
        //to null.
        while (current.next != null){
            //check if next node's data is in hashSet or not.
            //If yes, it means this data already comes before this node.
            if(integers.contains(current.next.data)){
                //skip next node of current node.
                //by making current next node equals to
                // current.nextNode.nextNode.
                current.next = current.next.next;
            }else{
                //If data is unique, add to hash set,
                //and point current node to current.next node.
                integers.add(current.next.data);
                current = current.next;
            }
        }
    }

    //Simple method to print every node from the list.
    public void printList(){
        Node current = head;
        while (current.next != null){
            System.out.print(current.data + "->");
            current = current.next;
        }
        System.out.println(current.data);

    }
}

Main.java(Driver Class)

public class Main {

    public static void main(String[] args) {
       SingleLinkedList linkedList = new SingleLinkedList(12);
       linkedList.addItem(11);
       linkedList.addItem(12);
       linkedList.addItem(21);
       linkedList.addItem(41);
       linkedList.addItem(43);
       linkedList.addItem(21);
       linkedList.printList();

       linkedList.removeDuplicates(linkedList.getSingleLinkedList());

       linkedList.printList();
    }
}

Output

Add a comment
Know the answer?
Add Answer to:
Q2 [15 pts] Linked list: For this question use class Node.java Assume this file implements a...
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
  • Assume we have a linked list with 12 nodes pointed to by FIRST. Write a function...

    Assume we have a linked list with 12 nodes pointed to by FIRST. Write a function that deletes the first node. Write a function that deletes the 7th node. Write a function that deletes the last node.

  • Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype...

    Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype data; node *tail; }; class LinkedList{ private: node *head; node *current;public: //constructors LinkedList(); LinkedList(int i); //destructor ~LinkedList(); bool start(); //sets list postion to header bool nextNode(); //increments to next node in list int getCurrent(); //returns data from current node void insertNode(int i); //inserts node after current node    //then sets current node to new node bool deleteNode();//deletes currentnode void deleteAll(); //deletes all nodes };...

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

  • Linked List in Java The data node should be modeled somewhat like this: class node{ int...

    Linked List in Java The data node should be modeled somewhat like this: class node{ int node iNum; node next; } Write a program that creates a linked list and loads it with the numbers 0 to 9. Start with an empty list and then use a "for loop" to fill it. Create a linked list class, a node class, etc. Routines like makeNode and findTail should be methods in the linked list class. Create a showList function to display...

  • List of Candles Create a class called CandleNode which has fields for the data (a Candle)...

    List of Candles Create a class called CandleNode which has fields for the data (a Candle) and next (CandleNode) instance variables. Include a one-argument constructor which takes a Candle as a parameter. (For hints, see the PowerPoint on "Static vs. Dynamic Structures”.) public CandleNode (Candle c) { . . } The instance variables should have protected access. There will not be any get and set methods for the two instance variables. Create an abstract linked list class called CandleList. This...

  • Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list -...

    Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list - Reference: chapter 18: Create an array of 15 student records that should not be sorted Create a liked list of 15 student record nodes. Each node is a node of one student record from the above unsorted array. The list of student records should be sorted by student ID. (Insert function without sort function to create a linked list.) (If you insert correctly, the...

  • CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to...

    CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to implement its inventory of computing machines as a linked list, called ComputerList. Write a Computer node class, called ComputerNode, to hold the following information about a Computer: • code (as a String) • brand (as a String) • model (as a String) • price (as double) • quantity (as int) ComputerNode should have constructors and methods (getters, setters, and toString()) to manage the above...

  • c++ Computational Complexity Create a singly linked list for storing positive integers. Each node will store...

    c++ Computational Complexity Create a singly linked list for storing positive integers. Each node will store one integer. For example, 12->3->5->777-111 is such a list. There are five nodes in this list. 12 is the head node and 111 is the tail node. (111 points to NULL.) Your linked list starts empty. It should support the following three operations: Add x to tail: Create a new node whose data field contains x. Append this node at the end of the...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • Please write a c++ header file, class implementation file and main file that does all of...

    Please write a c++ header file, class implementation file and main file that does all of the following and meets the requirements listed below. Also include a Output of your code as to show that your program works and functions properly. EXERCISING A DOUBLY-LINKED LIST CLASS This project consists of two parts, the second of which appears below. For the first part, write a class that implements an unordered list abstract data type using a doubly-linked list with pointers to...

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