Question

Solve the following LinkedList problems. You can use the LinkedList class from the previous lab. Write the following methods for the LinkedList class Problem 1: Write a method that deletes the first half of the list. Problem 2: Write a method that deletes the second half of the list. Problem 3: Write a method that deletes every other element in the list. Problem 4: Write a method that, given a second LinkedList, creates and returns a third list containing all elements from the first two list. Problem 5: Write a method that, given a second LinkedL ist, creates and returns a third list containing only those elements that the first two lists have if common.

media%2F98c%2F98c4c7f5-67cb-4a04-94ea-d8

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public void deleteHalf() {

if(this.head == null)

return;

int size = this.size();

int i=0;

Node temp = head;

while(i < size/2) {

temp = temp.link;

i++;

}

this.head = temp.link;

}

public void deleteSecondHalf() {

if(this.head == null)

return;

int size = this.size();

int i=0;

Node temp = head;

while(i < size/2) {

temp = temp.link;

i++;

}

temp.link = null;

}

public void deleteElement() {

this.head = null;

}

public LinkedList<E> allElements(LinkedList<E> list2) {

LinkedList<E> thirdList = new LinkedList<E>();

if(this.head == null && list2.head == null) {

thirdList = null;

}

if(list2.head == null) {

thirdList.head = this.head;

}

else if(this.head == null)

thirdList.head = list2.head;

else {

Node temp = this.head;

while(temp.link != null) {

temp = temp.link;

}

temp.link = list2.head;

thirdList.head = this.head;

}

return thirdList;

}

Add a comment
Know the answer?
Add Answer to:
Solve the following LinkedList problems. You can use the LinkedList class from the previous lab. Write...
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
  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • JAVA - Write a function in your Main class LinkedList<E> mergeLists(LinkedList<E> list1, LinkedList<E> list2) - given...

    JAVA - Write a function in your Main class LinkedList<E> mergeLists(LinkedList<E> list1, LinkedList<E> list2) - given two ordered linked lists of integers, merge the two lists into a single LinkedList whose elements are in sorted order. You should create a new LinkedList and add the values from list1 and list2 into the new list in sorted order. Do not modify list1 or list 2. list1 => 1 -> 3 -> 7 -> 8, list2 => 2 -> 5 -> 7...

  • if we have following List classes: public class LinkedList<T> { class ListNode { protected T value;...

    if we have following List classes: public class LinkedList<T> { class ListNode { protected T value; protected ListNode next; public ListNode(T val, ListNode nxt) { value = val; next = nxt; } public ListNode(T val) { this(val, null); } public ListNode() { this(null, null); } } can you write the folowing methods in java: 1.Write a method for the LinkedList class called int indexOf(T val) which returns the integer index indicating the location of val in the list, or -1...

  • JAVA Write Java code for extending the LinkedList class of java.util.* to ExtLinkedList that would include...

    JAVA Write Java code for extending the LinkedList class of java.util.* to ExtLinkedList that would include the following method: public ExtLinkedList firstHalfList() which returns the first half of the list. In other words, if you have a ExtLinkedList of size 5, for example, the method firstHalfList should return the list with the first two nodes. If it is a list of size 6, it should return the list with the first three nodes. The original list should not be modified....

  • Add the following methods to the ArrayList class that we wrote during lecture. You may call...

    Add the following methods to the ArrayList class that we wrote during lecture. You may call the existing methods in ArrayList if you want, but do not use anything from the built-in java.util.ArrayList class. 1. (3 pts) Write a new method named addAll(ArrayList anotherList) that adds all the elements in anotherList to the back of the calling list. Be sure to reallocate the data array if necessary. anotherList should not be modified. 2. (4 pts) Write a new method named...

  • You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must...

    You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must include all the following described methods. Each of these methods should be public and static.Write a method named getFirst, that takes an Array of int as an argument and returns the value of the first element of the array. NO array lists. Write a method named getLast, that takes an Array of int as an argument and returns the value of the last element...

  • Exercise 3: Work exercise 11.4, 11.12, and 11.14 into one program (to develop 3 methods for...

    Exercise 3: Work exercise 11.4, 11.12, and 11.14 into one program (to develop 3 methods for ArrayList: first method finds the maximum element in an array list; the second method computes the sum of all elements in an array list; and the third method combines (union) two lists by adding the second list to the first list). Use Integer type for all methods. See problem statements for methods signatures and sample test data. Write one separate test program to test...

  • Implement a c++ 'List' class to handle a list with general operations. That means you can...

    Implement a c++ 'List' class to handle a list with general operations. That means you can insert and delete any element anywhere in the list. The list has no order, except for the order you insert or delete.   The methods you are to implement are as given: Constructor methods (two, default and copy constructor, a list to a newly defined list, ie 'List listA(listB)' ) empty returns true or false if list is empty or not. front makes current position...

  • create a class named IntegerQueue given a singlylinkedqueue of integers, write the following methods: max(SinglyLinkedQueue<Integer> s)...

    create a class named IntegerQueue given a singlylinkedqueue of integers, write the following methods: max(SinglyLinkedQueue<Integer> s) to return the max element in the queu. min(SinglyLinkedQueue<Integer> s) to return the min element in the queue. sum(SinglyLInkedQueue<Integer> s) to return the sum of elements in the queu. median(SinglyLinkedQueue<Integer> s) to return the median of elements in the queue. split(SinglyLinkedQueue<Integer> s) to separate the SinglyLinkedQueue into two ArrayQueues based on whether the element values are even or odd. package Stack_and_Queue; import java.util.Iterator; import...

  • In Java You may add any classes or methods to the following as you see fit in order to complete t...

    In Java You may add any classes or methods to the following as you see fit in order to complete the given tasks. Modify the LinkedList (or DoubleLinkedList) class and add a method append. append should take another LinkedList (DoubleLinkedList) as input and append that list to the end of this list. The append method should work by doing a few "arrow" adjustments on the boxes and it should not loop through the input list to add elements one at...

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