Question

In: Computer Science

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 any other data structure. You can assume n is a positive integer and that the stack contains at least n elements.

1) I'd like to know what is meant by a "help stack", please and how should I avoid using it?

2) Also, if I used while and for loops would that fine or is that considered a data structure?

If you could assist me in writing this code too, it will be appreciated

Solutions

Expert Solution

1. By help stack , it means that we cannot use any other stack object for finding the nth element except the stack object received by the function

2. No while and for loops are not considered as data structure. These are looping statements. But in recursion we do not use loops. The work of loops is achieved by using recursion

// C++ recursive function to find the Nth element from the top of a stack.

// Assuming stack has a method Pop() that removes and returns the top element of the stack
// Assuming stack has a method Push() that inserts the element at top of the stack
template <class Type>
Type getNth(stack<Type> & s, int n)
{
   // if we have not reached the nth element
   if(n > 1)
   {
       T top = s.Pop(); // remove and get the current top element of the stack
       T a = getNth(s,n-1); // call the getNth with parameters s and n-1
       s.Push(top); // push the current top back to stack
       return a; // return the element returned by getNth
   }
   else // if we have reached the nth element
   {
       T top = s.Pop(); // remove and get the top element of the stack
       s.Push(top);// push the top element back to the stack
       return top; // return this top element
   }
}


Related Solutions

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...
PYTHON: Write a recursive function named linear_search that searches a list to find a given element....
PYTHON: Write a recursive function named linear_search that searches a list to find a given element. If the element is in the list, the function returns the index of the first instance of the element, otherwise it returns -1000. Sample Output >> linear_search(72, [10, 32, 83, 2, 72, 100, 32]) 4 >> linear_search(32, [10, 32, 83, 2, 72, 100, 32]) 1 >> linear_search(0, [10, 32, 83, 2, 72, 100, 32]) -1000 >> linear_search('a', ['c', 'a', 'l', 'i', 'f', 'o', 'r',...
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)
From the following mySql query create a function that returns a nth value of a table:...
From the following mySql query create a function that returns a nth value of a table: SELECT name, (SUM(price * quantity)) AS TotalPrice FROM Items NATURAL JOIN Inventory GROUP BY name ORDER BY (TotalPrice) DESC LIMIT 1, OFFSET 1; Shema: Inventory(name: String,item:String,quantity:integer) Items(item: String, brand: String, price:double)
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
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
Problem 3: Minimum In this problem, we will write a function to find the smallest element...
Problem 3: Minimum In this problem, we will write a function to find the smallest element of a list. We are, in a sense, reinventing the wheel since the min() function already performs this exact task. However, the purpose of this exercise is to have you think through the logic of how such a function would be implemented from scratch. Define a function named minimum(). The function should accept a single parameter named x, which is expected to be a...
Question I'm having trouble with: Using LISP, write a recursive function that takes a list and...
Question I'm having trouble with: Using LISP, write a recursive function that takes a list and returns the number of times the symbol 'a' occurs in it. Note: do not count a's that might occur in a sublist within the list.
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.
Write a MATLAB function, called arbpoly, that computes a polynomial arbitrary nth degree. The function will...
Write a MATLAB function, called arbpoly, that computes a polynomial arbitrary nth degree. The function will take 2 inputs: 1) the first input will be a row vector, c, containing the coefficients of the polynomial, starting with the coefficient of the highest - degree term; 2) the second input will be a scalar, x, which is a real number at which the polynomial will be evaluated. The function's only output, y, will be the scalar value of the polynomial computed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT