java - Implementation of ArrayList using a LinkedList -
i need implement both queue , arraylist using internal linkedlist. created doublylinkedlist class , able implement queue no problem. problem running add or delete to/from arraylist, add/delete methods take in integer index , object/element. methods inside doublylinkedlist class take in either elements and/or nodes.
my question this, how can implement doublylinkedlist methods inside arraylist when dll class doesn't take int values in.
i want able add or delete node using index, can't. realistically, want list.addafter(i) without being integer.
note: goal of assignment implement adts, can't modify method signatures of arraylist adt.
doublylinedlist class
public class doublylinkedlist<e> { private node<e> head; private node<e> tail; private int size; public doublylinkedlist() { this.head = new node<e>(null, null, null); this.tail = new node<e>(null, null, null); this.size = 0; head.setnext(tail); tail.setprev(head); } public int size() { return size; } public boolean isempty() { return size == 0; } public node<e> getprev(node<e> n) { return n.getprev(); } public node<e> getnext(node<e> n) { return n.getnext(); } public node<e> getfirst() { return head.getnext(); } public node<e> getlast() { return tail.getprev(); } public e remove(node<e> c) { node<e> = c.getprev(); node<e> b = c.getnext(); b.setnext(a); a.setprev(b); c.setnext(null); c.setprev(null); size--; return c.getelement(); } public e removefirst() { return remove(head.getnext()); // first element beyond header } public e removelast() { return remove(tail.getprev()); } public void addbefore(node<e> node, e e) { node<e> prev = getprev(node); node<e> n = new node<e>(e, prev, node); node.setprev(n); prev.setnext(n); size++; } public void addfirst(e e) { addafter(head, e); } public void addlast(e e) { addbefore(tail, e); } public void addafter(node<e> node, e e) { node<e> next = getnext(node); node<e> n = new node<e>(e, node, next); node.setnext(n); next.setprev(n); size++; } }
larraylist class (my arraylist implementation)
public class larraylist implements list { private doublylinkedlist list; private int size; public larraylist() { this.list = new doublylinkedlist(); this.size = 0; } public int size() { return size; } public boolean isempty() { return size == 0; } public void add(int i, object e) throws indexoutofboundsexception { if (isempty()) { list.addfirst(e); } // here concern. these 4 methods take in int values while // non of dll methods do! } public object get(int i) throws indexoutofboundsexception { return null; } public object remove(int i) throws indexoutofboundsexception { return null; } public object set(int i, object e) throws indexoutofboundsexception { return null; } }
it seems easy thing - use api exposed linkedlist , add logic it. here bit missing
if (list.size() < i) { throw new indexoutofboundsexception() } //get starting point node node = list.getfirst(); //loop until specified position while(i-- > 0) { node = list.getnext(node); } //now node points @ node in position - insert new //node before comply list interface list.addbefore(node, e); this.size++;
i have note linkedlist implementation can improved - first of all, getprev()
getnext()
addbefore()
, addafter()
should static, shouldn't have use linkedlist instance call them. however, better if methods methods in node, because way traversal , usage of linkedlist way more easy. here how above code if methods in node:
if (list.size() < i) { throw new indexoutofboundsexception() } //get starting point node node = list.getfirst(); //loop until specified position while(i-- > 0) { node = node.getnext(); } //now node points @ node in position - insert new //node before comply list interface node.addbefore(e); this.size++;
you pretty not need list @ - don't need pass parameters functions. can still keep (hopefully static) methods in linked list same thing, they'd proxies node implementation of methods, e.g.:
public static void addafter(node<e> node, e e) { node.addafter(e); }
i not sure if need these methods in linkedlist can there "backwards compliance", if will.
edit forgot mention - fist bit of code implementation add(), sure can work out rest, they'd same thing.
Comments
Post a Comment