Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question package unl.cse.queues; import unl.cse.linked_list.LinkedList; public c

ID: 653784 • Letter: Q

Question

Question

package unl.cse.queues;

import unl.cse.linked_list.LinkedList;

public class Queue {

   private final LinkedList list = new LinkedList();
  
   public T dequeue() {
       //TODO: implement this method
       return null;
   }
  
   public void enqueue(T item) {
       //TODO: implement this method
   }

   public int size() {
       //TODO: implement this method
       return -1;
   }
  
   public boolean isEmpty() {
       //TODO: implement this method
       return false;
   }
  
}

-------------------------------------------------------------------------

package unl.cse.linked_list;

import java.util.Iterator;

public class LinkedList implements Iterable {

   private Node head = null;
   private Node tail = null;
  
   public void addElementToHead(T item) {
       if(item == null)
           throw new IllegalArgumentException("This LinkedList impelmentation does not allow null elements");
       Node newHead = new Node(item);
       if(this.tail == null) {
           this.head = newHead;
           this.tail = newHead;
       } else {
           newHead.setNext(this.head);
           this.head.setPrevious(newHead);
           this.head = newHead;
       }
   }
  
   public T removeElementFromHead() {
       T item = null;
       if(this.size() == 0) {
           throw new IllegalStateException("Cannot remove from an empty list");
       } else if(this.size() == 1) {
           item = this.head.getItem();
           this.head = null;
           this.tail = null;
       } else {
           item = this.head.getItem();
           this.head = this.head.getNext();
           this.head.setPrevious(null);
       }
       return item;
   }
  
   /**
   * Returns the element at the head of this list, but does not remove it.
   * @return
   */
   public T getElementFromHead() {
       if(this.size() == 0) {
           throw new IllegalStateException("Cannot retrieve from an empty list");
       } else {
           return this.head.getItem();
       }
   }
  
   public void addElementToTail(T item) {
       if(item == null)
           throw new IllegalArgumentException("This LinkedList impelmentation does not allow null elements");
       Node newTail = new Node(item);
       if(this.tail == null) {
           this.tail = newTail;
           this.head = newTail;
       } else {
           newTail.setPrevious(this.tail);
           this.tail.setNext(newTail);
           this.tail = newTail;
       }
   }
  
   public T removeElementFromTail() {
       T item = null;
       if(this.size() == 0) {
           throw new IllegalStateException("Cannot remove from an empty list");
       } else if(this.size() == 1) {
           item = this.tail.getItem();
           this.head = null;
           this.tail = null;
       } else {
           item = this.tail.getItem();
           this.tail = this.tail.getPrevious();
           this.tail.setNext(null);
       }
       return item;
   }

   /**
   * Returns the element at the tail of this list, but does not remove it.
   * @return
   */
   public T getElementFromTail() {
       if(this.size() == 0) {
           throw new IllegalStateException("Cannot retrieve from an empty list");
       } else {
           return this.tail.getItem();
       }
   }

   public int size() {
       int count = 0;
       for(T item : this) {
           count++;
       }
       return count;
   }
  
   public boolean isEmpty() {
       return (head == null);
   }
  
   @Override
   public Iterator iterator() {
       return new Iterator() {
           Node curr = head;
           @Override
           public boolean hasNext() {
               if(curr == null)
                   return false;
               else
                   return true;
           }
           @Override
           public T next() {
               T item = curr.getItem();
               curr = curr.getNext();
               return item;
           }

           @Override
           public void remove() {
               throw new UnsupportedOperationException("not implemented");
           }};
   }

   @Override
   public String toString() {
       if(this.head == null) {
           return "[empty]";
       }
       StringBuilder sb = new StringBuilder();
       sb.append("[");
       Node curr = head;
       while(curr != null) {
           sb.append(curr.getItem());
           if(curr.getNext() != null)
               sb.append(", ");
           curr = curr.getNext();
       }
       sb.append("]");
       return sb.toString();
   }

   public static void main(String args[]) {
       LinkedList llist = new LinkedList();
       llist.addElementToHead(10);
       llist.addElementToHead(20);
       llist.addElementToTail(-10);
       llist.addElementToTail(-20);
       System.out.println(llist);
       System.out.println(llist.removeElementFromHead());
       System.out.println(llist.removeElementFromTail());
       System.out.println(llist);
       llist.addElementToTail(-50);
       llist.addElementToTail(-60);
       System.out.println(llist);
       System.out.println(llist.isEmpty());
       System.out.println(llist.removeElementFromTail());
       System.out.println(llist.removeElementFromTail());
       System.out.println(llist.removeElementFromTail());
       System.out.println(llist.removeElementFromTail());
       System.out.println(llist.isEmpty());
       llist.addElementToTail(-50);
       llist.addElementToTail(-60);
       System.out.println(llist);
   }

}

-------------------------------------------------------------------------

package unl.cse.linked_list;

public class Node {

   private final T item;
   private Node prev;
   private Node next;
  
   public Node(T item) {
       this.item = item;
       next = null;
   }

   public T getItem() {
       return this.item;
   }

   public void setNext(Node next) {
       this.next = next;
   }
  
   public void setPrevious(Node prev) {
       this.prev = prev;
   }
  
   public Node getNext() {
       return this.next;
   }
  
   public Node getPrevious() {
       return this.prev;
   }
  
}

--------------------------------------------------------------------------

package unl.cse.queues;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class FileReader {

   Queue lines = new Queue();
  
   public void readFile(String fileName) {
       try {
           Scanner s = new Scanner(new File(fileName));
           while(s.hasNext()) {
               String line = s.nextLine();
               //TODO: do something with line
           }
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
   }
  
   public void display() {
      
       int maxCols = 80;
       int maxLines = 40;
       boolean cont = true;
       while(!lines.isEmpty() && cont) {
          
           StringBuilder sb = new StringBuilder();
           for(int i=0; i                //TODO: modify the following line of code to get an element from the queue
               String line = null;
               if(line.length() < maxCols) {
                   sb.append(line);
                   sb.append(" ");
                   i++;
               } else {
                   String words[] = line.split("\s+");
                   StringBuilder subLine = new StringBuilder();
                   for(String w : words) {
                       if( (w.length() + subLine.length() + 1) < maxCols) {
                           subLine.append(w);
                           subLine.append(" ");
                       } else {
                           sb.append(subLine.toString());
                           sb.append(" ");
                           subLine = new StringBuilder();
                           subLine.append(w);
                           subLine.append(" ");
                       }
                   }
                   sb.append(subLine);
                   sb.append(" ");
               }
           }
           System.out.println(sb.toString());
          
           System.out.println("PRESS TO CONTINUE, Q-ENTER to QUIT...");
           int b = 0;
           try {
               b = System.in.read();
           } catch (IOException e) {
               e.printStackTrace();
           }
           if(b == 81 || b == 113)
               cont = false;
       }
   }
  
   public static void main(String args[]) {
       String fileName = "data/star_wars.txt";
       FileReader fr = new FileReader();
       fr.readFile(fileName);
       fr.display();
       System.out.println("DONE");
   }
}

Queue data structures, in contrast to Stacks, are a FIFO (First-In First-Out) data structure. Like stacks, they can easily be implemented using a linked list. Queues have numerous applications; one such application is as a data buffer. In many applications, processing a stream of data is expensive. The data stream may be faster than an application can process it. For this reason, incoming data is temporarily stored in a buffer (a queue). The application then reads from the buffer and processes the data independent of the data stream. In this activity, you will implement and use a queue that acts as a data buffer for a plain text file. The application reads an entire text file and stores lines into your queue for later processing. The processing in this case is a human user that reads the text file page-by-page (by pressing enter). In addition, the text file is not well-formatted for human readers as it contains very long lines. The application processes these lines and displays them page-by-page with a predefined limit on the number of characters per line so that it is more human-user friendly. It will be your responsibility to complete the implementation of the Queue class and to use it properly in the FileReader class. Implement the four methods in the unl. cse.queue. Queue class (which uses the fully implemented LinkedList class) As before, you could test your implementation by creating a small main method and enqueue/dequeue elements from your to see if you get the expected behavior. Once your queue is implemented, modify the FileReader class as specified in the source code (the TODO tasks) to use your queue to process the text file.

Explanation / Answer

public T dequeue() {
   if (list.isEmpty() == true)
       return null;
   else{
return removeElementFromHead();
   }
}

public void enqueue(T item) {
list.addElementToTail(item)
}

public int size() {
   return list.size();
}


public boolean isEmpty() {
   return (list.isEmpty() && true);
}

/* Here are 4 function */

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote