In: Computer Science
java
Modify doubly Linked List code to include following index (rank)
based access operations
int RetrieveAt(int index)
void DeleteAt(int index)
void Swap(int index, int index)
index starts at 0 (just like array's subscript)
If the index is out of bound, RetrieveAt returns 0, DeleteAt does
nothing, do nothing in Swap.
Write your own testing program to test the modified class
-----------------------------------------DLinkedlist.java----------------------------
public class DLinkedList { private class Node { String data; Node next; Node prev; public Node(String s) { data = s; next = null; prev = null; } } private Node first; private Node last; private int length; private Node curPos; public DLinkedList() { length = 0; first = last = curPos = null; } public void makeEmpty() { length = 0; first = last = curPos = null; } public int lengthIs() { return length; } public void addToFirst(String s) { Node newNode = new Node(s); newNode.next = first; if(length>0) first.prev = newNode; first = newNode; length++; if(length==1) last = newNode; } public void addToLast(String s) { Node newNode = new Node(s); if(length==0) { first = last = newNode; length++; return; } last.next = newNode; newNode.prev = last; last = newNode; length++; } public int Find(String s) { Node current = first; int rank = 0; while(current!=null) { if(current.data.equals(s)) return rank; else { current = current.next; rank++; } } return -1; } public void Insert(String s, int rank) { if(rank<=0) { addToFirst(s); return; } if(rank>=length) { addToLast(s); return; } Node newNode = new Node(s); Node current = first; for(int i=0;i<rank-1;i++) current = current.next; newNode.next = current.next; current.next = newNode; newNode.prev = current; newNode.next.prev = newNode; length++; } public void deleteFromFirst() { if(length==0) return; if(curPos == first) curPos = null; first = first.next; if(first!=null) first.prev = null; length--; if(length==0) last = null; } public void deleteFromLast() { if(length==0) return; if(length==1) { makeEmpty(); return; } last.prev.next = null; if(curPos==last) curPos = last.prev; last = last.prev; length--; } public boolean isLast() { return curPos == last; } public String getNext() { if(isLast()) return null; if(curPos==null) curPos = first; else curPos = curPos.next; return curPos.data; } public void reset() { curPos = null; } public String toString() { String output = "The list has:"; Node current = first; for(int i=0;i<length;i++) { output = output+'\n'+current.data; current = current.next; } return output; } }
-------------------------------------------DLinkedListProg.java---------------
public class DLinkedListProg { public static void main(String []args) { DLinkedList myList = new DLinkedList(); myList.addToLast("Hello 1"); myList.addToLast("Hello 2"); myList.addToLast("Hello 3"); myList.addToLast("Hello 4"); myList.addToLast("Hello 5"); myList.addToLast("Hello 6"); myList.Insert("Special", 2); System.out.println(myList.Find("Special")); System.out.println(myList.Find("Speciall")); System.out.println(myList); myList.reset(); while(!myList.isLast()) System.out.println(myList.getNext()); while(myList.lengthIs()>0) myList.deleteFromLast(); for(int i=0;i<500;i++) myList.addToFirst(Integer.toString(i)); while(myList.lengthIs()>0) { if(myList.lengthIs()%10000==0) System.out.println(myList.lengthIs()); myList.deleteFromLast(); } } }
public Node getAt(int index){
Node node= first;
// int i=0;
for(int i=0;i<index-1;i++)
node=node.next;
return node;
}
public String RetrieveAt(int index){
return getAt(index).data;
}
public void Delete(int index){
Node del= getAt(index);
if(del==first)
first=del.next;
if(del.next!=null)
del.next.prev=del.prev;
if(del.prev!=null)
del.prev.next=del.next;
return;
}
public void Swap(int indx1, int indx2){
Node node1= getAt(indx1);
Node node2= getAt(indx2);
String tmp= node1.data;
node1.data=node2.data;
node2.data=tmp;
}
Simply Copy paste these methods. Do not print myList using print command. Use while loop like u have usen below.