Question

In java Write a method public void printReverse() that prints the elements of a doubly linked...

In java

Write a method public void printReverse() that prints the elements of a doubly linked list in reverse.

Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting.

In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the list becomes empty. Make sure to print the list after each deletion.

For example, your list initially could be:

[ 3 1 2 5 8 7 ].

After deleting 5th element from the end:

[ 3 1 2 5 8 7 ] => [ 3 2 5 8 7 ].

After deleting 5th element again;

[ 3 2 5 8 7] => [ 2 5 8 7].

After deleting 5th element again (counting in the reverse direction, then moving forward),

[ 2 5 8 7] => [ 2 8 7 ]

[2 8 7] => [2 8]

//**************************** DLL.java *******************************
// generic doubly linked list class

public class DLL<T> {
private DLLNode<T> head, tail;
public DLL() {
head = tail = null;
}
public boolean isEmpty() {
return head == null;
}
public void setToNull() {
head = tail = null;
}
public T firstEl() {
if (head != null)
return head.info;
else return null;
}
public void addToHead(T el) {
if (head != null) {
head = new DLLNode<T>(el,head,null);
head.next.prev = head;
}
else head = tail = new DLLNode<T>(el);
}
public void addToTail(T el) {
if (tail != null) {
tail = new DLLNode<T>(el,null,tail);
tail.prev.next = tail;
}
else head = tail = new DLLNode<T>(el);
}
public T deleteFromHead() {
if (isEmpty())
return null;
T el = head.info;
if (head == tail) // if only one node on the list;
head = tail = null;
else { // if more than one node in the list;
head = head.next;
head.prev = null;
}
return el;
}
public T deleteFromTail() {
if (isEmpty())
return null;
T el = tail.info;
if (head == tail) // if only one node on the list;
head = tail = null;
else { // if more than one node in the list;
tail = tail.prev;
tail.next = null;
}
return el;
}
public void printAll() {
for (DLLNode<T> tmp = head; tmp != null; tmp = tmp.next)
System.out.print(tmp.info + " ");
}
public T find(T el) {
DLLNode<T> tmp;
for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next);
if (tmp == null)
return null;
else return tmp.info;
}
}

//**************************** DLLNode.java *******************************
// node of generic doubly linked list class

public class DLLNode<T> {
public T info;
public DLLNode<T> next, prev;
public DLLNode() {
next = null; prev = null;
}
public DLLNode(T el) {
info = el; next = null; prev = null;
}
public DLLNode(T el, DLLNode<T> n, DLLNode<T> p) {
info = el; next = n; prev = p;
}
}

************************DLLTest.java*********************************

