In C++ Assuming: struct node { int data; node * next; }; and copyList(node * head);
Write a recursive function (copyList(node * head)) that will create a copy of the singly linked list.
#include <iostream> using namespace std; struct node { int data; node * next; }; node *copyList(node * head) { if(head == NULL) { return NULL; } else { node *copy = new node; copy->data = head->data; copy->next = copyList(head->next); return copy; } } int main() { node *original_list = new node; original_list->data = 5; original_list->next = new node; original_list->next->data = 2; original_list->next->next = new node; original_list->next->next->data = 9; original_list->next->next->next = NULL; node *copy_list = copyList(original_list); node *temp = copy_list; while (temp != NULL) { cout << temp->data << " "; temp = temp->next; } cout << endl; return 0; }
In C++ Assuming: struct node { int data; node * next; }; and copyList(node * head);...
Write a function with the following prototype struct node* copyList(struct node* 1ist) The struct node is the same used in question 18. The function should create a new, separate copy of the linked list pointed to by the list parameter and return a pointer to the head of the newly created linked list copy.
13. Given the following structure struct node { struct node *next; int id; }; Write a code to insert a new node into the linked list as the last node of the linked list struct node *insertLast(stuct node **head, int newId) { } 14. Given the following structure struct node { struct node *next; int id; }; Write a code to insert a new node into the linked list after a node with the same id as the input parameter...
Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...
Structure struct Node int Manth; // Mont h double dAvg: 1/ Average struct Node pNext // with, the linked İist 3hown above the function will return gven that the average is 3.8 Ptr to next -Node; Ret (3,3.8) (4,2.5) (20pts)( Recursive function) Show the code for a function that receives a pointer to the head of an ordered singly linked list that uses the structure in the top left. The function will return the pointer node that shows the highest...
Submit a source.cpp file with the following: struct node { int data; node* next; } a for loop that creates a singly linked list of nodes with the values 0-9 stored in them. a for loop that prints off the data of the singly linked list, each one on its own line ******************************************************************************************
C programming Write an iterative and recursive version of a function to print out a linked list from head to tail and then do the same for printing a linked list from tail to head. Assume a singly linked list in all cases and access only to a head pointer at the time of the function call. struct node; typedef struct node Node; struct node int data; Node next;
A linked list of integers is built using the following struct: struct node { int data; struct node *next; }; Define a function named max that returns the maximum integer in a list. The function takes one arguments, a pointer to the head of the list. The function returns an integer, which is the maximum value. If the list is empty, return zero. NOTE: You know nothing about the values in the list. They could all be negative!
Consider the following C++ statements: struct node Type int info; node Type *link; nodeType *head, *p, q, *newNode; newNode = new node Type; 1. Write C++ statement to store 50 in the info field of the newNode. 2. Write C++ statement to set the link field of the newNode to NULL. 3. Write C++ statement to make head pointer points to newNode. 4. Write C++ statement to delete the first node in the linked list. (the first an the only...
C++ Consider the following structure of node and linked list. struct Node { int key; Node *next; }; 10 -> 20 -> 30 -> 10 -> 10 -> 50 -> 10 -> NULL What will be the output of following pseudo-code? Consider head is the pointer to the first node of above linked list. Node *walker = head; int count = 0; while(walker!= NULL && count < 3) { if(walker->key == 10) { count = count + 1; } walker...
C++ Assume that you have a structure Node as follows: struct Node { int value; Node* next; Node(int v, Node* n = nullptr) { // constructor value = v; next = n; } }; Question 1 (20 points) Write a function "count" that counts the number of elements in a given linked list. The prototype of this function is as follows: int count_elements(Node*); Question 2 (20 points) Write a function “average" that calculates the average of all the elements in...