import java.util.NoSuchElementException;     class LinkedList {             private Node head;             private Node tail;               private class Node {                 private int value;                 private Node next;                   public Node(int value) {                     this.value = value;                 }             }               private boolean isEmpty() {                 return (head == null);             }             private boolean hasNext(Node node) {                 return (node.next != null);             }               public void print() {                 Node current = head;                 System.out.print("[");                   while (current != null) {                     if (hasNext(current)) {                         System.out.print(current.value + "' ");                     } else {                         System.out.print(current.value);                     }                     current = current.next;                 } System.out.println("]");}                              public void addFirst(int value) {                     Node node = new Node(value);                     if (isEmpty()) {                         head = tail = node;                     } else {                         node.next = head;                         head = node;                     }                 }                 public void addLast(int value) {                     Node node = new Node(value);                     if (isEmpty()) {                         head = tail = node;                     } else {                         tail.next = node;                         tail = node;                     }                 }                 public int indexOf(int value) {                     int i = 0;                     Node current = head;                     while (current != null) {                         if (current.value == value) {                             return i;                         }                         i++;                         current = current.next;                     }                     return -1;                 }                 public boolean contains(int value) {                     return (indexOf(value) != -1);                 }                 public void deleteFirst() throws NoSuchElementException {                     if (isEmpty()) {                         throw new NoSuchElementException();                     }                     if (head == tail) {                         head = tail = null;                         return;                     } Node formerHeadNext = head.next;                     head.next = null;                     head = formerHeadNext;                 }                 public Node previous(Node node) throws NoSuchElementException {                     if (isEmpty())                         throw new NoSuchElementException();                     Node current = head;                     while (current.next != node) {                         if (!hasNext(current)) {                             throw new NoSuchElementException();                         }                         current = current.next;                     }                     return current;                 }                 public void deleteLast() throws NoSuchElementException {                     if (isEmpty())                         throw new NoSuchElementException();                     if (head == tail) {                         head = tail = null;                         return;                     }                     Node lastButOne = previous(tail);                     lastButOne.next = null;                     tail = lastButOne;                 }             }     public class LinkedListFromScratch {     public static void main(String[] args) {         LinkedList myList = new LinkedList();         myList.addLast(2);         myList.addLast(4);         myList.addLast(8);         myList.addFirst(-2);         myList.addFirst(-4);         myList.addLast(9);         myList.print();         System.out.println(myList.indexOf(4));         System.out.println(myList.contains(9));         for (int i = 0; i < 6; ++i) {             myList.deleteLast();             myList.print();         }              } }   In this linked list Add in an O(n) method that reverse to the class LinkedList created in the problem 1. This method must reverse the order of the nodes in the list. For example, having applied reverse to the list [2 → 4 → 8 → 9 → 8], we will change it to [8 → 9 → 8 → 4 → 2].

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

import java.util.NoSuchElementException;

 

 

class LinkedList {

            private Node head;

            private Node tail;

 

            private class Node {

                private int value;

                private Node next;

 

                public Node(int value) {

                    this.value = value;

                }

            }

 

            private boolean isEmpty() {

                return (head == null);

            }

            private boolean hasNext(Node node) {

                return (node.next != null);

            }

 

            public void print() {

                Node current = head;

                System.out.print("[");

 

                while (current != null) {

                    if (hasNext(current)) {

                        System.out.print(current.value + "' ");

                    } else {

                        System.out.print(current.value);

                    }

                    current = current.next;

                }


System.out.println("]");}

            

                public void addFirst(int value) {

                    Node node = new Node(value);

                    if (isEmpty()) {

                        head = tail = node;

                    } else {

                        node.next = head;

                        head = node;

                    }

                }

                public void addLast(int value) {

                    Node node = new Node(value);

                    if (isEmpty()) {

                        head = tail = node;

                    } else {

                        tail.next = node;

                        tail = node;

                    }

                }

                public int indexOf(int value) {

                    int i = 0;

                    Node current = head;

                    while (current != null) {

                        if (current.value == value) {

                            return i;

                        }

                        i++;

                        current = current.next;

                    }

                    return -1;

                }

                public boolean contains(int value) {

                    return (indexOf(value) != -1);

                }

                public void deleteFirst() throws NoSuchElementException {

                    if (isEmpty()) {

                        throw new NoSuchElementException();

                    }

                    if (head == tail) {

                        head = tail = null;

                        return;

                    }

Node formerHeadNext = head.next;

                    head.next = null;

                    head = formerHeadNext;

                }

                public Node previous(Node node) throws NoSuchElementException {

                    if (isEmpty())

                        throw new NoSuchElementException();

                    Node current = head;

                    while (current.next != node) {

                        if (!hasNext(current)) {

                            throw new NoSuchElementException();

                        }

                        current = current.next;

                    }

                    return current;

                }

                public void deleteLast() throws NoSuchElementException {

                    if (isEmpty())

                        throw new NoSuchElementException();

                    if (head == tail) {

                        head = tail = null;

                        return;

                    }

                    Node lastButOne = previous(tail);

                    lastButOne.next = null;

                    tail = lastButOne;

                }

            }

 

 

public class LinkedListFromScratch {

    public static void main(String[] args) {

        LinkedList myList = new LinkedList();

        myList.addLast(2);

        myList.addLast(4);

        myList.addLast(8);

        myList.addFirst(-2);

        myList.addFirst(-4);

        myList.addLast(9);

        myList.print();

        System.out.println(myList.indexOf(4));

        System.out.println(myList.contains(9));

        for (int i = 0; i < 6; ++i) {

            myList.deleteLast();

            myList.print();

        }

        

    }

}

 

In this linked list Add in an O(n) method that reverse to the class LinkedList created in the problem
1. This method must reverse the order of the nodes in the list. For
example, having applied reverse to the list [2 → 4 → 8 → 9 → 8], we
will change it to [8 → 9 → 8 → 4 → 2]. 

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Avoiding deadlock
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education