0% found this document useful (0 votes)
13 views9 pages

Java Stack Exercises for Students

The document discusses using stacks to solve data structures and algorithms problems. It provides code examples to implement a stack for a card game, reverse a string input using a stack, and reverse a given string using a stack in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views9 pages

Java Stack Exercises for Students

The document discusses using stacks to solve data structures and algorithms problems. It provides code examples to implement a stack for a card game, reverse a string input using a stack, and reverse a given string using a stack in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exercise

Data Structures and Algorithms

IIT 302 - 2

Rajapaksha R. A. N. T. D.
UWU/IIT/18/022
Industrial Information Technology Degree Program
Department of Computer Science and Informatics
Faculty Of Applied Sciences
1. Implement a stack for a card game. Push 5 cards to the stack.

a. Print the stack

b. Remove 1 element from the stack and print

c. Findthestacksize

METHOD 01

Answer:

package dsa;
import [Link].*;
/**
*
* @author nathasha
*/
public class DSA {

public static void main(String[] args) {

Stack<Integer> stk = new Stack<>();


boolean result = stk .empty();
[Link]("Before entering values : " +result);

[Link](1);
[Link](2);
[Link](3);
[Link](4);
[Link](5);

[Link]("\na. Print the stack : " +stk);

[Link]();
[Link]("b. Remove 1 element from the stack and print : " +stk);

[Link]("c. Find the stack size : " +[Link]());

2
3
METHOD 02

Answer:

package dsa;
import [Link].*;
/**
*
* @author nathasha
*/
public class DSA {

public static void main(String[] args) {

Stack<String> stk = new Stack<>();

[Link]("Implement a stack for a card game. Push 5 cards to the stack.


: " );

[Link]("CARD 01");
[Link]("CARD 02");
[Link]("CARD 03");
[Link]("CARD 04");
[Link]("CARD 05");

[Link]("\na. Print the stack : " +stk);

[Link]();
[Link]("b. Remove 1 element from the stack and print : " +stk);

[Link]("c. Find the stack size : " +[Link]());

4
5
2. You are required to write a program input a string through keyboard and output
the reserves of the string using Stack data structure.

E.g.:

Input : Hello

Output : 0lleH

Answer:

package dsa;
import [Link].*;
import [Link];
/**
*
* @author nathasha
*/
public class DSA {

public static String Reverse(String str) {

char [] ReverseString = new char [[Link]()];


Stack <Character> stack = new Stack <Character>();

for (int i=0; i<[Link](); i++) {


[Link]([Link](i));
}

int i=0;
while(![Link]())
{
ReverseString[i++]=[Link]();
}
return new String (ReverseString);
}

public static void main(String[] args)


{
Scanner sc= new Scanner([Link]); //[Link] is a standard input stream
[Link]("Input : ");
String str2= [Link](); //reads string
[Link]("Output : " +Reverse(str2) + " \n " );
}
}
6
7
3. Write a program to reverse a given string input by using a stack in Java?

Answer:

package dsa;
import [Link].*;
import [Link];
/**
*
* @author nathasha
*/
public class DSA {

public static String Reverse(String str) {

char [] ReverseString = new char [[Link]()];


Stack <Character> stack = new Stack <Character>();

for (int i=0; i<[Link](); i++) {


[Link]([Link](i));
}
int i=0;
while(![Link]())
{
ReverseString[i++]=[Link]();
}
return new String (ReverseString);
}

public static void main(String[] args)


{

String str= "Hello. This is Nathasha. IIT18022. ";


[Link]("Write a program to reverse a given string input by using a stack
in Java?\n\n" );
[Link]("Given string : " +str +"\n" );
[Link]("Reverse : " +Reverse(str) + " \n " );
}
}

8
9

You might also like