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.    
Develop a recursive algorithm to find the smallest and largest element in an array and trace...
Develop a recursive algorithm to find the smallest and largest element in an array and trace the recursive function with appropriate message. using c++ add comment to the code
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a...
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a specific character from a string b. Write the pseudocode for the program.
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.
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){
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT