Read , Sort , Merge
Using the ListNode structure of Program 17-2, write the following functions:
ListNode *read()
ListNode *sort(ListNode* list1)
ListNode *merge(ListNode* list1, ListNode* list2)
The first function reads a sequence of whitespace-separated positive numbers and forms the numbers read into a linked list of nodes. The input for a sequence of numbers is terminated by - 1. The second function sorts a linked list of nodes and returns a pointer to the head of the sorted lists. The function should sort by rearranging existing nodes, not by making copies of existing nodes. The third function merges two linked lists that are already sorted into a linked list that is also sorted. The merge function should not make copies of nodes: Rather, it must remove nodes from the two lists and form those nodes into a new list.
Test your functions by having the user enter two sequences of numbers, sorting each sequence, and then merging and printing the resulting list.
Want to see the full answer?
Check out a sample textbook solutionChapter 17 Solutions
STARTING OUT WITH C++ MPL
Additional Engineering Textbook Solutions
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
Starting Out with Python (4th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Degarmo's Materials And Processes In Manufacturing
Electric Circuits. (11th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
- JAVA CODE PLEASE Linked List Practice ll by CodeChum Admin Write a function printNodes that takes in the head of a linked list and prints all the values of that linked list using a while loop. Print the values separated by a [space]->[space] In the main function, write a program that asks the user to input five integers and assign these values to the nodes. Arrange the nodes in ascending order first before printing them using the printNodes function. Input 1. One line containing an integer 2. One line containing an integer 3. One line containing an integer 4. One line containing an integer 5. One line containing an integer Output Enter·number·1:·1 Enter·number·2:·2 Enter·number·3:·3 Enter·number·4:·4 Enter·number·5:·5 1·->·2·->·3·->·4·->·5arrow_forwardJAVA CODE PLEASE Linked List Practice l by CodeChum Admin Write a function printNodes that takes in the head of a linked list and prints all the values of that linked list using a while loop. Print the values separated by a [space]->[space] In the main function, write a program that asks the user to input five integers and assign these values to the nodes then print the five nodes using the printNodes function. An initial code is provided for you. Just fill in the blanks. Input 1. One line containing an integer 2. One line containing an integer 3. One line containing an integer 4. One line containing an integer 5. One line containing an integer Output Enter·number·1:·1 Enter·number·2:·2 Enter·number·3:·3 Enter·number·4:·4 Enter·number·5:·5 1·->·2·->·3·->·4·->·5arrow_forward#### Part 1 Write a Python function (`binary_search`) that implements the binary search algorithm. This function will take a list argument (`values`), a value to search for (`to_find`), a start index (`start_index`), and an end index (`end_index`). The function will return `True` if `to_find` is present in `values`, and `False` otherwise. Below, you will find some code to test your binary search: ```python values = [2,4,6,8,10,12,14,16,18,20] print(binary_search(values, 14, 0, len(values) - 1)) print(binary_search(values, 7, 0, len(values) - 1)) ``` The output of the test code is below: ``` True False ``` #### Part 2 Modify your `binary_search` function to count the number of comparisons (==, <, <=, >, or >=). The function will now return both the number of comparisons made and the `True` or `False` result. The code below calls your function, and creates a simple ASCII bar chart of the number of comparisons (divided by 10, to account for small differences). Some…arrow_forward
- 16-Write a function called find_integer_with_most_divisors that receives a list of integers and returns the integer from the list with the most divisors .In the event of a tie, the first case with the most divisors returns .For example: If the list looks like this: [8, 12, 18, 6] In this list, the number 8 has four divisors, which are ;[1,2,4,8] :The number 12 has six divisors, which are ;[1,2,3,4,6,12] :The number 18 has six divisors, which are ;[1,2,3,6,9,18] :And the number 6 has four divisors, which are : [1,2,3,6]Note that both items 12 and 18 have the maximum number of divisors and are equal in number of divisors (both have 6 divisors) .Your function must return the first item with the maximum number of divisors ;Therefore it must return the following value: 12arrow_forwardWrite the code to create a linked list using c++ Include the following functionality: Insert at the head or tail of the list. Delete one item (based on an entered value) or all of the list. Print out the linked list. Use the following class and struct to create your linked list: #include <iostream> #include <algorithm> using std::cout; using std::endl; using std::swap; struct Node { int m_data; Node* m_next; }; class LList { public: LList(); LList(int data); LList(const LList& copy); LList(LList&& move) noexcept; ~LList(); int getData(); Node* getNext(); void setData(int data); void insertHead(int data); void insertTail(int data); void deleteNode(int data); void setNext(LList* next); LList& operator = (const LList& copy); LList& operator = (LList&& move) noexcept; void printList(); private: Node* head; };arrow_forwardUsing the ListNode structure introduced in this chapter, write a function void printFirst(ListNode *ptr)that prints the value stored in the first node of a list passed to it as parameter. The function should print an error message and terminate the program if the list passed to it is empty.arrow_forward
- please solution in python language and Apply lists and strings an write commentarrow_forwardscheme: Q9: Sub All Write sub-all, which takes a list s, a list of old words, and a list of new words; the last two lists must be the same length. It returns a list with the elements of s, but with each word that occurs in the second argument replaced by the corresponding word of the third argument. You may use substitute in your solution. Assume that olds and news have no elements in common. (define (sub-all s olds news) 'YOUR-CODE-HERE )arrow_forwardlanguage : c++ question : Create a doubly circular linked list. Write a function in doubly Linked List class to find the minimum value in list. Swap values of any two nodes given by user from doubly linked list and show the result. You have to paste code and screenshots of all tasks. Failure to do so will result in minus two marks each.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education