File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import java .util .*;
2+ import java .util .Stack ;
23class stackReverse {
34
5+ static Stack <Character > s =new Stack <>();
6+ static void reverse ()
7+ {
8+ if (s .isEmpty ())
9+ {
10+ return ;
11+ }
12+ char a =s .pop ();
13+ reverse ();
14+ insertBottom (a );
15+ }
16+ static void insertBottom (char a )
17+ {
18+ if (s .isEmpty ())
19+ {
20+ s .push (a );
21+ return ;
22+ }
23+ char b =s .pop ();
24+ insertBottom (a );
25+ s .push (b );
26+
27+ }
428 public static void main (String [] args ) {
529
6- Stack <Character > s = new Stack <Character >();
730 Scanner in = new Scanner (System .in );
8- String ans = "" , data = in .nextLine ();
31+ // String ans = "",
32+ String data = in .nextLine ();
933
10- for (char element : data .toCharArray ()) {
34+ for (char element : data .toCharArray ())
35+ {
1136 s .push (element );
1237 }
13- for (char element : data .toCharArray ()) {
14- ans += s .pop ();
15- }
16- System .out .print (ans );
38+ reverse ();
39+ // for (char element : data.toCharArray()) {
40+ // ans += s.pop();
41+ // }
42+ System .out .print (s );
1743 }
1844
1945}
You can’t perform that action at this time.
0 commit comments