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

write a recursive algorithm to find the maximum element in an array of n elements and...
write a recursive algorithm to find the maximum element in an array of n elements and analyze its time efficiency. (I am using c++ programming language)
Given a stack S and an element x, write a recursive algorithm that returns True if...
Given a stack S and an element x, write a recursive algorithm that returns True if x is in S, or False otherwise. Note that your algorithm should not change the content in S. What is the time complexity of your algorithm?
Given a stack S and an element x, write a recursive algorithm that returns True if...
Given a stack S and an element x, write a recursive algorithm that returns True if x is in S, or False otherwise. Note that your algorithm should not change the content in S. What is the time complexity of your algorithm?
Use java .Given a stack S and an element x, write a recursive algorithm that returns...
Use java .Given a stack S and an element x, write a recursive algorithm that returns True if x is in S, or False otherwise. Note that your algorithm should not change the content in S. What is the time complexity of your algorithm?
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
Write a recursive and an iterative function to calculate the nth element in a Fibonacci sequence....
Write a recursive and an iterative function to calculate the nth element in a Fibonacci sequence. A Fibonacci sequence is defined as the element 1, followed by another 1, and each element thereafter is the sum of the previous two elements. For example, the first 9 elements of a Fibonacci sequence are: 1 2 3 5 8 13 21 34 This famous sequence was originally used to predict the growth of rabbit populations! Once you have each of the functions...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT