This is a Java Assigment Linked List workout Your job in this assignment is to c
ID: 3831225 • Letter: T
Question
This is a Java Assigment
Linked List workout
Your job in this assignment is to create a generic linked list. The linked list should have the ability to perform the following actions:
check if the list is empty
check the size of the list
add data to the list
add data to a specific (valid) location in the list
remove an item from the list based on position
remove an item from the list based on data value
removes all elements from the list
gets an item from the list based on the position
outputs the contents of the list
Use the dat file from the previous assignment (Generic Quicksort) to demonstrate effectiveness of the linked list you created.
the file is names.dat and it contains names
MARY PATRICIA LINDA BARBARA ELIZABETH JENNIFER MARIA SUSAN MARGARET DOROTHY LISA NANCY KAREN BETTY HELEN SANDRA DONNA CAROL RUTH SHARON MICHELLE LAURA SARAH KIMBERLY DEBORAH JESSICA SHIRLEY CYNTHIA ANGELA MELISSA BRENDA AMY ANNA REBECCA VIRGINIA KATHLEEN PAMELA MARTHA DEBRA AMANDA STEPHANIE CAROLYN CHRISTINE MARIE JANET CATHERINE FRANCES ANN JOYCE DIANE ALICE JULIE HEATHER TERESA DORIS GLORIA EVELYN JEAN CHERYL MILDRED KATHERINE JOAN ASHLEY JUDITH ROSE
Explanation / Answer
import java.util.*;
import java.io.*;
class Node<T>
{
public T data;
public Node<T> next;
public Node(){}
}
class List<T>
{
private Node<T> head;
public List(){
head = null;
}
public boolean isEmpty(){
return (head == null);
}
public int getSize(){
int size = 0;
Node<T> temp = head;
while(temp != null){
size++;
temp = temp.next;
}
return size;
}
public void display(){
Node<T> temp = head;
while(temp != null){
System.out.println(temp.data);
temp = temp.next;
}
}
public void add(T info){
if(head == null){
head = new Node<T>();
head.data = info;
head.next = null;
}
else{
Node<T> temp = new Node<T>();
temp.data = info;
temp.next = null;
Node<T> itr = head;
while(itr.next != null){
itr = itr.next;
}
itr.next = temp;
}
}
public void add(T info , int pos){
int i = 1 ,size = getSize();
if((pos < 1) || (pos > size)){
System.out.println(pos + ":" + " Invalid location");
return;
}
Node<T> temp = new Node<T>();
temp.data = info;
temp.next = null;
Node<T> front = head, back = head;
while(i != pos){
back = front;
front = front.next;
i++;
}
if(front == head){
temp.next = head;
head = temp;
}
else{
back.next = temp;
temp.next = front;
}
}
public void removeAt(int pos){
int i = 1 ,size = getSize();
if((pos < 1) || (pos > size)){
System.out.println(pos + ":" + " Invalid location");
return;
}
Node<T> front = head, back = head ,temp = null;
while(i != pos){
back = front;
front = front.next;
i++;
}
if(front == head){
temp = head;
head = head.next;
temp.next = null;
}
else{
back.next = front.next;
front.next = null;
}
}
public void removeVal(T val){
boolean ret = false;
Node<T> front = head, back = head ,temp = null;
while(front != null){
try{
ret = front.data.equals(val);
}catch(Exception e){
System.out.println(e);
}
if(ret == true){
break;
}
else{
back = front;
front = front.next;
}
}
if(front == null){
System.out.println( val + " : No such value found");
return;
}
else{
back.next = front.next;
front.next = null;
}
}
public void removeAll(){
if(head == null)
return;
Node<T> temp = head;
while(head != null){
temp = head;
head = head.next;
temp.next = null;
}
head = null;
}
public T getItem(int pos){
int i = 1 ,size = getSize();
if((pos < 1) || (pos > size)){
System.out.println(pos + ":" + " Invalid location");
return null;
}
Node<T> temp = head;
while(i != pos){
temp = temp.next;
i++;
}
return temp.data;
}
}
public class listDriver
{
public static void main(String []args){
File file = new File("names.dat");
Scanner sc = null;
try{
sc = new Scanner(file);
}
catch(Exception e){
System.out.println(e);
}
List<String> list = new List<String>();
sc.useDelimiter(" ");
while(sc.hasNext()){
list.add(sc.next());
}
list.add("Ashish",2);
list.display();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.