Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write an add method for the following DLL, it would add the element to a specific position . Use the following code to test your

Write an add method for the following DLL, it would add the element to a specific position.

Use the following code to test your answer: 

// Should give you the result from zero to five in Test#1

// throw an exception if the position is out of bound, which happened in Test#2

Test#1 DLL d = new DLL<>(); d.add("TWO",0); d.add("FOUR",1); d.add("ZERO",0); d.add("ONE",1); d.add("THREE",3); d.print();

Test#2:

 d.add("TWO",0); d.add("FOUR",1); d.add("ZERO",0); d.add("ONE",1); d.add("THREE",6); d.print();

public class DLL < E > {

private static class Node < E > {

private E ele;

private Node < E > pre;

private Node < E > next;

public Node(E e) {

this(e, null, null);

}

public Node(E e, Node < E > p, Node < E > n) {

ele = e;

pre = p;

next = n;

}

public E getE() {

return ele;

}

public Node < E > getPre() {

return pre;

}

public Node < E > getNext() {

return next;

}

public void setE(E e) {

ele = e;

}

public void setPrev(Node < E > p) {

pre = p;

}

public void setNext(Node < E > n) {

next = n;

}

}

private Node < E > first;

private Node < E > last;

private int size;

public DLL() {

first = new Node < E > (null);

last = new Node < E > (null, first, null);

first.setNext(last);

size = 0;

}

public void print() {

Node < E > tempo = first.getNext();

while (tempo != last) {

System.out.print(tempo.getE().toString() + ", ");

tempo = tempo.getNext();

}

System.out.println();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Transaction Processing

Authors: Philip M. Lewis, Arthur Bernstein, Michael Kifer

1st Edition

0201708728, 978-0201708721

More Books

Students also viewed these Databases questions

Question

2. Discuss various aspects of the training design process.

Answered: 1 week ago