Question

CSCI-2467 Lab 11 – Refactor LinkedList Application to use Generics Background The code consists of three...

CSCI-2467 Lab 11 – Refactor LinkedList Application to use Generics

Background

The code consists of three files that implement and use a simple linked list. The code was written in early Java-style using the Object class in order to allow the linked list to be a list of objects of any type. While the code works, it is not type-safe. Refactor the code to use Java Generics. You will need to change the Main class to create a linked list of Strings in the list1() method and a linked list of Integers in the list2() method. You’ll then need to make appropriate modifications to the other two Java files so that they use Java Generics. Do NOT use the built-in Java LinkedList class.

package edu.cscc;
public class Main {
    public static void main(String[] args) {
        list1();
        list2();
    }

    // Use linked list with Strings
    public static void list1() {
        LinkedList llist = new LinkedList();
        llist.addFirst("one");
        llist.addLast("two");
        llist.addLast("three");
        llist.addFirst("zero");
        llist.addLast("xxxx");
        llist.addFirst("yyyyy");
        llist.deleateLast();
        llist.deleteFirst();
        System.out.println(llist);
        llist.deleteFirst();
        llist.deleateLast();
        llist.deleteFirst();
        llist.deleateLast();
        System.out.println(llist);
    }

    // Use linked list with Integers
    public static void list2() {
        LinkedList llist = new LinkedList();
        llist.addFirst(1);
        llist.addLast(2);
        llist.addLast(3);
        llist.addFirst(0);
        llist.addLast(142);
        llist.addFirst(-97);
        llist.deleateLast();
        llist.deleteFirst();
        System.out.println(llist);
        llist.deleteFirst();
        llist.deleateLast();
        llist.deleteFirst();
        llist.deleateLast();
        System.out.println(llist);
    }
}


}

package edu.cscc;
public class LinkedList {
    private Node head;

    public LinkedList() {
        head = null;
    }

    public void addFirst(Object content) {
        Node ptr = head;
        head = new Node(content,ptr);
    }

    public void addLast(Object content) {
        Node last = new Node(content,null);
        if (head == null) {
            head = last;
        } else {
            Node ptr = head;
            while(ptr.getNext() != null) {
                ptr = ptr.getNext();
            }
            ptr.setNext(last);
        }
    }

    public boolean deleteFirst() {
        if (head == null) {
            return false;
        } else {
            head = head.getNext();
            return true;
        }
    }

    public boolean deleateLast() {
        if (head == null) {
            return false;
        }
        else if (head.getNext() == null) {
            head = null;
            return true;
        } else {
            Node ptr = head;
            while (ptr.getNext().getNext() != null) {
                ptr = ptr.getNext();
            }
            ptr.setNext(null);
            return true;
        }
    }

    public Node getHead() {
        return head;
    }

    public String toString() {
        String str;
        if (head == null) {
            str = "<empty>";
        } else {
            Node ptr = head;
            str = "("+ptr.getContent().toString()+")";
            while(ptr.getNext() != null) {
                str = str + "->";
                ptr = ptr.getNext();
                str = str + "(" + ptr.getContent().toString() + ")";
            }
        }
        return str;
    }
}
packageedu.cscc;

public class Node {
    private Object content;
    private Node next;

    public Node(Object content, Node next) {
        this.content = content;
        this.next = next;
    }

    public Object getContent() {
        return content;
    }

