Question

In: Computer Science

Write a recursive algorithm replace (start) to replace the value of each element of A with...

Write a recursive algorithm replace (start) to replace the value of each element of A with that of the next element in A. A is a singly linked list.

Solutions

Expert Solution

The solution of the above problem is below:

The screenshot of the code is below:

The output screen:

  

code--

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

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

void replace(struct node*head)
{
if(head->next==NULL)
{ cout<<head->data;
return;
}
else
{
cout<<head->next->data<< " ";
replace(head->next);
}
}
int main()
{
struct node*p,*head=NULL,*prev;
int i,n;
cout<<"Enter size of likned list";
cin>>n;
int s;
cout<<"Enter value in linked list\n";
for(i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
cin>>s;
p->data=s;
p->next=NULL;
if(head==NULL)
{
head=p;
}
else
{
prev->next=p;
}
prev=p;
}
cout<<"values after replacing of the linked list is:\n";
replace(head);

}

please upvote in case of any query comment me.


Related Solutions

In c++ Write a recursive driver function that will replace each of the odd values in...
In c++ Write a recursive driver function that will replace each of the odd values in a stack with the cube of the value.
Describe an efficient recursive algorithm for solving the element uniqueness problem
Describe an efficient recursive algorithm for solving the element uniqueness problem, which runs in time that is at most O(n2) in the worst case without using sorting.    
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of integers. Assume that the List type has members: int List.length returns the length of the list. void List.push(T n) pushes an element n to the front of the list T List.pop() pops an element from the front of the list. List$$ List$$.concat(List$$ other) returns the concatenation of this list with other. Explain in plain English the reasoning behind your algorithm. Power Lists should be...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
Write a RECURSIVE algorithm (different from the ones your provided in 3) implemented in Java a...
Write a RECURSIVE algorithm (different from the ones your provided in 3) implemented in Java a in a complete Java program to reverse a stack of characters using recursion. You are not allowed to use loop constructs like while, for..etc, and you can only use the following functions on Stack S shown below (15pts) Provide an explanation of the running time. You will need to implement your own stack if needed isEmpty(S) push(S) pop(S) Make your own assumption and your...
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays...
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays of integers. Neither list contains duplicates, and the resulting list should not contain duplicates either. Hint: You may want to call a helper function from merge. PROGRAM: C
Write a modification of the recursive binary search algorithm that always returns the smallest index whose...
Write a modification of the recursive binary search algorithm that always returns the smallest index whose element matches the search element. Your algorithm should still guarantee logarithmic runtime. Give a brief discussion of the best- and worst-case runtimes for this new algorithm as they compare to the original. NOTE: You do not have to re-write the entire algorithm. You just need to indicate any changes you would make and show the pseudocode for any portions that are changed. Example: Given...
Given two integers, start and end, where end is greater than start, write a recursive C++...
Given two integers, start and end, where end is greater than start, write a recursive C++ function that returns the sum of the integers from start through end, inclusive.Example: If start is 5 and end is 10 then the function will return: 45 which is sum of 5, 6, 7, 8, 9, and 10. int sum (int start, int end){
Exercises a - b refer to the recursive algorithm SelectionSort (a.) In one part of algorithm...
Exercises a - b refer to the recursive algorithm SelectionSort (a.) In one part of algorithm SelectionSort, the index of the maximum item in a list must be found. This requires comparisons between list elements. In an n-element (unsorted) list, how many such comparisons are needed in the worst case to find the maximum element? How many such comparisons are needed in the average case? (b.) Defining the basic operation as the comparison of list elements and ignoring the amount...
I have the following question: Write a recursive function to find the Nth element from the...
I have the following question: Write a recursive function to find the Nth element from the top of a stack. For example, if N is 3 the function should return the third element in the stack. Use the following header: template <class Type> Type getNth( stack<Type> & s, int n) Although your function may push and pop values from the stack, it must eventually leave the stack in its original state. Your function may NOT use a help stack or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT