public class MyLinkedList <AnyType> implements Iterable<AnyType> { private int t
ID: 3667596 • Letter: P
Question
public class MyLinkedList <AnyType> implements Iterable<AnyType> {
private int theSize;
private Node<AnyType> beginMarker;
private Node<AnyType> endMarker;
public class Node<AnyType>{
public Node (AnyType data, Node<AnyType> head, Node<AnyType> tail){
myData=data;
myHead=head;
myTail=tail;
}
public AnyType myData;
public Node<AnyType> myHead;
public Node<AnyType> myTail;
}
public MyLinkedList(){}
public void clear(){}
public int size(){
}
public boolean exist(AnyType newVal){
}
public boolean add(AnyType newVal){
}
public boolean add(int index, AnyType newVal) {
}
public Node<AnyType> get(AnyType nodeData){}
public void printList(){};
public java.util.Iterator<AnyType> iterator(){}
public void remove(AnyType removeVal){}
private class LinkedListIterator implements java.util.Iterator<AnyType>{
public void remove(){}
public boolean hasNext() {}
public AnyType next() {}
}
private void addBefore(Node<AnyType> previousNode, AnyType newNode){}
private void testListIntegers(){
MyLinkedList<Integer> testList = new MyLinkedList<Integer>();
testList.add(new Integer(5));
testList.add(new Integer(4));
testList.add(new Integer(3));
System.out.println(" We have so far inserted " + testList.size() + "
elements in the list");
testList.remove(4);
System.out.println(" Now, there is only " + testList.size() + " elements
left in the list");
testList.add(1, new Integer(7));
testList.add(2, new Integer(8));
System.out.println(" About to print content of the list");
testList.printList();
}
private void testListStrings(){
MyLinkedList<String> testList = new MyLinkedList<String>();
testList.add(new String("hello"));
testList.add(new String("this is"));
testList.add(new String("cs3345 project 2"));
System.out.println(" We have so far inserted " + testList.size() + ''
elements in the list");
testList.remove("this is");
System.out.println(" Now, there is only " + testList.size() + " elements
left in the list");
testList.add(1, "a modified version of");
testList.add(2, "cs3345 project 2, call it version 2" );
System.out.println(" About to print content of the list");
testList.printList();
}
public static void main (String args[]) throws Exception {
// Add whatever code you need here
// However, you will need to call both testListIntegers()
// and testListStrings()
}
}
Finish the coding the following double-linked list implementation (called “myLinkedList”) given bellow:
Most of the functions/definitions are not finished. You should code the existing functions without changing their existing definition. Also, you can add code to the “main” function. The goal is to show that you have a complete myLinkedList implementation. If needed, add new functions/methods to complete your implementation. Hint: Some of the definitions are already in the textbook, but might be a bit different
Explanation / Answer
import java.util.*;
import java.io.*;
public class DoublyLinkedListImpl<E>
{
private Node head;
private Node tail;
private int size;
public DoublyLinkedListImpl() {
size = 0;
}
/**
* this class keeps track of each element information
* @author java2novice
*
*/
private class Node {
E element;
Node next;
Node prev;
public Node(E element, Node next, Node prev) {
this.element = element;
this.next = next;
this.prev = prev;
}
}
/**
* returns the size of the linked list
* @return
*/
public int size() { return size; }
/**
* return whether the list is empty or not
* @return
*/
public boolean isEmpty() { return size == 0; }
/**
* adds element at the starting of the linked list
* @param element
*/
public void addFirst(E element) {
Node tmp = new Node(element, head, null);
if(head != null ) {head.prev = tmp;}
head = tmp;
if(tail == null) { tail = tmp;}
size++;
System.out.println("adding: "+element);
}
/**
* adds element at the end of the linked list
* @param element
*/
public void addLast(E element) {
Node tmp = new Node(element, null, tail);
if(tail != null) {tail.next = tmp;}
tail = tmp;
if(head == null) { head = tmp;}
size++;
System.out.println("adding: "+element);
}
/**
* this method walks forward through the linked list
*/
public void iterateForward(){
System.out.println("iterating forward..");
Node tmp = head;
while(tmp != null){
System.out.println(tmp.element);
tmp = tmp.next;
}
}
/**
* this method walks backward through the linked list
*/
public void iterateBackward(){
System.out.println("iterating backword..");
Node tmp = tail;
while(tmp != null){
System.out.println(tmp.element);
tmp = tmp.prev;
}
}
/**
* this method removes element from the start of the linked list
* @return
*/
public E removeFirst() {
if (size == 0) throw new NoSuchElementException();
Node tmp = head;
head = head.next;
head.prev = null;
size--;
System.out.println("deleted: "+tmp.element);
return tmp.element;
}
/**
* this method removes element from the end of the linked list
* @return
*/
public E removeLast() {
if (size == 0) throw new NoSuchElementException();
Node tmp = tail;
tail = tail.prev;
tail.next = null;
size--;
System.out.println("deleted: "+tmp.element);
return tmp.element;
}
public static void main(String a[]){
DoublyLinkedListImpl<Integer> dll = new DoublyLinkedListImpl<Integer>();
dll.addFirst(10);
dll.addFirst(34);
dll.addLast(56);
dll.addLast(364);
dll.iterateForward();
dll.removeFirst();
dll.removeLast();
dll.iterateBackwar();
}
}
Output:
adding: 10
adding: 34
adding: 56
adding: 364
iterating forward..
34
10
56
364
deleted: 34
deleted: 364
iterating backword..
56
10
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.