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

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...
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...
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++
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks the user to enter n marks for n students, read the marks and the names and store them in a double linked list. 2. Write a method to find the largest mark and print the name of the student having that mark 3. Write a method to print the content of the list (name, mark) 4. Write a method to search the list for...
Can you fix please? this is adding the given location of doubly linked list in java...
Can you fix please? this is adding the given location of doubly linked list in java but if I make reverse is not adding the new node that I added but is printing forward correctly. just fix to me that part public void addAtLocation(E newNode, int location) {        Node node = new Node(newNode);               node.data = newNode;               Node previous = head;                      int counter = 1;...
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
Modify the linked list code from class to work with strings. Insert the following food items...
Modify the linked list code from class to work with strings. Insert the following food items into the list and display the list. The items are: bread, noodles, milk, bananas, eggs. Insert them in that order. Display the list. Then delete milk and redisplay the list. Then insert ice cream and redisplay the list. Then append zucchini and redisplay the list. c++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT