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
What are some applications of computer in public administration?​
Volgvan

Answer:

Explanation:

Embedded Systems.

Windows applications (also called 'Desktop applications')

Web Applications.

Web Services.

Console applications.

6 0
2 years ago
Write a application that can determine if a 5 digit number you input is a palindrome. If the number is a palindrome then print "
notsponge [240]

Answer:

Written in Python:

num = int(input("Number: "))

strnum = str(num)

if len(strnum) !=5:

    print("Length must be 5")

else:

    if(strnum[::-1] == strnum):

         print("The number is a palindrome.")

    else:

         print("The number is NOT palindrome.")

Explanation:

This prompts user for input

num = int(input("Number: "))

This converts user input to string

strnum = str(num)

This checks the length of the string

if len(strnum) !=5:

    print("Length must be 5") If length is not 5, this prompt is printed

else:If otherwise

    if(strnum[::-1] == strnum): The compares the string reversed with the original

         print("The number is a palindrome.") If both are the same, this line is executed

    else:

         print("The number is NOT palindrome.") If otherwise, this line is executed

4 0
3 years ago
A student wants an algorithm to find the hardest spelling word in a list of vocabulary. They define hardest by the longest word.
makkiz [27]

Answer:

Initialize the “longest word” by an empty string and update it when a longer word is found

Explanation:

import java.util.stream.Stream;

public static String findLongest(String[] spellingList) {

   return Stream.of(spellingList).reduce("", (longestWord, word) -> (

       longestWord.length() < word.length() ? word : longestWord

   ));

}

5 0
3 years ago
Which represents the hierarchical structure of a Google Analytics account from top to bottom?
zlopas [31]

Answer: (D) Account > Property > View

Explanation:

 The Account > Property > View is the hierarchical structure of the google analytics account and it this hierarchical stricture basically represent from top to the bottom. The google account basically contain two and more properties for structuring the efficient accounts.

The property of the reporting view contain unique ID and it is the combination of digits and account ID. The view property in the analytic account basically used for access, analyzing and customize the data for viewing in proper way.

8 0
2 years ago
Is it possible for a PowerPoint user to add notes to slides and see the added comments
Arturiano [62]

Answer:

I am not for sure on this, but yes I think it is possible for the PowerPoint user to add notes to slides and see the added comments. If it isn't possible I know for a fact on Google slides it is possible.

:

hope this helps! :)

7 0
3 years ago
Other questions:
  • which of the following are used on cable trays to protect the cable insulation from direct sunlight? a. barrier strips b. solid
    5·1 answer
  • For a color display using 8 bits for each of the primary colors (red, green, blue) per pixel, what should be the minimum size in
    6·1 answer
  • A block style business letter is:
    5·1 answer
  • Two powerboats are about to cross paths. what should the boat on the starboard (right) do?
    11·1 answer
  • The person in charge of recording the sound should always
    15·1 answer
  • Count positive and negative number and compute the average. The program will have the user input an unspecified number of intege
    6·1 answer
  • Using the Vigenere cipher, does the length of the key matter?
    8·1 answer
  • Which of the following statements about the relationship between hardware and software is true? a) Hardware can be present in in
    9·1 answer
  • Why does computer uses 0s and 1s to procress data​
    7·2 answers
  • Que es una red de datos
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!