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 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...
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   ...
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...
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the...
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the addPlayers(int players) method * Complete the passPotatoe(int passes) method * No other methods/variables should be added/modified */ public class A3CircleLL {    /*    * Grading:    * Correctly uses helpers to play game - 1pt    * Prints correct winner when game is complete - 0.5pt    */    public void playGame(int players, int passes) {        /*        * Use...
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]
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given....
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add “100” to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print...
Build a doubly linked list with these operations: AddToHead(10); AddToHead(20); AddToTail(30); AddToTail(40); Build a sorted doubly...
Build a doubly linked list with these operations: AddToHead(10); AddToHead(20); AddToTail(30); AddToTail(40); Build a sorted doubly linked list with these operations: Add(30), Add(20), Add(40), Add(15), Add(35);
HOW DO I ACCESS THE HEAD OF A LINKED LIST FROM INT MAIN () WHEN IT'S...
HOW DO I ACCESS THE HEAD OF A LINKED LIST FROM INT MAIN () WHEN IT'S IN ANOTHER CLASS. HERE IS MY CODE FOR MY MAIN AND HEADER FILES. HEADER FILE: #ifndef HANOISTACK_H #define HANOISTACK_H #include <iostream> using namespace std; class HanoiStack { //class private: struct Disc{ //linked list for towers int num; Disc* next; }; Disc* head; public: HanoiStack(){ //constructor head = nullptr; }; //HanoiStack operator+=(const Disc&); //HanoiStack operator<<(const Disc&); void push(int); //push function int pop(); //pop function void...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT