answersLogoWhite

0


Best Answer

public void reverse(Stack st)

{

int m = (int)st.Pop();

if (st.Count != 1)

reverse(st);

Push(st , m);

}

public void Push(Stack st , int a)

{

int m = (int)st.Pop();

if (st.Count != 0)

Push(st , a);

else

st.Push(a);

st.Push(m);

}

User Avatar

Wiki User

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

Hi,

The basic idea implementing the stack using recursive function calling is simple. while calling a function in c++ the function parameters are being push on to the internal stack.

So we will use this same concept and I'll show you how we can achieve this stack functionality using recursive function call in C++.

int n=0;

int arr[4]={1,2,3,4};

int stack(int a)

{

if(n<4)

{

n++;

cout<

}

return a;

}

int main() { stack(arr[0]);

}

Here, I'm passing the elements of stack to the function 'stack' and returning the same value from the function. Means the last value we 'push' onto the stack will come first from the stack using 'return a'.

The array input is 1,2,3,4 and output will be 4,3,2,1 which is exactly LIFO which is the primary goal of stack.

Feel free to contact me on ashisht4u@rediffmail.com if you need any other clarification on the same.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Implementation of stack using recursion
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can you implement merge sort without using recursion?

Sure, recursion can always be substituted with using a stack.


Is Recursion operation of the stack?

yes


Can you provide a solution to the diamond-square algorithm using Java and recursion?

Yes. It is possible to provide a solution to the diamond-square algorithm using Java and recursion.


C program to implement tower of hanoi using array implementation of stack abstract datatype?

stack abstract datatype


What is the use of recursion function?

Read the part in your programming manual/text book about recursion. The short answer while easy does not tell you anything about the power or dangers of recursion. It is the power and dangers of recursion that is important because once understood you can use recursion to good effect without running out of stack space.


Is there any way to perform quick sort other than stack?

Stack is not a way to perform quicksort, it is a tool used to implement recursion.


What is funcation?

Read the part in your programming manual/text book about recursion. The short answer while easy does not tell you anything about the power or dangers of recursion. It is the power and dangers of recursion that is important because once understood you can use recursion to good effect without running out of stack space.


Can heap implement recursion?

Heap is a data-structure, it cannot implement anything. On the other hand, it is true that: 1. Recursive routines might use heap. 2. You can use dynamic memory allocation (heap), to implement a stack; and use the stack to implement recursion.


Is cursor implementation possible in queue or stack?

yes,cursor implementation possible in priority queue.


How do you overcome limitations of stacks in polygon filling?

You overcome limitations of the stack in polygon filling, or in any other algorithm, far that matter, but using an iterative technique, rather than a recursive technique. Recursion is quite useful, and can simplify algorithm design. Polygon filling, however, is a class of algorithm can potentially have a very deep recursion depth. This causes stress on the stack, hence the need for iteration.


How do you choose between recursion and iteration?

Some problems cry out for recursion. For example, an algorithm might be defined recursively (e.g. the Fibonacci function). When an algorithm is given with a recursive definition, the recursive implementation is straight-forward. However, it can be shown that all recursive implementations have an iterative functional equivalent, and vice versa. Systems requiring maximum processing speed, or requiring execution within very limited resources (for example, limited stack depth), are generally better implemented using iteration.


What do you mean by base case recursive case binding time runtime stack and tail recursion?

These terms are found in Recursion.1.Base Case:it is the case in recursion where the answer is known,or we can say the termination condition for a recursion to unwind back.For example to find Factorial of num using recursion: int Fact(int num){ if(num==1 num==0)//base casereturn 1;else // recursive case: return num*Fact(num-1);} 2.Recursive case:It is the case whcih brings us to the closer answer. Run Time Stack:It is a system stack us to save the frame stack of a function every recursion or every call.This frame stack consists of the return address,local variables and return value if any. Tail Recursion:The case where the function consist of single recursive call and it is the last statement to be executed.A tail Recursion can be replace by iteration. The above function consists of tail recursion case.where as the below function does not. void binary(int start,int end,int el){int mid;if(end&gt;start){mid=(start+end)/2;if(el==ar[mid])return mid;else{if(el&gt;ar[mid])binary(mid+1,end,ele);elsebinary(start,mid-11,ele);