Question

In: Computer Science

C++ language. struct Node {    int data;    Node *next; } Write a function to...

C++ language.

struct Node

{

   int data;

   Node *next;

}

Write a function to concatenate two linked lists. Given lists A* = (4, 6) and B* = (3, 7, 12), after return from Concatenate_Lists(Node A*, Node B*) the list A should be changed to be A = (4, 6, 3, 7, 12). Your function should not change B and should not directly link nodes from A to B (i.e. the nodes inserted into A should be copies of the nodes from B.)

Write a function to reverse the nodes in a linked list. The reverse function rearranges the nodes in the list so that their order is reversed. You should do this without creating or destroying nodes.

The function prototype:          void reverse_list(Node*& head);

Write a function that returns the minimum data value in a linked list. Given a linked list A = (4, 5, 10, -1, 0), the function int Find_Max(Node * head) shall return -1.

Write a function to split a linked list into two linked list. Given lists A* = (4, 6, 3, 9), your function shall create new linked lists, A1* = (4, 6) and A2* = (3, 9), after that your function should delete the input linked list A. If the size of A is odd, then list A1* is greater than list A2* by one value. Print out A before splitting, A1, A2, and A after splitting. Your function prototype: void Split_List(Node*& head).

Solutions

Expert Solution

code in C++ with comments (code to copy)


#include <bits/stdc++.h>
using namespace std;

struct Node{
   int data;
   Node *next;
};

Node* Concatenate_Lists(Node *A, Node *B){
        if(B==NULL)
        return A;
        //make a copy of B
        Node *head=new Node();
        head->next=NULL;
        head->data=B->data;
        Node *temp=head;
        while(B->next!=NULL){
                temp->next=new Node();
                temp->next->next=NULL;
                temp->next->data=B->next->data;
                temp=temp->next;
                B=B->next;
        }
        if(A==NULL){
                return head;
        }
        temp = A;
        while(temp->next!=NULL)
                temp=temp->next;
        temp->next=head;
        return A;
}

void reverse_list(Node *&head){
        Node* current = head; 
        Node *prev = NULL, *next = NULL; 
        while (current != NULL) { 
                next = current->next; 
                current->next = prev; 
                prev = current; 
                current = next; 
        } 
        head = prev; 
}

//this  returns the minimum data value in a linked list
//list must have at leadt one element
int Find_Max(Node * head){
        int val = head->data;
        while(head->next!=NULL){
                val = min(val, head->data); 
                head=head->next;
        }
        return val;
}
void printList(Node* head){
        Node* temp=head;
        while(temp!=NULL){
                cout<<temp->data<<" ";
                temp=temp->next;
        }
        cout<<endl;
}
void Split_List(Node*& head){
        //print A before splitting
        cout<<"List A: ";
        printList(head);
        int len=0;
        Node* temp=head;
        while(temp!=NULL){
                temp=temp->next;
                len++;
        }
        int a1_size = (len+1)/2;
        int a2_size = len-a1_size;
        Node* A1=NULL, *A2=NULL;
        temp = A1;
        while(a1_size--){
                if(temp==NULL){
                        A1 = new Node();
                        A1->next=NULL;
                        A1->data=head->data;
                        temp=A1;
                }else{
                        temp->next=new Node();
                        temp->next->next=NULL;
                        temp->next->data=head->data;
                        temp=temp->next;
                }
                Node *to_be_deleted = head;
                head=head->next;
                delete to_be_deleted;
        }
        temp=A2;
        while(a2_size--){
                if(temp==NULL){
                        A2 = new Node();
                        A2->next=NULL;
                        A2->data=head->data;
                        temp=A2;
                }else{
                        temp->next=new Node();
                        temp->next->next=NULL;
                        temp->next->data=head->data;
                        temp=temp->next;
                }
                Node *to_be_deleted = head;
                head=head->next;
                delete to_be_deleted;
        }
        //print A before splitting
        cout<<"List A1: ";
        printList(A1);
        //print A2 before splitting
        cout<<"List A2: ";
        printList(A2);
}
//helper method to create list from array
Node* createListFromArray(int arr[], int n){
        Node* head = new Node();
        head->next=NULL;
        head->data=arr[0];
        Node *temp=head;
        for(int i=1;i<n;i++){
                temp->next=new Node();
                temp->next->next=NULL;
                temp->next->data=arr[i];
                temp=temp->next;
        }
        return head;
}
int main(){
        //Part 1
        int arr1[] = {4,6};
        Node* A = createListFromArray(arr1,2);
        cout<<"A : ";
        printList(A);
        int arr2[] = {3,7,12};
        Node* B = createListFromArray(arr2,3);
        cout<<"B : ";
        printList(B);
        cout<<"calling A = Concatenate_Lists(A, B)"<<endl;
        A = Concatenate_Lists(A, B);
        cout<<"A : ";
        printList(A);
        cout<<"B : ";
        printList(B);
        cout<<endl;

        //Part 2
        cout<<"A : ";
        printList(A);
        cout<<"calling reverse_list(A)"<<endl;
        reverse_list(A);
        cout<<"A : ";
        printList(A);
        cout<<endl;

        //part 3
        int arr3[] = {4, 5, 10, -1, 0};
        A = createListFromArray(arr3,5);
        cout<<"A : ";
        printList(A);
        cout<<"calling max = Find_Max(A) to return minimum data value in the linked list"<<endl;
        int max = Find_Max(A);
        cout<<"max = "<<max<<endl<<endl;

        //part 4
        int arr4[] = {4, 6, 3, 9};
        A = createListFromArray(arr4,4);
        cout<<"A : ";
        printList(A);
        cout<<"calling Split_List(A)"<<endl;
        Split_List(A);
}

