package unl.cse.queues; import unl.cse.linked_list.LinkedList; public class Queu
ID: 3569415 • Letter: P
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");
}
}
Explanation / Answer
Hi,
I'm also stuck on this problem. Could you please send me the answer about that? Thank you!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.