Data Structures and Algorithms
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.
c. Findthestacksize
METHOD 01
Answer:
package dsa;
import java.util.*;
/**
*
* @author nathasha
*/
public class DSA {
stk.push(1);
stk.push(2);
stk.push(3);
stk.push(4);
stk.push(5);
stk.pop();
System.out.println("b. Remove 1 element from the stack and print : " +stk);
2
3
METHOD 02
Answer:
package dsa;
import java.util.*;
/**
*
* @author nathasha
*/
public class DSA {
stk.push("CARD 01");
stk.push("CARD 02");
stk.push("CARD 03");
stk.push("CARD 04");
stk.push("CARD 05");
stk.pop();
System.out.println("b. Remove 1 element from the stack and print : " +stk);
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 {
int i=0;
while(!stack.isEmpty())
{
ReverseString[i++]=stack.pop();
}
return new String (ReverseString);
}
Answer:
package dsa;
import java.util.*;
import java.util.Scanner;
/**
*
* @author nathasha
*/
public class DSA {
8
9