Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA: Please help me fix the error. (see my code below)My project is reverse a linked list and find the middle node in the linked

JAVA: Please help me fix the error. (see my code below)My project is reverse a linked list and find the middle node in the linked list. also help me add reading and writing part use argv[ ]. (reading input from text file, (string list in text file giving below) print the list to outFile, from listHead to the end of the list in the following format:

listHead -> (this node data, this nodes memo address, next nodes memo address, next nodes data) -> (this node data, this nodes memo address, next nodes memo address, next nodes data) -> . . . . . -> NULL

inFile (use argv[1]): A text file contains a list of English words (strings), giving below

outFile1 (use argv[2])a text file includes

i) The completed sorted linked list, in ascending order.

//With caption indicating you are printing the original sorted list

ii) The reversed linked list.

//With caption indicating you are printing the reversed sorted list

outFile2( use argv[3]): All debugging outputs.

(Do not print this file in the hard copy!! )

string list in a text file:

Hishaam Esteban Kevin

Matthew Brandon

Joel Luis Jianwei Yechiel

Taeyong

Jiayu

Jiade Phillip

Russell Mohebullah

Akshar Evgeniia Andres Marco Justin

Robin Kelvin Zhiheng Jeffrey

Yifei Yinyu

Jiaxin Youyia Eleftherios

Yuan

my code:

class LinkedList { static Node head; static class Node { int data; Node next; Node(int d) { data = d; next = null; } } Node reverseUtil(Node curr, Node prev) { if (curr.next == null) { head = curr; curr.next = prev; return head; } Node next1 = curr.next; curr.next = prev; reverseUtil(next1, curr); return head; }

void printList(Node node) { while (node != null) { System.out.print(node.data + " "); node = node.next; } } }

Find Middle

void printMiddle() { Node slow_ptr = head; Node fast_ptr = head; if (head != null) { while (fast_ptr != null && fast_ptr.next != null) { fast_ptr = fast_ptr.next.next; slow_ptr = slow_ptr.next; } System.out.println("The middle element is [" + slow_ptr.data + "] "); } }

Sort linked list in ascending order

public void sortList() { //Node current will point to head Node current = head, index = null; int temp; if(head == null) { return; } else { while(current != null) { //Node index will point to node next to current index = current.next; while(index != null) { //If current node's data is greater than index's node data, swap the data between them if(current.data > index.data) { temp = current.data; current.data = index.data; index.data = temp; } index = index.next; } current = current.next; } } }

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

More Books

Students also viewed these Databases questions