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

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...
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...
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?
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.
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?
C++ exercise ! Change the WEEK‐4 program to work through the GradeBook class. This program has...
C++ exercise ! Change the WEEK‐4 program to work through the GradeBook class. This program has some functionality that you can use with the Gradebook class. So, please revise Gradebook class so that the class can use sort() and display() functions of WEEK4 program . week-4 program : #include #include using namespace std; void sort(char nm[][10]); void display(char name[][10]); int main() { char names[10][10]; int i; for (i=0; i<10; i++) { cout << "Enter name of the student : ";...
Healthcare Management 1. Describe tools that healthcare leaders can use to change and adapt culture in...
Healthcare Management 1. Describe tools that healthcare leaders can use to change and adapt culture in a health organization. 2. Compare global leadership style differences and similarities using appropriate constructs to transformational leadership practices. 3. Evaluate approaches to knowledge management, organizational learning, and transformational leadership with health organization culture change.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT