Using a doubly linked list, create a list L1 with words from a text file in Java.
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!
Using a doubly linked list, create a list L1 with words from a text file in...
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 least two data structures
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 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 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 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 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)
Create a random access Java program to insert 3 missing words into a text file. You will be supplied with the missing words, which you may hard code in your program. You need to determine from the given text file where the words should be placed! Missing words are: details populate determine Randominput.txt Create a class to hold the x_x_x_x of a new vehicle (include make, model, type, color, number of doors and price as a minimum). Include an ___init___...
java write a method to to find the negative numbers in L1 and L2 (linked list) and put them in L3 .
#1 [4 points] a) Draw a sketch of a doubly-linked list, specifically of class DLinkedList in DLinkedList.h, containing the courses, including the section number, you are taking this semester; for example, CPSC 131.01 will be "CPSC 131.01". Make sure to show the DLinkedList object, all node objects, and all objects' data members and pointers. b) Complete the following main function to create your linked list above (Your code should go in only the indicated space - do not change any...