Question

In: Computer Science

java Modify doubly Linked List code to include following index (rank) based access operations int RetrieveAt(int...

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();
        }
    }
}

Solutions

Expert Solution


    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.


Related Solutions

Java Data Structure Doubly Linked List /* * Complete the swap(int index) method * No other...
Java Data Structure Doubly Linked List /* * Complete the swap(int index) method * No other methods/variables should be added/modified */ public class A3DoubleLL<E> {    /*    * Grading:    * Swapped nodes without modifying values - 2pt    * Works for all special cases - 1pt    */    public void swap(int index) {        //swap the nodes at index and index+1        //change the next/prev connections, do not modify the values        //do not...
Using Doubly Linked List, create a java code that does the following Without using LinkedList from...
Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function. Create a menu that contains the following operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL. ( as a METHOD...
Modify this linked list code to work with string. Insert the following items into the list...
Modify this linked list code to work with string. Insert the following items into the list and display the list. The items are: Pepsi, Coke, DrPepper, Sprite, Fanta. Insert them in that order. Display the list. Then delete DrPepper and redisplay the list. Then insert 7-UP and redisplay the list. Then append Water and redisplay the list. c++ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ #include <iostream> using namespace std; class ListNode { public:     int value;     ListNode *next;     ListNode(int nodeValue) {       value...
I need an example of how to swap and index within a doubly linked list with...
I need an example of how to swap and index within a doubly linked list with index + 1. This is written in java. public class A3DoubleLL<E> {    /*    * Grading:    * Swapped nodes without modifying values - 2pt    * Works for all special cases - 1pt    */    public void swap(int index) {        //swap the nodes at index and index+1        //change the next/prev connections, do not modify the values   ...
I know this code takes in a set of numbers into a doubly linked list and...
I know this code takes in a set of numbers into a doubly linked list and sorts it using insertion sort. Could you explain exactly how this code is working? main.c Source Code: #include #include #include "node.h" int main() { struct mynode *head=NULL; int value; printf("Give first value: \n"); scanf("%d",&value); printf("Give next values, and input 0 to end list: \n"); do{ if(value>0){    head = pushNode(head, value);    scanf("%d",&value); } }while (value>0); printf("Before insertion sort: "); printlist(head); head=insertsort(head); printf("After insertion...
This is the code what I have for doubly linked list for STACK. This is Python...
This is the code what I have for doubly linked list for STACK. This is Python language and I want anyone to help me with the following questions. Can you check for me if it is good Doubly Linked List? ####THIS IS THE ENTIRE ASSIGNMENT#### ADD the Following feature: Include a class attribute in the container class called name. In the implementation - Pod: You should ask the user to enter the name of the container and the program should...
Hi! I 'm writing a code for a doubly linked list and this is the header...
Hi! I 'm writing a code for a doubly linked list and this is the header file #include<iostream> #include <string> using namespace std; struct node { int data; node *next,*prev; node(int d,node *p=0,node *n=0) { data=d; prev=p; next=n; } }; class list { node *head,*tail; public: list(); bool is_empty(); int size(); void print(); void search(); int search2(int el); void add_last(int el); void add_first(int el); bool add_pos(); bool delete_first(); bool delete_last(); void delete_pos(int pos); void delete_el(); void add_sorted(); }; i want...
In this Java program you will implement your own doubly linked lists. Implement the following operations...
In this Java program you will implement your own doubly linked lists. Implement the following operations that Java7 LinkedLists have. 1. public DList() This creates the empty list 2. public void addFirst(String element) adds the element to the front of the list 3. public void addLast(String element) adds the element to the end of the list 4. public String getFirst() 5. public String getLast() 6. public String removeLast() removes & returns the last element of the list. 7. public String...
In JAVA: Create a circular doubly linked list. It need not be generic. Implement addToStart and...
In JAVA: Create a circular doubly linked list. It need not be generic. Implement addToStart and addToEnd methods, as well as printList method. Implement delete(Node n) method that deletes a node n, if n is in the linked list. Make no assumptions about n. Test your linked list.
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT