Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class LinkedList { private Node head; private Node tail; / / About method public String About ( ) { return This LinkedList class was

public class LinkedList {
private Node head;
private Node tail;
// About method
public String About(){
return "This LinkedList class was implemented by Gabriel";
}
// AddHead method
public void AddHead(String value){
Node newNode = new Node(value);
newNode.next = head;
head = newNode;
if (tail == null){
tail = head;
}
}
// AddTail method
public void AddTail(String value){
Node newNode = new Node(value);
if (tail == null){
head = tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
}
// RemoveHead method
public String RemoveHead(){
if (head == null){
return "";
}
String value = head.value;
head = head.next;
if (head == null){
tail = null;
}
return value;
}
// RemoveTail method
public String RemoveTail(){
if (head == null){
return "";
}
if (head == tail){
String value = head.value;
head = tail = null;
return value;
}
Node current = head;
while (current.next != tail){
current = current.next;
}
String value = tail.value;
tail = current;
tail.next = null;
return value;
}
// IsEmpty method
public boolean IsEmpty(){
return head == null;
}
// ToString method
public String ToString(){
if (head == null){
return "";
}
StringBuilder sb = new StringBuilder();
Node current = head;
while (current != null){
sb.append(current.value);
if (current.next != null){
sb.append(",");
}
current = current.next;
}
return sb.toString();
}
// ToStringReverse method
public String ToStringReverse(){
if (head == null){
return "";
}
StringBuilder sb = new StringBuilder();
Node current = tail;
while (current != null){
sb.append(current.value);
if (current != head){
sb.append(",");
}
current = current.prev;
}
return sb.toString();
}
// Node class
private class Node {
private String value;
private Node next;
private Node prev; // for ToStringReverse
public Node(String value){
this.value = value;
}
}
}
In java programming, can you add a code in main class on how to read a file under LinkedList programming, and please do not put it inside the LinkedList class. Different programming languages implement file IO in different ways. Some use classes and others use more primitive (or
ingrained) features. This will take the form of either a While Loop or a Do Loop. In all cases, your main testing class will read
the contents of the file and it to a instance of your linked-list class.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Theory Icdt 99 7th International Conference Jerusalem Israel January 10 12 1999 Proceedings Lncs 1540

Authors: Catriel Beeri ,Peter Buneman

1st Edition

3540654526, 978-3540654520

More Books

Students also viewed these Databases questions

Question

Describe two methods for estimating the cost of retained earnings.

Answered: 1 week ago