Write each of the following methods at the end of the DLL.java: 1. public T get(
ID: 3915513 • Letter: W
Question
Write each of the following methods at the end of the DLL.java:
1. public T get(int index) : Returns the element at the given index. Returns null if the index is not valid. Note: index starts at 0. So valid index is in the range [0 …length()-1].
2. public int indexOf(T el) : Returns the index of the given el if it exists in the list, Returns -1 otherwise.
3. public void replace(int index, T el) : Replaces the element at the given index with the given el. Do nothing if the index is not valid.
4. public void insertAt(int index, T el) : Inserts el at position index in the list. If index is outside the range [0 ... length()], your method should ignore the insertion.
5. public T deleteAt(int index) : Deletes and returns the element at the given index if the index is invalid or returns null otherwise.
_________________________________________________________________________________________
//**************************** 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 clear() {
head = tail = null;
}
public T getFirst() {
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 delete(T el) { // delete the node with an element el;
DLLNode<T> tmp;
for (tmp = head; tmp != null && !el.equals(tmp.info); tmp = tmp.next); //locate the item
if (tmp != null) { // item found
if (head == tail) //the found item was the only one
head = tail = null;
else if (head == tmp) { //the found item is the first
head = head.next;
head.prev = null;
}
else if (tail == tmp) { // the found item is the last
tail = tail.prev;
tail.next = null;
}
else { // the found item is in the middle
tmp.prev.next = tmp.next;
tmp.next.prev = tmp.prev;
}
}
}
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;
}
// node of generic doubly linked list class
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;
}
}
}
______________________________________________________________________________
Explanation / Answer
I have added the required method in below class
public class DLL<T> {
private DLLNode<T> head, tail;
public DLL() {
head = tail = null;
}
public boolean isEmpty() {
return head == null;
}
public void clear() {
head = tail = null;
}
public T getFirst() {
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 delete(T el) { // delete the node with an element el;
DLLNode<T> tmp;
for (tmp = head; tmp != null && !el.equals(tmp.info); tmp = tmp.next)
; // locate the item
if (tmp != null) { // item found
if (head == tail) // the found item was the only one
head = tail = null;
else if (head == tmp) { // the found item is the first
head = head.next;
head.prev = null;
}
else if (tail == tmp) { // the found item is the last
tail = tail.prev;
tail.next = null;
}
else { // the found item is in the middle
tmp.prev.next = tmp.next;
tmp.next.prev = tmp.prev;
}
}
}
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 int length() {
int count = 0;
DLLNode<T> tmp = head;
for (tmp = head; tmp != null; tmp = tmp.next, count++)
;
return count;
}
public T get(int index) {
DLLNode<T> tmp = head;
int count = 0;
if (index < 0 || index > length() - 1) {
return null;
} else {
for (tmp = head; count != index; tmp = tmp.next, count++)
;
return tmp.info;
}
}
public int indexOf(T el) {
DLLNode<T> tmp = head;
int count = 0;
for (tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next, count++)
;
if (tmp == null) {
return -1;
} else {
return count;
}
}
public void replace(int index, T el) {
DLLNode<T> tmp = head;
int count = 0;
for (tmp = head; tmp != null && count != index; tmp = tmp.next, count++)
;
if (tmp != null) {
tmp.info = el;
}
}
public void insertAt(int index, T el) {
DLLNode<T> tmp = head;
int count = 0;
if (index >= 0 && index <= length()) {
if (index == 0) {
addToHead(el);
} else if (index == length()) {
addToTail(el);
} else {
for (tmp = head; count != index; tmp = tmp.next, count++)
;
tmp.prev.next = tmp.prev = new DLLNode<T>(el, tmp, tmp.prev);
}
}
}
public T deleteAt(int index) {
DLLNode<T> tmp = head;
int count = 0;
if (index < 0 || index > length() - 1) {
return null;
} else if (isEmpty()) {
return null;
} else if (length() == 1) {
T el = head.info;
clear();
return el;
} else if (index == 0) {
T el = tmp.info;
head = tmp.next;
head.prev = null;
return el;
} else if (index == length() - 1) {
T el = tail.info;
tail = tail.prev;
tail.next = null;
return el;
} else {
for (tmp = head; count != index; tmp = tmp.next, count++)
;
T el = tmp.info;
tmp.next.prev = tmp.prev;
tmp.prev.next = tmp.next;
return el;
}
}
// node of generic doubly linked list class
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;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.