Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please modify the given code below so that the node and linked list classes can store objects of any type (not only integers). package code;

Please modify the given code below so that the node and linked list classes can store objects of any type (not only integers).

package code;

public class Code { static class Node { int data; Node next; };

private static Node root;

private static Node insert(Node root, int val) { Node temp = new Node(); temp.data = val; temp.next = root; root = temp; return root; }

private static Node arrToLL(int arr[], int n) { root = null; for (int i = n - 1; i >= 0; i--) root = insert(root, arr[i]); return root; }

public static Node reverse(Node node) { Node prev = null; Node current = node; Node next = null; while (current != null) { next = current.next; current.next = prev; prev = current; current = next; } node = prev; return node; }

static void display(Node root) { while (root != null) { System.out.print(root.data + " "); root = root.next; } System.out.print(" "); }

public static void main(String[] args) { int[] intArray = new int[] { 10, 15, 35, 67, 89, 101 };

Node root = arrToLL(intArray, 6); System.out.print("Elements of the list: "); display(root); Node new_root = reverse(root); System.out.print("Elements of list after reversing: "); display(new_root); }

}

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

Step: 3

blur-text-image

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

Mobile Usability

Authors: Jakob Nielsen, Raluca Budiu

1st Edition

0133122131, 9780133122138

More Books

Students also viewed these Programming questions

Question

40. Consider a M/G/1 system with E[S] Answered: 1 week ago

Answered: 1 week ago