Question

JAVA Program: reverse a linked list and find the middle node in the linked list. inFile...

JAVA Program: reverse a linked list and find the middle node in the linked list.

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

  Must have all the object classes as given below.

********************************

- listNode class

- data (string)

- next (listNode *)

methods:

- printNode (node) // use the format:

(this node’ data, this node’s memo address, next node’s memo address, next node’s data)

see print list example below.

- A linkedList class

- listHead (listNode *) // Initially it points to a dummy node

methods:

- constructLL (…)

- findMiddleNode (…) // This method is not called in this project!

- reverseLL(…) // See algorithm steps below.

- moveSpotNodeToFront (listHead, Spot) // on your own!

            // this method moves the node which is pointed by Spot to the front of the list.

// i.e., listHead’s next is pointing to the node in the front of the list.

- listInsert (…)

- findSpot (…) // Use the findSpot algorithm steps taught in class.

- printList (listHead, outFile)

// print the list to outFile, from listHead to the end of the list in the following format:

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Given that:

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 + "] \n");
}
}

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;
}
}
}

Add a comment
Know the answer?
Add Answer to:
JAVA Program: reverse a linked list and find the middle node in the linked list. inFile...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • Linked List in Java The data node should be modeled somewhat like this: class node{ int...

    Linked List in Java The data node should be modeled somewhat like this: class node{ int node iNum; node next; } Write a program that creates a linked list and loads it with the numbers 0 to 9. Start with an empty list and then use a "for loop" to fill it. Create a linked list class, a node class, etc. Routines like makeNode and findTail should be methods in the linked list class. Create a showList function to display...

  • python Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item,...

    python Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item, left, and rightL. Left link points to the previous node in the list, right link points to the next node in the list. You can also add the display method to this class (like we did it in class for the ListNode class). Then test your class. For example, create a linked list of 5 values: 34,1, 23, 7, and 10. Display it. Then...

  • In Java The following Java implementation of a class Node is given: private class Node<Object> {...

    In Java The following Java implementation of a class Node is given: private class Node<Object> { Node() { this(null, null); } Node(Object d) { this(d, null); } Node(Object d, Node n) { data = d; next = n; } Object data; Node next; } Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a reference to the header node. Using the class Node described above, write a MySingleLinkedList...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • Q2 [15 pts] Linked list: For this question use class Node.java Assume this file implements a...

    Q2 [15 pts] Linked list: For this question use class Node.java Assume this file implements a node in a linked list. It has the following public data members: ➢ int data ➢ Node next Write class Node and Class single linked list then write the following functions: 2.2 Remove duplicates from an unsorted linked list Write a Java method removeDuplicates() which takes a list and deletes any duplicate nodes from the list. The list is not sorted. Example: if the...

  • Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list -...

    Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list - Reference: chapter 18: Create an array of 15 student records that should not be sorted Create a liked list of 15 student record nodes. Each node is a node of one student record from the above unsorted array. The list of student records should be sorted by student ID. (Insert function without sort function to create a linked list.) (If you insert correctly, the...

  • Using Java You are given a Node class and a List class: public class Node {...

    Using Java You are given a Node class and a List class: public class Node {    int   data;     Node next; } public class List {     Node first; } You are also given a Stack class. The following functions are available for use: public class Stack { public boolean isEmpty(){}; public void push(int n){}; public int pop(){};} Write a Java method snglyRevToStck that pushes the data found in a linked list t in reverse order into the stack...

  • Write a Python function to implement the quick sort algorithm over a singly linked list. The...

    Write a Python function to implement the quick sort algorithm over a singly linked list. The input of your function should be a reference pointing to the first node of a linked list, and the output of your function should also be a reference to the first node of a linked list, in which the data have been sorted into the ascending order. (You may use the LinkedQueue class we introduced in the lecture directly in your program.)

  • Problem Statement This problem provides you with a linked list composed of node objects chained together...

    Problem Statement This problem provides you with a linked list composed of node objects chained together via node pointers. Each node has a next pointer which points to the next node in the chain. The last node is identified by having a NULL (or nullptr) next pointer. The linked lists for this problem store string data. Your task: identify the longest string stored in the linked list. If two strings are of the same length, return the string that occurred...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT