Question

In: Computer Science

Change the LinkList class to work with DNode, in particular, adapt the implementation of the void...

  • Change the LinkList class to work with DNode, in particular, adapt the implementation of the void LinkList.deleteLast() method.

LINKLIST CLASS BELOW

/**
* Builds a singly linked list of size 5 and prints it to the console.
*
* @author Jochen Lang
*/

class LinkList {
DNode llist;

LinkList( int sz ) {
   if ( sz <= 0 ) {
   llist = null;
   }
   else {
   // start with list of size 1
   llist = new DNode( "0", null, null );
   DNode current = llist; // temp node for loop
   // add further nodes
   for ( int i=1; i        // create node and attach it to the list
       DNode node2Add = new DNode( Integer.toString(i), null, null );
       current.setNext(node2Add); // add first node
       current=node2Add;
   }
   }
}
  
/**
* Print all the elements of the list assuming that they are Strings
*/
public void print() {
   /* Print the list */
   DNode current = llist; // point to the first node
   while (current != null) {
   System.out.print((String)current.getElement() + " ");  
   current = current.getNext(); // move to the next
   }
   System.out.println();  
}

public void deleteFirst() {
   if ( llist != null ) {
   llist = llist.getNext();
   }
}

public void deleteLast() {

}

// create and display a linked list
public static void main(String [] args){
   /* Create the list */
   LinkList llist = new LinkList( 5 );
   /* Print the list */
   llist.print();
   /* delete first and print */
   llist.deleteFirst();
   llist.print();
   /* delete last and print 5 times */
   for ( int i=0; i< 5; ++i ) {
   llist.deleteLast();
   llist.print();
   }
}
}

DNODE CLASS BELOW

public class DNode {
  private Object element;
  private Node next;
  private Node prev;
  Node() { this(null, null, null); }
    Node(Object e, Node n, Node p) {
    element = e;
    next = n;
    prev = p;
  }
  public void setElement(Object newElem) { element = newElem; }
  public void setPrev(Node newPrev) { prev = newPrev; }
  public void setNext(Node newNext) { next = newNext; }
  public Object getElement() { return element; }
  public Node getNext() { return next; }
  public Node getPrev() { return prev; }
}

Solutions

Expert Solution

Working fine now with DNode

/**

* Builds a singly linked list of size 5 and prints it to the console.

*

* @author Jochen Lang

*/

public class LinkList {

DNode llist;

LinkList(int sz) {

if (sz <= 0) {

llist = null;

} else {

// start with list of size 1

llist = new DNode("0", null, null);

DNode current = llist; // temp node for loop

// add further nodes

for (int i = 1; i <= 5; ++i) { // create node and attach it to the list

DNode node2Add = new DNode(Integer.toString(i), null, null);

current.setNext(node2Add); // add first node

current = node2Add;

}

}

}

/**

* Print all the elements of the list assuming that they are Strings

*/

public void print() {

/* Print the list */

DNode current = llist; // point to the first node

while (current != null) {

System.out.print((String) current.getElement() + " ");

current = current.getNext(); // move to the next

}

System.out.println();

}

public void deleteFirst() {

if (llist != null) {

llist = llist.getNext();

}

}

public void deleteLast() {

if (llist == null)

return;

else {

DNode temp = llist;

while (temp.getNext().getNext() != null)

temp = temp.getNext();

temp.setNext(null);

}

}

// create and display a linked list

public static void main(String[] args) {

/* Create the list */

LinkList llist = new LinkList(5);

/* Print the list */

llist.print();

/* delete first and print */

llist.deleteFirst();

llist.print();

/* delete last and print 5 times */

for (int i = 0; i < 5; ++i) {

llist.deleteLast();

llist.print();

}

}

}

===============================================================================
// DNODE CLASS BELOW, Modified DNODE Class as well

public class DNode {

private Object element;

private DNode next;

private DNode prev;

DNode() {

this(null, null, null);

}

DNode(Object e, DNode n, DNode p) {

element = e;

next = n;

prev = p;

}

public void setElement(Object newElem) {

element = newElem;

}

public void setPrev(DNode newPrev) {

prev = newPrev;

}

public void setNext(DNode newNext) {

next = newNext;

}

public Object getElement() {

return element;

}

public DNode getNext() {

return next;

}

public DNode getPrev() {

return prev;

}

}

======================================================================

Thanks, PLEASE COMMENT if there is any concern.


Related Solutions

Adapt the original LinkList class to use the generic Gnode class Code for LinkList below class...
Adapt the original LinkList class to use the generic Gnode class Code for LinkList below class LinkList { Node llist; LinkList( int sz ) { if ( sz <= 0 ) { llist = null; } else { // start with list of size 1 llist = new Node( "0", null ); Node current = llist; // temp node for loop // add further nodes for ( int i=1; i<sz; ++i ) { // create node and attach it to...
class LinkListTest { public static void main(String[] args) { LinkList theList = new LinkList(); theList.insertFirst(7); theList.insertFirst(6);...
class LinkListTest { public static void main(String[] args) { LinkList theList = new LinkList(); theList.insertFirst(7); theList.insertFirst(6); theList.insertFirst(5); theList.insertFirst(4); theList.insertFirst(3); theList.insertFirst(2); theList.insertFirst(1); theList.displayList(); System.out.println("delete(4)"); theList.delete(4); System.out.println("delete(16)"); theList.delete(16); theList.displayList(); System.out.println("insertAfter(2, 12)"); theList.insertAfter(2, 12); System.out.println("insertAfter(4, 14)"); theList.insertAfter(4, 14); System.out.println("insertAfter(7, 17)"); theList.insertAfter(7, 17); theList.displayList(); System.out.println("insertLast(20)"); theList.insertLast(20); theList.displayList(); } } class Link { public int iData; // data item public Link next; // next link in list // ------------------------------------------------------------- public Link(int id) // constructor { iData = id; // initialize data next = null; }...
1. Adapt the custom array list implementation code with the following changes: (a) Add code to...
1. Adapt the custom array list implementation code with the following changes: (a) Add code to the ensureCapacity() method to print out a message including how many elements are copied to the new array on resizing Array List Implementation: public class MyArrayList<E> implements MyList<E> { public static final int INITIAL_CAPACITY = 16; private E[] data = (E[])new Object[INITIAL_CAPACITY]; private int size = 0; // Number of elements in the list public MyArrayList() { }    public MyArrayList(E[] objects) { for...
16. Why is it important to be able to adapt to changes in technology and work...
16. Why is it important to be able to adapt to changes in technology and work organisation in a timely manner?
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed);...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed); void stop(); double get_speed() const; private: double speed; }; Which of the following options would make logical sense in the definition of the void accelerate(double speed)function? Group of answer choices this->speed = this->speed; this->speed = speed; this.speed = speed; speed1 = this->speed; Flag this Question Question 131 pts The Point class has a public function called display_point(). What is the correct way of calling...
1. Given that the business community must constantly adapt to change, examine the impact of the...
1. Given that the business community must constantly adapt to change, examine the impact of the Internet on current business practices. 2. Given that the “what” of business transactions has not changed, but the “how” of e-commerce has changed enormously, examine the infrastructure components necessary to implement a web strategy. 3.How do these two factors and determine what their effect would be on an online retail company.
1. How did Amazon adapt to change in technology as well as their customers' needs and...
1. How did Amazon adapt to change in technology as well as their customers' needs and wants since they were founded in 1995? 2. What were some of the struggles that Amazon faced when adapting to technolgy changes? 3. How did Amazon recover from the struggles they faced when adapting to change in technology?
Complete the implementation of the Die class. Note that the Die class uses an array to...
Complete the implementation of the Die class. Note that the Die class uses an array to represent the faces of a die. You need to complete the no-argument constructor and the two methods compareTo and equals. Code: import java.util.Arrays; import java.util.Objects; import java.util.Random; /* * NOTE TO STUDENTS: * The constructor that you need to complete can be found on line 47. * * The two methods you need to complete can be found at the end of this file....
● Change the Deck class into a Singleton class. ● Change the main method to get...
● Change the Deck class into a Singleton class. ● Change the main method to get the Deck instance rather than creating a new object. ● The program should print all 52 cards in random order import java.util.*; enum Suit { SPADES, HEARTS, CLUBS, DIAMONDS } class Card { public Card(Suit s, int n) { suit = s; if((n < 2) || (n > 14)) { throw new IllegalArgumentException( ); } number = n; } public void print( ) {...
How does the culture of an organization affect its effectiveness to adapt to change??300 words?
How does the culture of an organization affect its effectiveness to adapt to change??300 words?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT