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]
3 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]3 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
Overview
ludmilkaskok [199]

Answer:

the answer is in the image below:

Explanation:

5 0
2 years ago
What does “human-like” mean when we are talking about a machine?
pav-90 [236]
When talking about a machine and describing it as “human-like” it is suggesting that it had human like characteristics for the inanimate object.
8 0
3 years ago
____ is a special type of large, integrated system that ties together all types of a company’s activities, such as planning, man
Rainbow [258]

Answer:i think is an intranet

Explanation:because that's where they save most of their things

7 0
2 years ago
Read 2 more answers
What is an administrator?
Drupady [299]
A person responsible for running a business, organization, etc.
3 0
2 years ago
The advancement in speed of transportation is attributed to invention of this device
morpeh [17]
The automobile is the invention
3 0
3 years ago
Other questions:
  • Susan bought a new sweater on sale for $28.93.she was charged HST of 13%.find the total amount of her bill including taxes.​
    9·1 answer
  • Create a new conditional format that applies yellow fill (fourth color under Standard Colors) and bold font to values that conta
    14·1 answer
  • HURRY
    5·1 answer
  • True or False. When used with the cout object, the endl stream manipulator advances the cursor to the next line on the computer
    6·1 answer
  • Write a Python program that reads the CSV file, compares the population estimates of every row for 2010 and 2017 and computes th
    10·1 answer
  • What will be the values of ans, x, and y after the following statements are executed? int ans = 35, x = 50, y =50; if ( x &gt;=
    7·2 answers
  • You are a database administrator. Chantelle comes to you asking for help on how to access all the data in one row of the databas
    11·1 answer
  • What is the FaFASA4caster used for
    10·1 answer
  • Edhesive 2.3 code practice question 1​
    11·1 answer
  • Please help ASAP! will mark brianliest! 30 points!
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!