Question

using the single linked list code written in the c With the main function please
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//The main class
class MergeList {

Node head; // head of list
static Node a, b;

//Node class
static class Node {

int element;
Node next;

// Constructor to create a new node
Node(int d) {
element = d;
next = null;
}
}

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

Node sortMerge(Node node1, Node node2) {

// Here we will check whether both the list are null or not.
if (node1 == null && node2 == null) {
return null;
}

// sortResultant node
Node sortRes = null;

// Now we will compare and merge the elements of both the list.
while (node1 != null && node2 != null) {

// We will compare current element of both lists
if (node1.element >= node2.element) {
Node temp = node1.next;
node1.next = sortRes;
sortRes = node1;
node1 = temp;
} else {
Node temp = node2.next;
node2.next = sortRes;
sortRes = node2;
node2 = temp;
}
}

// If second list reached end, but first list has
// nodes. Add remaining nodes of first list at the
// front of sortResult list
while (node1 != null) {
Node temp = node1.next;
node1.next = sortRes;
sortRes = node1;
node1 = temp;
}

// If first list reached end, but second list has
// node. Add remaining nodes of first list at the
// front of sortResult list
while (node2 != null) {
Node temp = node2.next;
node2.next = sortRes;
sortRes = node2;
node2 = temp;
}

return sortRes;

}

public static void main(String[] args) {

MergeList list = new MergeList();
Node sortResult = null;

//Create two linked list
list.a = new Node(5);
list.a.next = new Node(10);
list.a.next.next = new Node(15);

list.b = new Node(2);
list.b.next = new Node(3);
list.b.next.next = new Node(20);

System.out.println("List a before merge :");
list.printlist(a);
System.out.println("");
System.out.println("List b before merge :");
list.printlist(b);

// Cal the function to merge the list in increasing order.
sortResult = list.sortedmerge(a, b);
System.out.println("");
System.out.println("Merged linked list : ");
list.printlist(sortResult);

}
}

Add a comment
Know the answer?
Add Answer to:
With the main function please using the single linked list code written in the class or...
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
  • JAVA - Write a function in your Main class LinkedList<E> mergeLists(LinkedList<E> list1, LinkedList<E> list2) - given...

    JAVA - Write a function in your Main class LinkedList<E> mergeLists(LinkedList<E> list1, LinkedList<E> list2) - given two ordered linked lists of integers, merge the two lists into a single LinkedList whose elements are in sorted order. You should create a new LinkedList and add the values from list1 and list2 into the new list in sorted order. Do not modify list1 or list 2. list1 => 1 -> 3 -> 7 -> 8, list2 => 2 -> 5 -> 7...

  • 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...

  • c++ 10. Code the template function merge, which merges two ranges into a single output container. The first two argu- ments indicate one range and the next two indicate the other range. The last argu...

    c++ 10. Code the template function merge, which merges two ranges into a single output container. The first two argu- ments indicate one range and the next two indicate the other range. The last argument is a container witha push back function. The input ranges are sorted into ascending order and operatorc is available to compare el- ements of these ranges. The output container should also be sorted into the same order. If an element from one container is equal...

  • Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

  • Please solve using java. 21. Merge Two Sorted Lists Easy 2705 396 ♡ Favorite Sha *...

    Please solve using java. 21. Merge Two Sorted Lists Easy 2705 396 ♡ Favorite Sha * Definition for singly-linked list. * public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. class Solution { public ListNode merge TwoLists(ListNode li, ListNode 12) { 10 Example: Input: 1->2->4, 1-3-4...

  • linked list operation /*************************************************************************************** This function creates a new node with the information give as a...

    linked list operation /*************************************************************************************** This function creates a new node with the information give as a parameter and looks for the right place to insert it in order to keep the list organized ****************************************************************************************/ void insertNode(string first_name, string last_name, string phoneNumber) { ContactNode *newNode; ContactNode *nodePtr; ContactNode *previousNode = nullptr; newNode = new ContactNode; /***** assign new contact info to the new node here *****/ if (!head) // head points to nullptr meaning list is empty { head = newNode;...

  • Generic Linked Lists ( Program should be written in Java language). Modify the linked list class...

    Generic Linked Lists ( Program should be written in Java language). Modify the linked list class presented in this chapter so it works with generic types. Add the following methods drawn from the java.util.List interface: -void clear(): remove all elements from the list. -E get(int index): return the element at position index in the list. -E set(int index, E element): replace the element at the specified position with the specified element and return the previous element. Test your generic linked...

  • 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...

  • ***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...

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

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