// A class\'s description to be provided by student // Fill in code in ALL comme
ID: 3728033 • Letter: #
Question
// A class's description to be provided by student
// Fill in code in ALL commented areas
public class LinkedList {
// Defined Node class
private class Node {
private Object Data = null;
private Node Next = null;
public Node() {
Data = null;
Next = null;
}
public Node(Object element) {
Data = element;
}
public Node(Object o, Node n) {
Data = o;
Next = n;
}
public void setNext(Node n) {
Next = n;
}
public Node getNext() {
return Next;
}
public Object getElement() {
return Data;
}
public void setElement(Object element) {
Data = element;
}
}
// Internal data for LinkedList
private Node head = null;
private Node current = null;
private int size = 0;
Explanation / Answer
here is your program : ------------>>>>>>
public class LinkedList {
// Defined Node class
private class Node {
private Object Data = null;
private Node Next = null;
public Node() {
Data = null;
Next = null;
}
public Node(Object element) {
Data = element;
}
public Node(Object o, Node n) {
Data = o;
Next = n;
}
public void setNext(Node n) {
Next = n;
}
public Node getNext() {
return Next;
}
public Object getElement() {
return Data;
}
public void setElement(Object element) {
Data = element;
}
}
// Internal data for LinkedList
private Node head = null;
private Node current = null;
private int size = 0;
// LinkedList constructor
public LinkedList() {
head = null;
current = head;
}
// Move the current position forward one position
public void forward() {
if(isEmpty()){
return;
}
if(current == null){
current = head;
return;
}
if(current.getNext() == null){
return;
}
current = current.getNext();
}
// Move the current position backward one position
public void backward() {
if(isEmpty()){
return;
}
if(current == head){
return;
}
Node temp = head;
while(temp.getNext() != current){
temp = temp.getNext();
}
current = temp;
}
// Get current object's data element
public Object currentData() {
return current.getElement();
}
// Add object to the first of the list
public void addFirst(Object o) {
Node temp(o,head);
head = temp;
size++;
}
// resetCurrent at the first position
public void resetCurrent() {
current = head;
}
// Add object to the last of the list
public void addLast(Object o) {
Node temp(o,null);
Node tmp = head;
while(tmp.getNext() != null){
tmp = tmp.getNext();
}
tmp.setNext(temp);
size++;
}
// Add an object o before the current position
public void insertBefore(Object o) {
if(current == null || current == head){
addFirst(o);
return;
}
Node temp(o,current);
Node tmp = head;
while(tmp.getNext() != current){
tmp = tmp.getNext();
}
tmp.setNext(temp);
size++;
}
// Add an object o after the current position
public void insertAfter(Object o) {
if(current == null){
addFirst();
return;
}
Node temp(o,current.getNext());
current.setNext(temp);
}
// Get first objectpublic
Object getFirst() {
return head.getElement();
}
// Get last object
public Object getLast() {
Node temp = head;
while(temp.getNext() != null){
temp = temp.getNext();
}
return temp.getElement();
}
// Remove the first object
public Object removeFirst(){
if(isEmpty()){
return;
}
head = head.getNext();
size--;
}
// Remove the last object
public Object removeLast() {
if(isEmpty()){
return;
}
Node temp = head;
while(temp.getNext().getNext() != null){
temp = temp.getNext();
}
temp.setNext(null);
}
// Remove object o from the list
public void remove(Object o) {
if(isEmpty()){
return;
}
Node temp = head;
Node prev = null;
while(temp != null){
if(temp.getElement().equals(o)){
if(current == temp){
forward();
}
if(prev == null){
removeFirst();
retrun;
}
prev.setNext(temp.getNext());
size--;
return;
}
temp = temp.getNext();
}
}
// Returns the number of elements on the list
public int getSize() {
return size;
}
// Is the list emptied?
public boolean isEmpty() {
if(size == 0 || head == null){
return true;
}
return false;
}
// Display a content of a list
public String toString() {
String r = "( HEAD -> ";
// Node l = head.getNext();
Node l = head;
while (l != null) {
r = r + l.getElement() + " -> " ;
l = l.getNext();
}
return r + " )";
}
public static void main(String args[]) {
LinkedList lst = new LinkedList();
// creat instances for testing
CsusStudent instance1 = new CsusStudent("John Doe 1", 1, "1Somewhere", "916-555-1211", "johndoe1@somewhere.com");
CsusStudent instance2 = new CsusStudent("John Doe 2", 2, "2Somewhere", "916-555-1212", "johndoe2@somewhere.com");
CsusStudent instance3 = new CsusStudent("John Doe 3", 3, "3Somewhere", "916-555-1213", "johndoe3@somewhere.com");
CsusStudent instance4 = new CsusStudent("John Doe 4", 4, "4Somewhere", "916-555-1214", "johndoe4@somewhere.com");
CsusStudent instance5 = new CsusStudent("John Doe 5", 5, "5Somewhere", "916-555-1215", "johndoe5@somewhere.com");
CsusStudent instance6 = new CsusStudent("John Doe 6", 6, "6Somewhere", "916-555-1216", "johndoe6@somewhere.com");
CsusStudent instance7 = new CsusStudent("John Doe 7", 7, "7Somewhere", "916-555-1217", "johndoe7@somewhere.com");
CsusStudent instance8 = new CsusStudent("John Doe 8", 8, "8Somewhere", "916-555-1218", "johndoe8@somewhere.com");
CsusStudent instance9 = new CsusStudent("John Doe 9", 9, "9Somewhere", "916-555-1219", "johndoe9@somewhere.com");
// begin adding instance1 to the list
lst.addFirst(instance1);
// test forward and backward for single entry
lst.resetCurrent();
System.out.println((CsusStudent)lst.currentData());
lst.forward();
System.out.println((CsusStudent)lst.currentData());
lst.backward();
System.out.println((CsusStudent)lst.currentData());
// now add instance2 and instance3 via addFirst and instance4,instance5, instance6 via addLast
lst.addFirst(instance2);
lst.addFirst(instance3);
lst.addLast(instance4);
lst.addLast(instance5);
lst.addLast(instance6);
System.out.println(lst);
// move current forward a few times
lst.forward();
lst.forward();
lst.forward();
System,out,println((CsusStudent)lst.currentData());
// insert instance 9 after
lst.insertAfter(instance9);
// remove instance9
lst.remove(instance9);
// print out the first and last entries
System.out.println("Show the first entry and last entry:");
System.out.println((CsusStudent)lst.getFirst());
System.out.println((CsusStudent)lst.getLast());
// print out the whole list
System.out.println("Show the whole list:");
System.out.println(lst);
// remove entries starting from the last entry
int s = lst.getSize();
for(int i = 0;i<s;i++){
lst.removeFirst();
}
System.out.println("Check for the content of the emptied list");
if(lst.isEmpty()){
System.out.println("List ia empty");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.