Help with some homework. Change and add to this code using java the following Si
ID: 3881499 • Letter: H
Question
Help with some homework. Change and add to this code using java the following
SinglyLinkedList(String fileName);
This is a constructor for the SinglyLinkedList class which will initialize the empty list
with the integers provided in the file named fileName. You can assume that integers in
the file are separated by commas
public String toString();
This method returns a string which contains a human-readable representation of the object.
In our case, if a linked list object myList has the content 3, 7,77, 50, then printing
out myList using System.out.println(myList); must print the following:
3 > 7 > -77 > 50
public void reverseList();
This method reverses a singly linked list
public void appendList(SinglyLinkedList anotherList);
This method appends the supplied linked list to the current one. Here, anotherList
must remain unmodified at the end. Thus, new nodes must be created and added to the
end of the current list.
public int countOccurence(int element);
This method returns how many times the integer element is present in the list
Assume that the input linked list contains numbers only in the file inputList.txt elements separated
by commas.
Code required but instead of generics switch to int
public class SinglyLinkedList<E> {
private static class Node{
private E element;
private Node<E> next;
public Node (E e, Node<E> n) {
element = e;
next = n;
}
public E getElement() {return element;}
public Node<E> getNext(){return next;}
public void setNext(Node<E> n) {next = n;}
//instance variables of the singlylinkedlist
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
public SinglyLinkedList() {}
//access methods
public int size() {return size;}
public boolean isEmpty() { return size == 0;}
public E first() {
if(isEmpty())return null;
return head.getElement();
}
public E last() {
if(isEmpty())return null;
return tail.getElement();
}
}
Explanation / Answer
here is your class : --------------->>>>>>>>>>>
import java.util.Scanner;
import java.util.File;
public class SinglyLinkedList<E> {
private static class Node{
private E element;
private Node<E> next;
public Node (E e, Node<E> n) {
element = e;
next = n;
}
public E getElement() {return element;}
public Node<E> getNext(){return next;}
public void setNext(Node<E> n) {next = n;}
}
//instance variables of the singlylinkedlist
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
public SinglyLinkedList() {}
SinglyLinkedList(String fileName){
try{
Integer data = new Integer();
Scanner sc = new Scanner(new File(fileName));
while(sc.hasNext()){
data = sc.nextInt();
add(data);
}
}catch(Exception e){e.printStackTrace();}
}
//access methods
public int size() {return size;}
public boolean isEmpty() { return size == 0;}
public E first() {
if(isEmpty())return null;
return head.getElement();
}
public boolean add(E element){
if(isEmpty()){
head = new Node(element,null);
tail = head;
size++;
return true;
}else{
Node<E> temp = new Node(element,head);
head = temp;
size++;
return true;
}
return false;
}
public boolean remove(E element){
if(isEmpty()){
return false;
}
Node<E> temp = head;
Node<E> prev = null;
while(temp != null){
if(temp.getElement().equals(element)){
if(prev == null){
head = null;
tail = null;
size--;
return true;
}else{
prev.setNext(null);
if(tail == temp){
tail = prev;
}
size--;
return true;
}
}
prev = temp;
temp = temp.getNext();
}
return false;
}
public boolean remove(){
if(isEmpty()){
return false;
}
if(head == tail){
head = null;
tail = null;
size--;
return true;
}
head = head.getNext();
size--;
return true;
}
public E last() {
if(isEmpty())return null;
return tail.getElement();
}
public int countOccurence(int element){
Node<E> temp = head;
int c =0;
while(temp != null){
if(temp.getElement() == elemnet){
c++;
}
temp = temp.getNext();
}
return c;
}
public void reverseList(){
SinglyLinkedList temp;
int s = size;
for(int i = 0;i<s;i++){
temp.add(last());
remove(last());
}
s = temp.size();
for(int i = 0;i<s;i++){
add(temp.first());
temp.remove();
}
}
public String toString(){
String s;
Node<E> temp = head;
for(int i = 0;i<size;i++){
s += temp.getElement();
if(i != size -1 ){
s += " > ";
}
temp = temp.getNext();
}
return s;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.