Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added Stack reverse #1816

Merged
merged 1 commit into from
Oct 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import java.util.Stack;

class Test {

// using Stack class for
// stack implementation
static Stack<Character> st = new Stack<>();

// Below is a recursive function
// that inserts an element
// at the bottom of a stack.
static void insert_at_bottom(char x)
{

if (st.isEmpty())
st.push(x);

else {

// All items are held in Function
// Call Stack until we reach end
// of the stack. When the stack becomes
// empty, the st.size() becomes 0, the
// above if part is executed and
// the item is inserted at the bottom
char a = st.peek();
st.pop();
insert_at_bottom(x);

// push allthe items held
// in Function Call Stack
// once the item is inserted
// at the bottom
st.push(a);
}
}

// Below is the function that
// reverses the given stack using
// insert_at_bottom()
static void reverse()
{
if (st.size() > 0) {

// Hold all items in Function
// Call Stack until we
// reach end of the stack
char x = st.peek();
st.pop();
reverse();

// Insert all the items held
// in Function Call Stack
// one by one from the bottom
// to top. Every item is
// inserted at the bottom
insert_at_bottom(x);
}
}

// Driver Code
public static void main(String[] args)
{

// push elements into
// the stack
st.push('1');
st.push('2');
st.push('3');
st.push('4');

System.out.println("Original Stack");

System.out.println(st);

// function to reverse
// the stack
reverse();

System.out.println("Reversed Stack");

System.out.println(st);
}
}