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

As it is written in the problem I have written both the public void printReverse() and public void delete5FromTheEnd() in the DLL class. Also, I created a linked list with random integer using Random class and integer range between 0 to 100. After that, I repeatedly called delete5FromTheEnd() until the list becomes empty.

In the below code I have bold all text that has been added by me in your given code.

Code:-

// 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;
}
public void printReverse()
{
DLLNode<T> temp=tail;
for (; temp!=null;temp=temp.prev)
System.out.print(temp.info + " ");
}
  
//Function created to delete 5th node from the last
public void delete5FromTheEnd()
{
//if no element in the list than nothing to delete
if(head==null || tail==null)
return;
  
//if linked list have only one element than setting head and tail to null after removing that element
if(head.equals(tail))
{
head=null;
tail=null;
return;
}
  
//storing end of the list so that we can start from counting from last
DLLNode<T> temp=tail;

//side store the direction in which we are moving it will start in rev
//and it changes when we reach first element to forward
String side="rev";
  
//counter to find out the 5th element of the list
int counter=1;
  
//loop runs until find out 5th element
while(counter!=5)
{
//counter increases for each node
counter=counter+1;
if(temp.equals(head))
{
//if we reach first element than we change direction of moving to forward
side="for";
}
else if(temp.equals(tail))
{
//if we reach last element than we change direction of moving to reverse
side="rev";
}
  
  
//if side is equal to rev we move backwards else move forwards
if(side.equals("rev"))
{
temp=temp.prev;
}
else
{
temp=temp.next;
}
}
  
//deleting the node by changing the value of the pointers
if(temp.equals(head))
{
deleteFromHead();
}
else if(temp.equals(tail))
{
deleteFromTail();
}
else
{
(temp.prev).next=temp.next;
(temp.next).prev=temp.prev;
}
}

}

**************************** 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******************************

import java.util.Random;
public class DLLTest
{
public static void main(String[] args)
{
//creating instance of random class
Random random=new Random();
  
//creating doubly linked list of integers
DLL<Integer> test = new DLL<Integer>();

//inserting 10 random generated integers in range 0 to 100
for(int i = 0; i < 10; i++)
test.addToTail(random.nextInt(100));
//printing linked list
System.out.println("Linked List:- ");
test.printAll();
System.out.println("");
System.out.println("Printing Linked list in reverse:- ");
test.printReverse();

System.out.println("");
System.out.println("Deleting 5th Element Until List become empty:-");
test.printAll();
System.out.println("");
while(!test.isEmpty())
{
test.delete5FromTheEnd();
test.printAll();
System.out.println("");
}

}
}

Output:-

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