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();
}
}
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
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 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...
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:...
C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes: -head: first node in the list -tail: last node in the list #include "circDLLNode.h" template class { public: //Default constructor: creates an empty list (); //Destructor: deallocate memory ~(); ...
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 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 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...
In C++, change this code to a circular linked list #include <iostream> using namespace std; struct node { string data; node *next; }; class linked_list { private: node *head,*tail; public: linked_list() { head = NULL; tail = NULL; } void add_node(string n) { node *tmp = new node; tmp->data = n; tmp->next = NULL; if(head == NULL) { head = tmp; tail = tmp; } else { tail->next = tmp; tail = tail->next; } } void display() { while (tmp...
(WRITE THIS PROGRAM IN C LANGUAGE).Before I ask the question, i'm just letting you know that I asked this question before but I unfortunately the answer was wrong, in fact, it didn't even answer my questions. So please make an effort to answer this question. In this question, we will learn about a variant of linked list called \doubly Linked List". In addition to the \next" pointer pointing to the next node in the list, a node in a doubly...
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,...