Question

In: Computer Science

Using java: Implement a basic doubly-linked list that implements a priority system sorting the elements that...

Using java:

Implement a basic doubly-linked list that implements a priority system sorting the elements that are inserted. Sort based on the speed of the warrior.

Driver code:

public class LinkedListDriver {
public static void main(String[] args) {
LinkedList list = new SortedDoublyLinkedList();

System.out.println(list);
Warrior krogg = new Warrior("Krogg", 30, 50, 200);
list.insert(krogg);

System.out.println(list);
Warrior gurkh = new Warrior("Gurkh", 40, 45, 180);
list.insert(gurkh);

System.out.println(list);
Warrior brynn = new Warrior("Brynn", 45, 40, 190);
list.insert(brynn);

System.out.println(list);
Warrior dolf = new Warrior("Dolf", 20, 65, 210);
list.insert(dolf);

System.out.println(list);
Warrior zuni = new Warrior("Zuni", 50, 35, 170);
list.insert(zuni);

System.out.println(list);
}
}

Warrior class:

public class Warrior {
private String name;
private int speed;
private int strength;
private int hp;
public Warrior(String name, int speed, int str, int hp) {
this.name = name;
this.speed = speed;
this.strength = str;
this.hp = hp;
}

public String getName() { return this.name; }
public int getSpeed() { return this.speed; }
public int getStrength() { return this.strength; }
public int getHp() { return this.hp; }

public String toString() { return this.name + "(" +
this.speed + ")"; }
}

Class that needs to be done:
interface LinkedList {
void insert(Warrior warrior);
String toString();
}

Expected output:
[ Zuni(50) Brynn(45) Gurkh(40) Krogg(30) Dolf(20) ]

Solutions

Expert Solution

SortedDoublyLinkedList class:

// create a SotredDoublyLinkedList class that implements LinkedList interface
public class SortedDoublyLinkedList implements LinkedList {
   // create a private Node class to store Warrior class data
   private class Node{
       Warrior warrior; // data member
       Node next; // link to next node
       Node prev; // link to previous node
       Node(Warrior w){ // constructor of inner class
           // initialize all members of inner class
           warrior = w;
           next = null;
           prev = null;
       }
   }
  
   public Node start; // first node of the list
   public Node end; // last node of list
  
   // constructor
   SortedDoublyLinkedList(){
       // initialize member variables
       start = null;
       end = null;
   }
  
   // implement interface methods

   @Override
   public void insert(Warrior warrior) {
       // add a node in the list,
       // in sorted order based on the speed of the warrior
      
       // create a node with given data
       Node n = new Node(warrior);
       // check if start is null
       if(start == null) {
           // enter first node
           start = n;
           end = n; // last node is first node(only node)
       }
       else {
           // check if node need to be inserted at start
           if(start.warrior.getSpeed()<n.warrior.getSpeed()) {
               // insert node at start
               n.next = start; // linked right side
               start.prev = n; // linked left side
               // reassign start
               start = n;
           }
           else {
               // iterate the list and insert node in sorted position
               Node itr = start;
               // create a flag to see if node is inserted
               boolean isNodeInseted = false;
               while(itr != end) {
                   if(itr.next.warrior.getSpeed()<n.warrior.getSpeed()) {
                       // insert node at the next location
                       n.next = itr.next; // link to right side
                       n.prev = itr; // link to left side
                       // reassign list node's links
                       itr.next.prev = n;
                       itr.next = n;
                       // set flag to true
                       isNodeInseted = true;
                       // break the while loop as node insertion is done
                       break;
                   }
                   else {
                       itr = itr.next;
                   }
               }//end of while loop
               // check if node is inserted or not
               if(!isNodeInseted) {
                   // insert the node at end of list
                   n.prev = end; // linked left side
                   end.next = n; // linked right side
                   // reassign end of list
                   end = n;
               }
           }// node inserted
       }
   }
  
   @Override
   public String toString() {
       // create a string to return
       String str = "[ ";
       // call toString of each node in the list
       // create iterator to loop the list
       Node itr = start;
       while(itr != null) {
           str = str + itr.warrior.toString() + " ";
           itr = itr.next;
       }
       str = str + "]";
       // return the string
       return str;
      
   }

}

LinkedListDriver class:


public class LinkedListDriver {
  
   public static void main(String[] args) {
       LinkedList list = new SortedDoublyLinkedList();
      
       System.out.println(list);
       Warrior krogg = new Warrior("Krogg", 30, 50, 200);
       list.insert(krogg);
      
       System.out.println(list);
       Warrior gurkh = new Warrior("Gurkh", 40, 45, 180);
       list.insert(gurkh);
      
       System.out.println(list);
       Warrior brynn = new Warrior("Brynn", 45, 40, 190);
       list.insert(brynn);
      
       System.out.println(list);
       Warrior dolf = new Warrior("Dolf", 20, 65, 210);
       list.insert(dolf);
      
       System.out.println(list);
       Warrior zuni = new Warrior("Zuni", 50, 35, 170);
       list.insert(zuni);
      
       System.out.println(list);
   }
}


Warrior class:

public class Warrior {
   private String name;
   private int speed;
   private int strength;
   private int hp;
   public Warrior(String name, int speed, int str, int hp) {
       this.name = name;
       this.speed = speed;
       this.strength = str;
       this.hp = hp;
   }
  
   public String getName() { return this.name; }
   public int getSpeed() { return this.speed; }
   public int getStrength() { return this.strength; }
   public int getHp() { return this.hp; }
  
   public String toString() {
       return this.name + "(" + this.speed + ")";
   }
}


LinkedLIst interface:

public interface LinkedList {
  
   void insert(Warrior warrior);
   String toString();

}



Related Solutions

write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
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++
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.
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy node. This challenge simulates an operating system’s window manager. Requirements Write the following struct struct Window { string appname; Window *next; Window *prev; }; Create a class called WindowManager. In this class, create a private variable Window * head. This will keep track of the location of the head node. Create private variables Window * current, * dummy. current will keep track of the...
Write an efficient java program to implement an integer doubly linked list Dequeue which insertion and...
Write an efficient java program to implement an integer doubly linked list Dequeue which insertion and deletion can be than at either end. You have to write 6 methods: add front, delete front, add rear, delete rear, print forward (left to right) and print backward (right to left). After each addition or deletion dequeue elements are printed forward or backward respectively. Your main method should be as follow: public static void main(String args[]) { xxxxx dq = new xxxxx ();...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT