If you can give me an answer with some explanation on what classes and methods d
ID: 3871250 • Letter: I
Question
If you can give me an answer with some explanation on what classes and methods do, that would be great! Thanks in advance. (Java)
Problem:
Implement a Double linked List ADT to store a collection of integers.
1. a member function pushFront(data) that inserts a node with data at the front of the list
2. a member function pushBack(data) that appends a node with data at the back of the list
3. a member function popFront() that removes first node of the list.
4. a member function popBack() that removes last node of the list.
5. a member function insert(index, val) that inserts a new node with value "val" at a specific position mentioned by the index argument.
6. a member function deleteDuplicates(val) that deletes a node with that number and all its copies from the list, where these copies can be located anywhere in the list.
7. a member function mtoLastElement(M) that returns Mth to the last element of a list such that when M = 0, the last element of the list is returned.
8. a member function size() that returns the size of the list.
9. a member function reverseList() that reverses a linked list without recreating a temporary copy of this linked list. In other words, your function CAN NOT use the 'new' operator. Here is an example, if a list contains the following data items, 3 -> 5 -> 1 -> 7; this reverse() function will change the list to 7 -> 1 -> 5 -> 3.
10. a MemberFunction mergeLists(linkedList one, linkedList two) which will create a new list that contains the elements of two lists in sorted order(ascending). Assume the two lists are in sorted order for this case
Ex: one: 1 -> 3 -> 7 -> 25 -> 50 (sorted)
two: 5 -> 9 -> 11 - > 12 -> 29 (sorted)
result list : 1 -> 3 -> 5 -> 7 - > 9 -> 11 -> 12 -> 25 -> 29 -> 50 (Sorted)
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.september;
/**
*
* @author Sam
*/
public class DoublyLL<T> {
class Node {
T data;
Node next;
Node prev;
public Node(T data, Node prev, Node next) {
this.data = data;
this.next = next;
this.prev = prev;
}
}
Node head, tail;
public DoublyLL() {
head = tail = null;
}
void pushFront(T data) {
if (head == null) {
head = tail = new Node(data, null, null);
return;
}
head = new Node(data, null, head);
head.next.prev = head;
}
void pushBack(T data) {
if (tail == null) {
head = tail = new Node(data, null, null);
return;
}
tail = new Node(data, tail, null);
tail.prev.next = tail;
}
T popFront() {
if (head == null)
return null;
T tmp = head.data;
head = head.next;
head.prev = null;
if (head == null)
tail = null;
return tmp;
}
T popBack() {
if (tail == null)
return null;
T tmp = tail.data;
tail = tail.prev;
tail.next = null;
if (tail == null)
head = null;
return tmp;
}
}
Inplemented 4 methods. Due to very high volume, we can only solve first 4 methods.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.