1- Assume that we have a stack (s1) that contains some letters as follows: s1.push ('r'); s1.push ('a'); s1.push ('d'); s1.push ('a'); s1.push ('r'); Assume that we have another stack (s2) that contains some numbers and a character 'X' as follows: s2.push (3); s2.push (5); s2.push (8); s2.push ('X'); s2.push (4) ; s2.push (14); Write a Java main program that inserts the elements of stack s1 into the stack s2 where the char 'X' exists. After insertion, print the stack s2 only. At the end of the program, the stack s1 can be empty, but the stack s2 must contain the numbers and characters.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 21SA
icon
Related questions
Question
1- Assume that we have a stack (s1) that contains some letters as follows:
s1.push ('r');
s1.push ('a');
s1.push ('d');
s1.push ('a');
sl.push ('r') ;
Assume that we have another stack (s2) that contains some numbers and a character 'X' as follows:
s2.push (3);
s2.push (5);
s2.push (8);
s2.push ('X');
s2.push (4) ;
s2.push (14);
Write a Java main program that inserts the elements of stack s1 into the stack s2 where the char 'X' exists.
After insertion, print the stack s2 only.
At the end of the program, the stack s1 can be empty, but the stack s2 must contain the numbers and characters.
Example 1:
Example 2:
top
top
s2:
5 16 x 9 7 7 12
s2:
8 7 14 12 10 10 8 x 6 12
sl:
dery a
sl:
a l go
top
top
s2: 5 16 d ery a 9 77 12
s2:
87 14 12 10 10 8 а 1 goб 12
Notes:
- You must use ONLY stack data structure. Don't use other different data structures like string, normal (pure) array, queue, array list, SLL, or DLL.
Don't write any other method in the Stack class. All methods must be written in the main program.
Transcribed Image Text:1- Assume that we have a stack (s1) that contains some letters as follows: s1.push ('r'); s1.push ('a'); s1.push ('d'); s1.push ('a'); sl.push ('r') ; Assume that we have another stack (s2) that contains some numbers and a character 'X' as follows: s2.push (3); s2.push (5); s2.push (8); s2.push ('X'); s2.push (4) ; s2.push (14); Write a Java main program that inserts the elements of stack s1 into the stack s2 where the char 'X' exists. After insertion, print the stack s2 only. At the end of the program, the stack s1 can be empty, but the stack s2 must contain the numbers and characters. Example 1: Example 2: top top s2: 5 16 x 9 7 7 12 s2: 8 7 14 12 10 10 8 x 6 12 sl: dery a sl: a l go top top s2: 5 16 d ery a 9 77 12 s2: 87 14 12 10 10 8 а 1 goб 12 Notes: - You must use ONLY stack data structure. Don't use other different data structures like string, normal (pure) array, queue, array list, SLL, or DLL. Don't write any other method in the Stack class. All methods must be written in the main program.
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning