Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help me complete the java methods in the picture. Here is my code so far: public class DynamicNode { private Object info; private DynamicNode
Please help me complete the java methods in the picture.
Here is my code so far:
public class DynamicNode { private Object info; private DynamicNode next; public DynamicNode(Object x, DynamicNode n){ info = x; next = n; } public DynamicNode() { } public Object getInfo(){ return info; } public DynamicNode getNext(){ return next; } public void setInfo(Object x){ info = x; } public void setNext(DynamicNode n){ next = n; } } // end of DynamicNode class
public class DynamicList { private DynamicNode list; public DynamicList() { list = null; } public boolean isEmpty() { return list == null; } public void insertFirst(Object x) { DynamicNode q = new DynamicNode(x, null); if (isEmpty()) { q.setNext(list); } list = q; } // end of insertFirst public void insertAfter(DynamicNode p, Object x) { if (p == null) { System.out.println("void insertion"); System.exit(1); } DynamicNode q = new DynamicNode(x, p.getNext()); p.setNext(q); } // end of insertAfter public Object deleteFirst() { if (isEmpty()) { System.out.println("void deletion"); System.exit(1); } Object temp = list.getInfo(); if (list.getNext() = null) { list = null; } else { list = list.getNext(); } return temp; } // end of deleteFirst public Object deleteAfter(DynamicNode p) { if (p == null || p.getNext() == null) { System.out.println("void deletion"); System.exit(1); } DynamicNode q = p.getNext(); Object temp = q.getInfo(); p.setNext(q.setNext()); return temp; } // end of deleteAfter public void print() { // finish print method } public boolean appendList(DynamicList othrList) { // finish append list method } public void reverse() { // finish reverse method } public Object deleteMid(){ // finish delete mid } public static void main(String[] args) { } }
Here is the image of what needs to be done please:
Write the code for Dynamiclist and DynamicNode from the slides and textbook, including a method to print all elements in the list. You may include insertFirst, but NOT insertAfter, deleteFirst, and deleteAfter. (if they are there,please comment them out). Your main() test code may use insertFirst() to create the lists to test your methods below (see output in last page) 1. 2. Implement the following member method appendList() for class Dynamiclist * Appends all elements in the parameter list othrList *to the end of this list. Returns true if the list was changed, false otherwise. *Please note that NO new 1list is created. Also, it is wrong to (repeatedly) insert new nodes to the list. public oolean appendlist (DynamicList othrList) i 1 Example: The original list is 1->6->5. The other list is 3->8->2 After the call, the list is now 1->6->5->3->8-2 Do not use insertFirst, insertAfter, deleteFirst, and deleteAfter in this method 3. Write a void method, called reverse(), for DynamicList (singly-linked). This method reverses the order of the nodes in the linked-list. This must be done "in place", without creating a new list. Do not use insertFirst, insertAfter, deleteFirst, and deleteAfter in this method see next page 4. Implement the following member method deleteMid) for class Dynamiclist. *Deletes the middle node in the list IF it exists. *If the list has an EVEN number of nodes, there is NO middle node * Constraint: you cannot use a counter or a boolean * Given a list abcd e, c is deleted * Given a list abcd, nothing is deleted. Returns: info in the middle node if it exists: null otherwise public Object deleteMid f 1 Do not use insertFirst, insertAfter, deleteFirst, and deleteAfter in this methodStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started