public class DLLTest {
   public static void main(String[] args) {
       DLL<String> test = new DLL<String>();
       for(int i = 0; i < 5; i++)
           test.addToTail("a" + i);
       test.printAll();
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem including test program. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// DLL.java

public class DLL<T> {

    private DLLNode<T> head, tail;

    public DLL() {

         head = tail = null;

    }

    public boolean isEmpty() {

         return head == null;

    }

    public void setToNull() {

         head = tail = null;

    }

    public T firstEl() {

         if (head != null)

             return head.info;

         else

             return null;

    }

    public void addToHead(T el) {

         if (head != null) {

             head = new DLLNode<T>(el, head, null);

             head.next.prev = head;

         } else

             head = tail = new DLLNode<T>(el);

    }

    public void addToTail(T el) {

         if (tail != null) {

             tail = new DLLNode<T>(el, null, tail);

             tail.prev.next = tail;

         } else

             head = tail = new DLLNode<T>(el);

    }

    public T deleteFromHead() {

         if (isEmpty())

             return null;

         T el = head.info;

         if (head == tail) // if only one node on the list;

             head = tail = null;

         else { // if more than one node in the list;

             head = head.next;

             head.prev = null;

         }

         return el;

    }

    public T deleteFromTail() {

         if (isEmpty())

             return null;

         T el = tail.info;

         if (head == tail) // if only one node on the list;

             head = tail = null;

         else { // if more than one node in the list;

             tail = tail.prev;

             tail.next = null;

         }

         return el;

    }

    public void printAll() {

         for (DLLNode<T> tmp = head; tmp != null; tmp = tmp.next)

             System.out.print(tmp.info + " ");

         System.out.println();

    }

    public T find(T el) {

         DLLNode<T> tmp;

         for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next)

             ;

         if (tmp == null)

             return null;

         else

             return tmp.info;

    }

    // prints the elements of a doubly linked list in reverse

    public void printReverse() {

         // looping from last node to first, printing data values

         for (DLLNode<T> node = tail; node != null; node = node.prev) {

             System.out.print(node.info + " ");

         }

         System.out.println();

    }

    // deletes the 5th element from end of the list

    public void delete5FromTheEnd() {

         // if list is empty, simply exiting from the method

         if (isEmpty()) {

             return;

         }

         // if list contains only one element, removing it and returning from

         // method

         if (head.next == null) {

             deleteFromHead();

             return;

         }

         // flag indicating direction, currently moving left (tail to head)

         boolean left = true;

         // taking reference to tail

         DLLNode<T> node = tail;

         // looping for 4 times

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

             // checking direction

             if (left) {

                 // if there is a previous node, moving to previous otherwise

                 // starting from head.next and changing direction

                 if (node.prev == null) {

                      left = false;

                      node = head.next;

                 } else {

                      node = node.prev;

                 }

             } else {

                 // if there is a next node, moving to next otherwise

                 // starting from tail.prev and changing direction

                 if (node.next == null) {

                      node = tail.prev;

                      left = true;

                 } else {

                      node = node.next;

                 }

             }

         }

         // if node to be removed is head node, calling deleteFromHead

         if (node.prev == null) {

             deleteFromHead();

         }

         // if node to be removed is tail node, calling deleteFromTail

         else if (node.next == null) {

             deleteFromTail();

         } else {

             // otherwise disconnecting node and linking node.prev with node.next

             node.prev.next = node.next;

             node.next.prev = node.prev;

         }

    }

}

// Test.java

public class Test {

    public static void main(String[] args) {

         //creating a DLL

         DLL<Integer> dll = new DLL<Integer>();

         //adding 10 random numbers between 0 and 9

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

             dll.addToTail((int) (Math.random() * 10));

         }

         //printing

         dll.printAll();

        

         //looping until dll is empty

         while (!dll.isEmpty()) {

             //deleting 5th node from end

             dll.delete5FromTheEnd();

             //printing the updated DLL

             dll.printAll();

         }

    }

}

/*OUTPUT*/

3 4 5 8 8 7 5 0 4 2

3 4 5 8 8 5 0 4 2

3 4 5 8 5 0 4 2

3 4 5 5 0 4 2

3 4 5 0 4 2

3 5 0 4 2

5 0 4 2

5 4 2

5 4

5

Add a comment
Know the answer?
Add Answer to:
In java Write a method public void printReverse() that prints the elements of a doubly linked...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In java Write a method public void printReverse() that prints the elements of a doubly linked...

    In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...

  • Using Doubly Linked List, and Sorting methods: (In Java) (please attach your output with the answer)...

    Using Doubly Linked List, and Sorting methods: (In Java) (please attach your output with the answer) (Please answer if it is correct and working) (Have been getting many wrong and spam answers lately) Introduction: In this project, we use the same file structure of Artist and Art to extend the concepts of array and linked list and have them applied to sorting. The input files of p1arts.txt and p1artists.txt have been slightly modified. They are named p7arts.txt and p7artists.txt. Assignments:...

  • Using a doubly linked list as the underlying data structure, implement a list ADT that implements...

    Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...

  • cadn you fix this brpblem this the code of removing at the front of the Doubly...

    cadn you fix this brpblem this the code of removing at the front of the Doubly Linked List in java public void removeFromFront() {               if(isEmpty()) {                       throw new NoSuchElementException();        }               Node temp = head;        if(head == tail) {                       tail = null;        }        else        {            head.next.prev = null;...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • In the first exercise, you will override the add method in a subclass in order to...

    In the first exercise, you will override the add method in a subclass in order to improve its performance. In the second exercise, you will implement an Iterator for your new linked list class. Exercise 1 The add method of the linked list class discussed during lecture performs in O(N) time. What can be done to improve its performance to O(1)? What are the boundary cases? Define a new class that inherits from the CS20bLinkedList class introduced during the lecture....

  • LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete...

    LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...

  • Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how...

    Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT