1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
valentina_108 [34]
2 years ago
8

Write a method reverse( ) for OurLinkedList class. The method should return a new OurLinkedList object that is the reverse of th

e 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
Computers and Technology
1 answer:
Salsk061 [2.6K]2 years ago
8 0

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());

}

}

You might be interested in
This is not a feature provided by most GUIS.<br> icons<br> windows<br> forms<br> menus
Murrr4er [49]
Maybe menus I’m not sure but good luck
5 0
2 years ago
Read 2 more answers
What is the function of the NOS? Select all that apply.
Zigmanuir [339]

Answer:

.network management

Explanation:

pls need brainliest

3 0
3 years ago
Read 2 more answers
Create the code that will find
frez [133]

Answer:

function findLongestWord(str) {

 var longestWord = str.split(' ').sort(function(a, b) { return b.length - a.length; });

 return longestWord[0].length;

}

findLongestWord(InputHere);

Explanation:

Replace InputHere with the input

7 0
3 years ago
All of the following are examples of what rigorous coursework in high school might mean except: A) added reading assignments. B)
faust18 [17]

Answer:b

Explanation:

3 0
3 years ago
Read 2 more answers
Which of the following is part of the 6-by-6 rule?<br><br> i need help!!!!!
Digiron [165]
No more than six lines, and no more than six words in a 6 by 6 rule.
5 0
2 years ago
Other questions:
  • If you want to boot from a hard drive what must it have
    6·1 answer
  • You manage an NLB cluster composed of three servers: Server1, Server2 and Server3. Your maintenance schedule indicates that Serv
    15·1 answer
  • Advantage and disavantage of malware maintenance
    12·1 answer
  • Does anyone have the GCSE 2018 Design Technology J310/01 practice paper?
    15·1 answer
  • What necessarily happens when a photographer chooses to give either the aperture or the shutter speed priority?
    10·1 answer
  • HELP MEEE PLEASE!!!
    11·1 answer
  • Phân tích 5 phương hướng nhiệm vụ phát triển nông -lâm -ngư nghiệp ở nước ta hiện nay
    11·1 answer
  • A multinational organization that offers web-based services has datacenters that are located only in the United State; however,
    6·1 answer
  • Task 2
    6·1 answer
  • What dictionary operation can you use to remove all the keys within the dictionary-named contacts?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!