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
C Programming Language
Starting Out with Python (4th Edition)
Web Development and Design Foundations with HTML5 (8th Edition)
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Starting Out With Visual Basic (7th Edition)
- 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_forwarda) Write a function to get the value of the Nu node in a Linked List. [Note: The first (N=1) item in the list means the item at index 0.] It takes two parameters: the list or its head, and N. Return False if the list has fewer than N elements. The Linked List structure supports the following function. def getlead(self): return selt.head # it points to a Node structure The Node structure supports the following functions. def getData(self): return self.data # it returns the value stored in the Node def getNext(self): return self next # it points to the next Node b) Write a function that counts the number of times a given integer occurs in a Linked List. Assume similar structures as defined in 1.arrow_forward
- scheme: 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_forwardcode in c programarrow_forwardMost common names Write a function mostCommonName(L:List[str]) -> List[str] that takes a list of names (such as ["Jane", "Aaron", "Cindy", "Aaron"], and returns the most common name in this list (in this case, "Aaron"). If there is more than one such name, return a list of the most common names, in any order. That is, mostCommonName(["Jane", "Aaron", 'Jane", "Cindy", "Aaron"]) returns, for example, the list ["Aaron", "Jane" ]. If the input list is empty, return an empty list. Also, names are case- sensitive i.e. "Jane" and "JANE" are different names. assert set (mostCommonName(["Jane", "Aaron", "Jane", "Cindy", "Aaron"])) == set ( ["Ai assert set (mostCommonName(["A", "B", "C", "a", "B"])) set(["B"]) assert mostCommonName([]) == [] Your Answer: 1 # Put your answer here 2 Submit ==arrow_forward
- Listdiff Use the built-in higher order functions2 and write a non-recursive function val listdiff : ’a list -> ’a list -> ’a list = that, given 2 lists, returns a third, containing elements of the first list that are not in the second list. # listdiff [1;2;2;6;3;9;1;4;6] [2;3]; Using OCamlarrow_forwardC++arrow_forwardIf you have the following node declaration: struct Node { int number; struct Node * next; }; typedef struct Node node; node *head,*newNode; Write a C program that contains the following functions to manipulate this linked list : 1. First function: Adding the odd numbers to the beginning of the list and even numbers to the end of the list until -1 is entered from keyboard. 2. Second function using given prototype below. This function cuts the last node of the list and adds it to the beginning as first node. It takes beginning address of the list as a parameter and returns the updated list. node* cutlastaddhead(node* head); 3. 3° function deletes the element in the middle of the list (free this memory location) (if the list has 100 or 101 elements, it will delete the 50th element). The function will take a list as a parameter and return the updated list. 4. 4th function named changeFirstAndLast that swaps the node at the end of the list and the node at the beginning of the list. The…arrow_forward
- TOPIC: Singly Linked ListImplement the following functions using C++ int add(int num) This will add the element num into the linked list and return the position of the newly-added element. In the above example, having add(60) will return 5 as it is the fifth position in the list. int remove(int num) This will remove the first instance of the element and return the position of the removed element. In the above example, having remove(40) will return 3 as 40 was the third element in the linked list before having removed. int get(int pos) This method will get the element at the specified position. This will return -1 if the specified position is invalid. bool addAt(int num, int pos) This method will add the integer num to the posth position of the list and returns true if the value of pos is valid. Performing addAt(20, 2) in the example list will add 20 at the 2nd position and the linked list will now look like this: 10 -> 20 -> 30 -> 40 -> 50. When the value of pos is…arrow_forwardExercise 1 Write a function called nested_sum that takes a list of lists of integers and adds up the elements from all of the nested lists. For example: >>> t = [[1, 2], [3], [4, 5, 6]] >>> nested_sum(t) 21arrow_forwardc++,CPP,C++ Language (Linked List) Implement the singly linked list of Employee with data members Name, id andsalary. Add at least 5 nodes in the list. You will create a pointer named as head tokeep the address of first node of list. Create the following functions:arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning