Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

below has there classes: MYLinkList, Node and TestList for link list, please adit or add some code to invert the list(by reference not data) it

below has there classes: MYLinkList, Node and TestList for link list, please adit or add some code to invert the list(by reference not data) it means the output should be reverse from "Linked list has: 11 elements -> [ 0 1 2 3 4 5 6 7 8 9 10 ]" to like "10 9 8 7 6 5 4 3 2 1 0" provide a screen shot for the output if u can plz 

---------------------------------------------------------------------------------------------------

public class MyLinkedList { private Node head; //reference to a node to indicate initial element private int size = 0; //the list is empty at creation //no-args constructor for the list public MyLinkedList() { head = new Node(); } //constructor with vector parameter, create a list with values //provided via a linear array public MyLinkedList(int[] args) { head = new Node(); for(int i=0;i "); Node aux = this.head; System.out.print("[ "); while ((aux != null) && (this.size > 0)) { System.out.print(aux.getData() + " "); aux = aux.getNext(); } System.out.println("]"); } public void removeLast() { if (this.size == 0) return; else if (this.size == 1) { this.size--; return; } else if (this.size == 2){ this.head.setNext(null); this.size = 1; return; } Node newLast = this.head; while(newLast.getNext().getNext() != null) { newLast = newLast.getNext(); } newLast.setNext(null); this.size--; } public void insertNode(int value) { Node aux = this.head; Node newNode = new Node(value); if(size==0) { head = newNode; this.size++; return; } if(this.head.getData()>=newNode.getData()) { newNode.setNext(head); head = newNode; } else { while((aux.getNext().getData() <= newNode.getData()) && (aux.getNext().getNext() != null)) { aux = aux.getNext(); } if(aux.getNext().getData() <= newNode.getData()) { this.append(value); this.size--; //append will increase size } else { newNode.setNext(aux.getNext()); aux.setNext(newNode); } } this.size++; } @Override public String toString() { String out = "Linked list has: " + this.getSize() + " elements -> "; Node aux = this.head; out += "[ "; while ((aux != null) && (this.size > 0)) { out = out + aux.getData() + " "; aux = aux.getNext(); } out += "]"; return out; } } 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

class Node{ private int data; //a generic can be used for accepting Objects private Node next; //point a node of this same class //no-args constructor public Node() { this(0); //calls next constructor with value=0 } //constructor with one value for data public Node(int nbr) { this.setData(nbr); this.next = null; } public Node getNext() { return next; } public void setNext(Node End) { this.next = End; } public int getData() { return this.data; } public void setData(int val) { this.data=val; } } 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class TestMyLList { public static void main(String[] args) { MyLinkedList lista = new MyLinkedList(); lista.display(); lista.append(5); lista.display(); lista.append(8); lista.display(); lista.append(9); lista.display(); lista.append(10); lista.display(); lista.removeLast(); lista.display(); lista.removeLast(); lista.display(); lista.removeLast(); lista.display(); lista.removeLast(); lista.display(); lista.removeLast(); lista.display(); lista.removeLast(); lista.display(); lista.append(10); lista.display(); MyLinkedList lista2 = new MyLinkedList(new int[] {2,4,5,6,8,9}); lista2.display(); lista2.insertNode(3); lista2.display(); lista2.insertNode(7); lista2.display(); lista2.insertNode(1); lista2.display(); lista2.insertNode(10); lista2.display(); lista2.insertNode(0); lista2.display(); System.out.println(lista2); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases DeMYSTiFieD

Authors: Andy Oppel

2nd Edition

0071747990, 978-0071747998

Students also viewed these Databases questions