Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Part A: Coding 2. Implement Doubly Linked List add method which add an element to a specific position. - Its an instance method that takes
Part A: Coding
2. Implement Doubly Linked List add method which add an element to a specific position. - Its an instance method that takes a position and an element, then adds the element to this specific position and shifts the element currently at this position and any subsequent elements to the right. It throws an exception if the position is out of bound. It traverses the list from header if the position is closer to the header and traverses the list from trailer otherwise.
Given Codes:
/** * * Student: * */ public class A1LinkedList{ public static void main(String argc[]){ DLinkedListdl = new DLinkedList<>(); dl.add("Three",0); dl.add("Five",1); dl.add("One",0); dl.add("Two",1); dl.add("Four",3); dl.print(); class DLinkedList { private static class DNode { private E element; private DNode prev; private DNode next; public DNode(E e){ this(e, null, null); } public DNode(E e, DNode p, DNode n){ element = e; prev = p; next = n; } public E getE(){ return element; } public DNode getPrev(){ return prev; } public DNode getNext(){ return next; } public void setE(E e){ element = e; } public void setPrev(DNode p){ prev = p; } public void setNext(DNode n){ next = n; } } private DNode header; private DNode trailer; private int size; public DLinkedList(){ header = new DNode (null); trailer = new DNode (null, header, null); header.setNext(trailer); size = 0; } public void print(){ DNode temp = header.getNext(); while (temp != trailer){ System.out.print(temp.getE().toString() + ", "); temp = temp.getNext(); } System.out.println(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started