You are on page 1of 9

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 java.util.*;
/**
*
* @author nathasha
*/
public class DSA {

public static void main(String[] args) {

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


boolean result = stk .empty();
System.out.println("Before entering values : " +result);

stk.push(1);
stk.push(2);
stk.push(3);
stk.push(4);
stk.push(5);

System.out.println("\na. Print the stack : " +stk);

stk.pop();
System.out.println("b. Remove 1 element from the stack and print : " +stk);

System.out.println("c. Find the stack size : " +stk.size());

2
3
METHOD 02

Answer:

package dsa;
import java.util.*;
/**
*
* @author nathasha
*/
public class DSA {

public static void main(String[] args) {

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

System.out.println("Implement a stack for a card game. Push 5 cards to the stack.


: " );

stk.push("CARD 01");
stk.push("CARD 02");
stk.push("CARD 03");
stk.push("CARD 04");
stk.push("CARD 05");

System.out.println("\na. Print the stack : " +stk);

stk.pop();
System.out.println("b. Remove 1 element from the stack and print : " +stk);

System.out.println("c. Find the stack size : " +stk.size());

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 java.util.*;
import java.util.Scanner;
/**
*
* @author nathasha
*/
public class DSA {

public static String Reverse(String str) {

char [] ReverseString = new char [str.length()];


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

for (int i=0; i<str.length(); i++) {


stack.push(str.charAt(i));
}

int i=0;
while(!stack.isEmpty())
{
ReverseString[i++]=stack.pop();
}
return new String (ReverseString);
}

public static void main(String[] args)


{
Scanner sc= new Scanner(System.in); //System.in is a standard input stream
System.out.print("Input : ");
String str2= sc.nextLine(); //reads string
System.out.print("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 java.util.*;
import java.util.Scanner;
/**
*
* @author nathasha
*/
public class DSA {

public static String Reverse(String str) {

char [] ReverseString = new char [str.length()];


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

for (int i=0; i<str.length(); i++) {


stack.push(str.charAt(i));
}
int i=0;
while(!stack.isEmpty())
{
ReverseString[i++]=stack.pop();
}
return new String (ReverseString);
}

public static void main(String[] args)


{

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


System.out.print("Write a program to reverse a given string input by using a stack
in Java?\n\n" );
System.out.print("Given string : " +str +"\n" );
System.out.print("Reverse : " +Reverse(str) + " \n " );
}
}

8
9

You might also like