    public void setContent(Object content) {
        this.content = content;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
// Node.java : Generic Node class representing the node of the linked list
packageedu.cscc;

public class Node<T> {
private T content;
private Node<T> next;

public Node(T content, Node<T> next) {
this.content = content;
this.next = next;
}

public T getContent() {
return content;
}

public void setContent(T content) {
this.content = content;
}

public Node<T> getNext() {
return next;
}

public void setNext(Node<T> next) {
this.next = next;
}
}
//end of Node.java

// LinkedList.java : Generic Linked List class to represent linked list of generic elements stored in Node

package edu.cscc;

public class LinkedList<T> {
private Node<T> head;

public LinkedList() {
head = null;
}

public void addFirst(T content) {
Node<T> ptr = head;
head = new Node<T>(content,ptr);
}

public void addLast(T content) {
Node<T> last = new Node<T>(content,null);
if (head == null) {
head = last;
} else {
Node<T> ptr = head;
while(ptr.getNext() != null) {
ptr = ptr.getNext();
}
ptr.setNext(last);
}
}

public boolean deleteFirst() {
if (head == null) {
return false;
} else {
head = head.getNext();
return true;
}
}

public boolean deleateLast() {
if (head == null) {
return false;
}
else if (head.getNext() == null) {
head = null;
return true;
} else {
Node<T> ptr = head;
while (ptr.getNext().getNext() != null) {
ptr = ptr.getNext();
}
ptr.setNext(null);
return true;
}
}

public Node<T> getHead() {
return head;
}

public String toString() {
String str;
if (head == null) {
str = "<empty>";
} else {
Node<T> ptr = head;
str = "("+ptr.getContent().toString()+")";
while(ptr.getNext() != null) {
str = str + "->";
ptr = ptr.getNext();
str = str + "(" + ptr.getContent().toString() + ")";
}
}
return str;
}
}
//end of LinkedList.java

// Main.java : Driver program to test the generic LinkedList class created using String and Integer types
package edu.cscc;

public class Main {

   public static void main(String[] args) {

       list1();
      list2();

   }
  
   // Use linked list with Strings
public static void list1() {
LinkedList<String> llist = new LinkedList<String>();
llist.addFirst("one");
llist.addLast("two");
llist.addLast("three");
llist.addFirst("zero");
llist.addLast("xxxx");
llist.addFirst("yyyyy");
llist.deleateLast();
llist.deleteFirst();
System.out.println(llist);
llist.deleteFirst();
llist.deleateLast();
llist.deleteFirst();
llist.deleateLast();
System.out.println(llist);
}
  
// Use linked list with Integers
public static void list2() {
LinkedList<Integer> llist = new LinkedList<Integer>();
llist.addFirst(1);
llist.addLast(2);
llist.addLast(3);
llist.addFirst(0);
llist.addLast(142);
llist.addFirst(-97);
llist.deleateLast();
llist.deleteFirst();
System.out.println(llist);
llist.deleteFirst();
llist.deleateLast();
llist.deleteFirst();
llist.deleateLast();
System.out.println(llist);
}

}

//end of Main.java

Output:

Add a comment
Know the answer?
Add Answer to:
CSCI-2467 Lab 11 – Refactor LinkedList Application to use Generics Background The code consists of three...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args )...

    //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args ) { Scanner in = new Scanner( System.in ); LinkedList teamList = new LinkedList(); final int TEAM_SIZE = Integer.valueOf(in.nextLine()); for (int i=0; i<TEAM_SIZE; i++) { String newTeamMember = in.nextLine(); teamList.append(newTeamMember); } while (in.hasNext()) { String removeMember = in.nextLine(); teamList.remove(removeMember); }    System.out.println("FINAL TEAM:"); System.out.println(teamList); in.close(); System.out.print("END OF OUTPUT"); } } =========================================================================================== //PoD import java.util.NoSuchElementException; /** * A listnked list is a sequence of nodes with...

  • Linkedlist implementation in C++ The below code I have written is almost done, I only need...

    Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() functio​n. ​ Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...

  • When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses...

    When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Any suggestions? public class LinkedList<T> {    Node<T> itsFirstNode;    Node<T> itsLastNode;    private int size;    public LinkedList() {        itsFirstNode = null;        itsLastNode = null;          size = 0;    }    public Iterator<T> getIterator() {        return new Iterator(this);    }    // THIS WILL NEED...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList()...

    P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList() { header = null; } public final Node Search(int key) { Node current = header; while (current != null && current.item != key) { current = current.link; } return current; } public final void Append(int newItem) { Node newNode = new Node(newItem); newNode.link = header; header = newNode; } public final Node Remove() { Node x = header; if (header != null) { header...

  • Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how...

    Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT