# It is time for you to demonstrate your skills in a project of your own choice. You must DESIGN, ANALYSE AND CODE any method for the GENERIC MyLinkedList class that will manipulate the linked list. You can decide yourself what it should be following the specification below: 1. Purpose: The method must make logical sense – it should be of some purpose to somebody. You should describe in the text who will use the method for which purpose. 2. Clearly explain the problem. Then clearly explain how your method will solve it. 3. Test program: Test the method using a wrapped class like Integer

40 0

Get full Expert solution in seconds

\$1.97 ONLY

``````public class MyLinkedList<E> {
private Node<E> head, tail;

tail = null;
}

/** Return the head element in the list */
public E getFirst() {
if (head == null) {
return null;
}
else {
}
}

/** Return the last element in the list */
public E getLast() {
return null;
}
else {
return tail.element;
}
}

/** Add an element to the beginning of the list */
public void prepend(E e) {
Node<E> newNode = new Node<>(e); // Create a new node
newNode.next = head; // link the new node with the head
head = newNode; // head points to the new node

if (tail == null) // the new node is the only node in list
}

/** Add an element to the end of the list */
public void append(E item) {

Node<E> newNode = new Node<>(item); // Create a new for element e

if (head == null) {
head = tail = newNode; // The new node is the only node in list
}
else {
tail.next = newNode; // Link the new with the last node
tail = newNode; // tail pointing to the last node
}
}

/** Remove the head node and
* return the object that is contained in the removed node. */
public E removeFirst() {
if (head == null) {
return null;
}
else {
E temp = head.element;
if (head == null) {
tail = null;
}
return temp;
}
}

public boolean delete(E item)
{
Node<E> ptr = head;
Node<E> prvPtr = null;
while (ptr!= null&& ((Comparable)ptr.element).compareTo(item)!= 0)
{
prvPtr=ptr;
ptr=ptr.next;
}
return false;
if (ptr==head) // item is first element
else // general case
prvPtr.next=ptr.next;
if (ptr==tail)// last element
tail=prvPtr;
return true;
}

public String toString() {
String result = "[";

Node<E> ptr = head;
for (ptr= head;ptr!=null; ptr=ptr.next)
{
result = result + ptr.element.toString();
if (ptr.next != null)
result = result + ","; // add commas but not to the final 1
}
result += "]";
return result;
}

public void clear() {
head = tail = null;
}

private static class Node<E> {
E element;
Node<E> next;

public Node(E element) {
this.element = element;
next = null;
}
}
/*
* How this method will work  ?

Iterate through the linked list. In loop, do following.
Before changing next of current,
store next node
next = curr->next
Now change next of current
This is where actual reversing happens
curr->next = prev
Move prev and curr one step forward
prev = curr
curr = next

*
*/
// this will reverse the linked list
public Node<E> reverse() {
Node<E> prev = null;
Node<E> current = head;
Node<E> next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}