void reverse(Stack **top) { if(!isEmpty(top)) { int temp = pop(top); reverse(top); insertAtBottom(top,temp); } }
void reverseStack(struct Stack *s) { if(isEmpty(s)) return; int data = pop(s); reverseStack(s); insertAtBottom(s, data); }
void insertAtBottom(Stack **top, int x) { if(isEmpty(top)){ push(top,x); } else { int item = pop(top); insertAtBottom(top, x); push(top,item); } }
void reverse(struct sNode ** top_ref) { if(!isEmpty(*top_ref)) { int temp = pop(top_ref); reverse(top_ref); insertAtBottom(top_ref,temp); } }
void insertAtBottom(struct Stack *s, int data) { if (isEmpty(s)) { push(s, data); return; } int ele = pop(s); insertAtBottom(s, data); push(s,ele); }
// Reverse the given stack using insertAtBottom void reverse(struct sNode** top_ref) { if (!isEmpty(*top_ref)) { /* Hold all items in function call stack until we reach end of the stack */ int temp = pop(top_ref); reverse(top_ref); /* Insert all the items (held in function call stack) one by one from the bottom to top. Every item is inserted at the bottom*/ insertAtBottom(top_ref, temp); } }
void insertAtBottom(struct sNode ** top_ref,int item) { if(isEmpty(*top_ref)) { push(top_ref,item); } else { int temp = pop(top_ref); insertAtBottom(top_ref,item); push(top_ref,temp); } }
// Recursive function that inserts an element // at the bottom of a stack void insertAtBottom(struct sNode** top_ref, int item) { if (isEmpty(*top_ref)) push(top_ref, item); else { /* Hold all items in function call stack until we reach end of the stack. when the stack becomes empty, the isEmpty(*top_ref) becomes true, the above if part is executed and the item is inserted at the bottom */ int temp = pop(top_ref); insertAtBottom(top_ref, item); /* Once the item is inserted at the bottom, push all the items held in function call stack */ push(top_ref, temp); } }