Write a method reverse( ) for OurLinkedList class. The method should return a new OurLinkedList object that is the reverse of the original. You may only assume the OurLinkedList class has only the following methods: addFront(E e) add(E e) isEmpty( ) : boolean remove( ) : E size( ) : int the no argument constructor

Respuesta :

Answer:

See explaination

Explanation:

class OurLinkedList<E> { // The head is the first node of the LinkedList private Node<E> head; /** * No argument constructor, as mentioned in question to initialize the head pointer with null */ public OurLinkedList() { head=null; } public void addFront(E e) { if(head==null) { head=new Node<E>(e);//forgot <E> } else { Node<E> temp=head; Node<E> n=new Node<E>(e); //forgot <E> n.setNext(temp); head=n; } } /* * The add(E data) method add the node to the end of the linked List, It first checks if the Linked List is empty or not >> If the * Linked list is not empty then, * the temp pointer traverses to the end of the linked List and inserts a new Node with data that is * passes as argument at the end of the linked List * */ public void add(E data) { if(head == null) { return; } Node<E> temp = head; Node<E> nodeToAdd = new Node<>(data); /* Moving temp pointer to end of the linked list where next pointer is null */ while(temp.getNext() != null) { temp.setNext(temp.getNext()); } // adding nodeToadd to the end of the linked List temp.setNext(nodeToAdd); } /** * This method checks if the head of the Linked List is null it returns TRUE otherwise it return FALSE */ public boolean isEmpty() { if(head==null) { return true; } return false; } /** * the method remove, removes the first node of the list. If the list is empty is does nothing, if the List is not empty * then it removes the first element of the linked List. */ public void remove() { if (!isEmpty()) { Node<E> toRemoved = head; head = head.getNext(); toRemoved.setNext(null); } } /** * the size() method returns the size of the Linked List, * this method calculates the size of the linkedList by traversing through each node and incrementing the * length variable value */ public int size() { int length = 0; Node<E> current = head; while (current != null) { current = current.getNext(); length++; } return length; } public String toString() { if(isEmpty()) { return "head ==> null"; } Node<E> temp=head; String s="head ==> "; while(temp!=null) { s+=temp.getData()+" ==> "; temp=temp.getLink(); } s+=" null"; return s; } /* method to reverse the Linked List */ Node<E> reverse() { Node<E> prev = null; Node<E> current = head; Node<E> next = null; while (current != null) { next = current.getNext(); current.setNext(prev); prev = current; current = next; } head = prev; return head; } }

Node.java

/* this file contains the Implementation of Node class, * Node class contains if pointer of Node type which points to next Node * and a variable to hold the data */ public class Node<E> { private E data; private Node<E> next; public Node(E data) { this.data = data; next = null; } public void setNext(Node<E> next) { this.next=next; } public Node<E> getLink() { return next; } public E getData() { return data; } public void setData(E data) { this.data = data; } public Node<E> getNext() { return next; } }

Main.java <Contains the drive code for the program>

/* This class is the driver class of the program, this file contains the * code which checks the working of other components of the program * */ public class Main { public static void main(String[] args) { OurLinkedList<Integer> llist=new OurLinkedList<Integer>(); // checking if linked list id empty System.out.println("is empty: "+llist.isEmpty()); // after adding first element to list llist.addFront(10); System.out.println("is empty: "+llist.isEmpty()); // adding more elements to th elist llist.addFront(20); llist.addFront(30); llist.addFront(40); // printing the list using toString() method System.out.println("\n\nBefore Reversing the list:\n"+llist.toString()); // calling the reverse method llist.reverse(); System.out.println("\n\nAfter Reversing the list:\n"+llist.toString());

}

}