Question: public class LinkedList { private Node front; //private int count; //private Node end; public LinkedList(Node f) { front = f; } public LinkedList() { front

public class LinkedList { private Node front; //private int count; //private Node end;
public LinkedList(Node f) { front = f; }
public LinkedList() { front = null;
} public void addToFront(String d) { Node n = new Node(d, front); front = n; } public boolean isEmpty() { if(front == null) return true; else return false; } public void clear() { front = null; } public String getFrontData() { return front.getData(); } public Node getFrontNode() { return front; } public String toString() { String ts = "["; Node cur = front; while(cur != null) { ts += cur; cur = cur.getNext(); } return ts + "]"; } public int size() { int count = 0; Node cur = front; while(cur != null) { count++; cur = cur.getNext(); } return count; } public void removeFront() { if(!isEmpty()) front = front.getNext(); // else // System.out.println(No front to remove!); } public void addToEnd(String d) { Node n = new Node(d, null); if(isEmpty()) front = n; else { Node cur = front; while(cur.getNext() != null) cur = cur.getNext(); cur.setNext(n); } } public void removeLast() { if(!isEmpty()) { if(front.getNext() == null) front = null; else { Node cur = front; while(cur.getNext() != null) cur = cur.getNext(); cur.setNext(null); } //} else { System.out.println(No end to remove!); } } public int contains(String d) { Node cur = front; boolean found = false; int index = -1; while(cur != null && !found) { index++; if(cur.getData().equals(d)) found = true; cur = cur.getNext(); } if(!found) index = -1; return index; } public void add(int index, String d) { if(index >= 0 && index = 0 && index = 0 && index = end || start size() || end >= size() )) { Node cur = getFrontNode(); for(int i=0; i } public class Node { private String data; private Node next; public Node(String d, Node n ) { data = d; next = n; } // The usual get/set methods, plus toString() public void setData(String d) { data = d; } public void setNext(Node n) { next = n; } public String getData() { return data; } public Node getNext() { return next; } public String toString() { return data + " --> "; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