Code screenshot

Console Output Screenshot

Let me know in the comments if you have any doubts.
Do leave a thumbs up if this was helpful.


Related Solutions

Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function void list_head_insert(NodePtr& head, int entry); The function should insert a new Node, in which entry is the value of attribute item, in front of the linked list that is pointed by head. 2. Write function void list_head_remove(NodePtr& head); The function will remove the first node from the linked list that is pointed by head. 3. Write function NodePtr list_search(NodePtr head, int target); The function...
Below is for C language typedef struct _node { Node *next; char *str; } Node; You...
Below is for C language typedef struct _node { Node *next; char *str; } Node; You are given a function that takes in two Node* pointers to two different linked lists. Each linked list node has a string represented by a character array that is properly null terminated. You're function is supposed to return true if the concatenation of strings in a linked list match each other, else return false. EXAMPLES List 1: "h" -> "el" -> "l" -> "o"...
Assume that struct Node{        int item;        Node* link; }; Write function void list_remove(NodePtr& prev_ptr);...
Assume that struct Node{        int item;        Node* link; }; Write function void list_remove(NodePtr& prev_ptr); The function will remove the node after the node pointed by prev_ptr. c++
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions...
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions below. a) int getSmallest(Node * r); // return smallest value in the BST with root r. Assume r not null. b) int getSecondSmallest(Node * r); // return 2nd smallest value in BST with root r. Assume r not null and r has a nonnull left or right child. c) void removeSecondSmallest(Node * r); // remove 2nd smallest value in BST with root r. Assume...
Please do this code with python. Thank you! struct Node {     int data;     struct...
Please do this code with python. Thank you! struct Node {     int data;     struct Node* left;     struct Node* right; }; // Node creation struct Node* newNode(int data) {     struct Node* nn         = new Node;     nn->data = data;     nn->left = NULL;     nn->right = NULL;     return nn; } // Function to insert data in BST struct Node* insert(struct Node* root, int data) {   if (root == NULL)         return newNode(data);     else {...
Consider the following struct that represents a node within a binary tree: struct Node { int...
Consider the following struct that represents a node within a binary tree: struct Node { int data; // Data of interest Node *left // Link to left subtree (nullptr if none) Node *right ; // Link to right subtree (nullptr if none) }; Complete the following function that computes the number of elements in a binary tree: // Counts the number of elements in the binary tree to which t points. // Returns the number of elements. int size(Node *t)...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*);...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*); void showList(node*); void printListBackwards(node *); int main(void) { node *list1; printf("\n Create a sorted list.."); printf("\n Enter values for the first list (-999 to end):"); list1=(node*)malloc(sizeof(node*)); //Allocate memory for the list node insert(list1); //insert values by calling the function insert showList(list1); //display values entered by user printf("\n After recursively reversing the list is :\n"); printListBackwards(list1); //print the values in reverse order using the function...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT