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
-Another question involving microsoft word-
Alexus [3.1K]
I'm PRETTY, certain it's C.
5 0
3 years ago
Which statement describes a characteristic of SRAM in a PC?
svlad2 [7]

It is used for cache memory.

Further Explanation:

Cache memory is the memory that stores the instructions and data that is used repeatedly. If the processor wants some data it first looks into cache memory then the processor doesn't has to look into the main memory for the data or instructions which is more time consuming. As the SRAM uses small but a constant source of power, it is usually used for making cache memories. Moreover it is benficial to keep the previously used data in SRAM(Which is faster) than acessing DRAM (Which is way more slow than SRAM.

  • Learn more about RAM in brainly.com/question/10602589
  • brainly.com/question/5159824
  • brainly.com/question/6062548

#LearnwithBrainly

8 0
3 years ago
Among the rights you have as a user of computing resources is the right to​ _______.
Ivanshal [37]
Protection from​ viruses, worms, and other threats
3 0
4 years ago
Read 2 more answers
What OBD-II term applies when all enabling standards for a specific diagnostic trouble code (DTC) are met?
mash [69]

Answer:

Option C i.e., Trip criteria is correct

Explanation:

The concept Trip criteria are described that when the allowing requirements for its diagnostic code are reached.

In other words, Trip seems to be a key-on method where all the allowing requirements for such a specific diagnostic display are fulfilled as well as the diagnostic monitoring is powered. It is finished once the ignition switch becomes switched off.

  • Option A and Option B are not correct because they are the engines that come under the performance ignition system and they are not related to the following scenario.
  • Option D is incorrect because it is the type of data mining that is not related to the scenario.
3 0
4 years ago
How to use discord for iPad?
Viktor [21]

Answer:

Download it

Explanation:

Just download it

6 0
2 years ago
Other questions:
  • Tool such as microsoft’s ____ are helping to bridge different platforms and programming languages.
    6·1 answer
  • Which recovery method usually involves an evasion effort and is dependent on the ip's condition?
    11·1 answer
  • If you need to provide a storage server with fault tolerance through the use of multiple paths between the server and the actual
    14·1 answer
  • What are the similarities between data mining and data analytics
    11·1 answer
  • Juan has performed a search on his inbox and would like to ensure the results only include those items with attachments which co
    14·2 answers
  • Type the correct answer in the box. Spell all words correctly.
    12·1 answer
  • The term callee save denotes the case where the __________ (calling / called) program saves the contents of registers whose cont
    10·1 answer
  • The negotiation process involves several steps such as: preparing for negotiation; knowing your walk-away point; and working tow
    14·1 answer
  • It is important to consider the quality of the data being used when considering the accuracy of a conclusion. Incorrectly collec
    11·2 answers
  • You are given an integer N where 0 &lt;= N &lt;= 100, followed by another line of input which has a word W with length L where 1
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!