Question

Using a doubly linked list, create a list L1 with words from a text file in...

Using a doubly linked list, create a list L1 with words from a text file in Java.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FileWords {

    public static void main(String[] args) throws FileNotFoundException {
        String fileName = "input.txt";

        Scanner reader = new Scanner(new File(fileName));

        DoublyLinkedList<String> list = new DoublyLinkedList<>();
        while(reader.hasNext()) {
            list.addLast(reader.next());
        }

        list.display();

        reader.close();
    }

}

=====================

public class DoublyLinkedList<E> {
    private Node<E> header;
    private Node<E> trailer;
    private int size = 0;

    public DoublyLinkedList() {
        header = null;
        trailer = null;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public E first() {
        if (isEmpty())
            return null;
        return header.getElement();
    }

    public E last() {
        if (isEmpty())
            return null;
        return trailer.getElement();
    }

    private void addBetween(E e, Node<E> predecessor, Node<E> successor) {
        Node<E> newest = new Node<>(e, predecessor, successor);
        if(predecessor == null) {
            header = newest;
        } else {
            predecessor.setNext(newest);
        }

        if(successor == null) {
            trailer = newest;
        } else {
            successor.setPrev(newest);
        }
        size++;
    }

    private E remove(Node<E> e) {
        Node<E> predecessor = e.getPrev();
        Node<E> successor = e.getNext();
        if(predecessor == null) {
            header = successor;
        } else {
            predecessor.setNext(successor);
        }
        if(successor == null) {
            trailer = predecessor;
        } else {
            successor.setPrev(predecessor);
        }
        size--;
        if(size == 0) {
            header = trailer = null;
        }
        return e.getElement();
    }

    public void addFirst(E e) {
        if(header == null) {
            addBetween(e, null, null);
        } else {
            addBetween(e, header, header.getNext());
        }
    }

    public void addLast(E e) {
        if(trailer == null) {
            addBetween(e, null, null);
        } else {
            addBetween(e, trailer.getPrev(), trailer);
        }
    }

    public E removeFirst() {
        if (isEmpty())
            return null;
        return remove(header);

    }

    public E removeLast() {
        if (isEmpty())
            return null;
        return remove(trailer);

    }

    public void reverse() {
        if(isEmpty() || header == trailer) {
            return;
        }
        Node<E> prev = null;
        Node<E> start = header;
        while(start != null) {
            Node<E> next = start.next;
            start.next = prev;
            start.prev = next;
            prev = start;
            start = next;
        }
        trailer = header;
        header = prev; 
    }

    public void display() {
        Node<E> start = header;
        while(start != null) {
            System.out.print(start.element + " ");
            start = start.next;
        }
        System.out.println();
    }

    private static class Node<E> {
        private E element;
        private Node<E> next;
        private Node<E> prev;

        public Node(E e, Node<E> p, Node<E> n) {
            element = e;
            prev = p;
            next = n;
        }

        public E getElement() {
            return element;
        }

        public Node<E> getPrev() {
            return prev;
        }

        public Node<E> getNext() {
            return next;
        }

        public void setPrev(Node<E> n) {
            prev = n;
        }

        public void setNext(Node<E> n) {
            next = n;
        }

    }
}


Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
Using a doubly linked list, create a list L1 with words from a text file in...
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
  • in c++ Create a circular, doubly linked list from a file (input.txt), containing a list of...

    in c++ Create a circular, doubly linked list from a file (input.txt), containing a list of names. The number of names is unknown. Prompt the user for the number of nodes to delete and then delete accordingly from the list. Assumption: The number inputted by the user will not exceed the actual number of nodes in the list.

  • In C++ Create a data structure doubly linked list, implement the following operations for the doubly...

    In C++ Create a data structure doubly linked list, implement the following operations for the doubly linked list: addFirst addLast insertBefore insertAfter delete printList

  • write any 2 c functions for a linked list and a doubly-linked sorted list create at...

    write any 2 c functions for a linked list and a doubly-linked sorted list create at least two data structures

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • JAVA please Java problem that has to be in doubly linked list. It is game problem...

    JAVA please Java problem that has to be in doubly linked list. It is game problem that I have to keep of the players' scores. this should have a Java blueprint class named player   It should have two fields, the name and score and include the constructors, toString() method, and getters and setters. Scores must be kept in ascending order (smallest at the front ) in a doubly linked list. It has menu as well: Load original data Print the...

  • Q.(1)Describe the algorithm and java implementation for the following operations A. Create a singly linked list...

    Q.(1)Describe the algorithm and java implementation for the following operations A. Create a singly linked list L1 with 4 nodes. You can use insert operation to add nodes to the list. Each element represent an airport code (e.g. BOS, ATL, JFK, MSP, etc.). Display the list L1 after it is created. B. Given singly linked list L1, create another singly linked list L2 that contains the same elements but in the reverse order. Display the content of both L1 and...

  • Using C++, create a doubly linked list data structure that stores strings. At a minimum, you...

    Using C++, create a doubly linked list data structure that stores strings. At a minimum, you must have a List class that contains the list functionality (including an insert function) and a linkable object ("link node") class. For convenience, you may include the data directly or the data may be external to the link node, connected with a reference. You may use an inner class for the LinkNode and/or include the LinkNode class with the List class file if you...

  • Using C++, create a doubly linked list data structure that stores strings. At a minimum, you...

    Using C++, create a doubly linked list data structure that stores strings. At a minimum, you must have a List class that contains the list functionality (including an insert function) and a linkable object ("link node") class. For convenience, you may include the data directly or the data may be external to the link node, connected with a reference. You may use an inner class for the LinkNode and/or include the LinkNode class with the List class file if you...

  • I already created a doubly linked list class. I need help doing this and adding the...

    I already created a doubly linked list class. I need help doing this and adding the doubly linked list class to this. I need this for Java language if someone can please help me Step 2 Stack and Queue Using the linked list class you created in Step 1 create stack and queue classes. Iwill leave it up to you as to whether to use composition or inheritance but whatever way you choose to go you should be able to...

  • 3. How can you implement a queue data structure using a doubly linked list? Do you think it is necessary to use a doubl...

    3. How can you implement a queue data structure using a doubly linked list? Do you think it is necessary to use a doubly linked list rather than a singly linked list or not?(3 marks) 3. How can you implement a queue data structure using a doubly linked list? Do you think it is necessary to use a doubly linked list rather than a singly linked list or not?(3 marks)

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