Question

Java Generic Linked List Problem: How do I remove a slice of a linked list and...

Java Generic Linked List Problem:

How do I remove a slice of a linked list and add its data to another linked list in java?

Example:

Private<T> head;

Private int size;

Public List<T> slice(int from, int to) {

// This method will remove the data from the given range (inclusive) and add it to a new List and return this new list.

}

Homework Answers

Answer #1

Program:

//class GenericLinkedList
import java.util.LinkedList;
import java.util.List;

public class GenericLinkedList {

   public static void main(String[] args) {
      
       //create variable for LinkedList
       List<String> list = new LinkedList<String>();

       //add element to linkedlist
       list.add("Slice");
       list.add("Linked");
       list.add("LinkedList");
       list.add("Remove");
       list.add("Add");

       //print original list
       System.out.println("Original List : " + list);

       //create an object of Generics with list parameter
       Generics generics = new Generics(list);

       //print the sliced list from the given list by calling slice() method
       System.out.println("Sliced List : " + generics.slice(1, 4));

       //print the modified original list after slice operation
       System.out.println("Modified List after slice : " + generics.getList());

   }

}

//class Generics
import java.util.LinkedList;
import java.util.List;

//<T> is added to class to indicate class will support generics
public class Generics<T> {

   // variable to store generic list
   private List<T> list;

   // list parameterized constructor
   public Generics(List<T> list) {
       this.list = list;// initialize list
   }

   // definition of slice() method
   public <T> List<T> slice(int from, int to) {

       // create new linkedlist to store the slice list content
       List<T> sliceList = new LinkedList<T>();

       // iterate list over (from, to)
       for (int i = from; i < to; i++) {
           // add element to slicelist
           sliceList.add((T) this.list.get(i));
       }

       // iterate list to remove sliced elements
       for (int i = to - 1; i > from - 1; i--) {
           this.list.remove(i);
       }

       return sliceList;//slicelist contains elements with given range(inclusive) not exclusive
   }

   // getter method for list variable
   public List<T> getList() {
       return list;
   }

   // setter method for list variable
   public void setList(List<T> list) {
       this.list = list;
   }

}

Sample output of the program:

Original List : [Slice, Linked, LinkedList, Remove, Add]
Sliced List : [Linked, LinkedList, Remove]
Modified List after slice : [Slice, Add]

Know the answer?
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
Similar Questions
Given this definition of a generic Linked List node: public class LLNode {     private T...
Given this definition of a generic Linked List node: public class LLNode {     private T data;     private LLNode next;     public LLNode(T data, LLNode next) {           this.data = data;           this.next = next;     }     public void setNext(LLNode newNext){ next = newNext; }     public LLNode getNext(){ return next; }     public T getData() {return data;} } Write the findMinimumNode method body. This method returns the linked list node that contains the minimum value in the...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next reference of the tail node is set to refer back to the head of the list (rather than null) . Start from the SinglyLinkedList provided in the Lecture (not the built-in Java LinkedList class). 2. Starting from  SinglyLinkedList you will need to modify or complete methods: first(), last(), addFirst(), addLast(), removeFirst(), toString(), and also create a new rotate() method which will move the tail to...
How would I make a generic insertion sort for a doubly linked list in java? or...
How would I make a generic insertion sort for a doubly linked list in java? or is it even possible to make it generic in the first place?
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
Add a recursive Boolean function called bool isGreater(int compare) to class IntegerLinkedList to check if any...
Add a recursive Boolean function called bool isGreater(int compare) to class IntegerLinkedList to check if any of the linked list data values is greater than the given parameter, compare. For example, isGreater(25) should return true and isGreater(100) should return false for the the linked list shown below. A main function (prob3.cpp) is given to you to add data values to the linked list and test your function. Other examples are given in the main function. // ADD ANSWER TO THIS...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
IN JAVA Language- Singly Linked List Implementation Implement a Linked List in your language. Use your...
IN JAVA Language- Singly Linked List Implementation Implement a Linked List in your language. Use your Can class. You need to create a driver that makes several Can objects and places them in alphabetical order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. NOT USE your language's Library List . You will receive zero points. Write a LinkedList class. Include...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT