19CSE 212: Data Structures and Algorithms Lab Sheet 5 Stack Adt
19CSE 212: Data Structures and Algorithms Lab Sheet 5 Stack Adt
Lab sheet 5
STACK ADT
Program:
package com.company;
public StackInt(){
this.top=-1;
}
}
2. Write a separate test driver class (Test.java) and create an object instance of StackInt
Compile both the .java files and run Test.java. Ensure no errors.
Program:
package com.company;
4. Add a default constructor that will create the stack of some standard top (say 10) in
case user does not give the top.
StackInt() {
top = -1;
Test cases:
System.out.println(si.arr.length);
Program:
package com.company;
int[] arr;
int top;
public StackInt(){
arr=new int[10];
top=-1;
}
}
Output:
5. Add a parameterized constructor that will create the stack with specified
Test cases:
System.out.println(si2.arr.length);
Program:
package com.company;
int[] arr;
int top;
public StackInt(){
arr=new int[10];
top=-1;
Output:
4. Add print function to StackInt.java that will print the contents up to the top of the stack. It
should be public.
Test cases:
si.push(100);
si.print();
si.push(200);
si.print();
StackInt();
........................................
ArrayIndexOutOfBoundsException.
Program:
top++;
arr[top]=el;
}
public class Driver {
public static void main(String[] args) {
}
}
Output:
6. Implement the check to push() method. Don't add an item to the array if top
Program:
public void push(int el){
if(top==arr.length-1){
System.out.println("Stack OverFlow Error");
}else{
top++;
arr[top]=el;
}
Output:
(a) Run this time. No exception will be thrown this time around.
7. Add a getter method getTop() which returns the current top of the
// return top;
Test cases:
(a) Invoke getTop() from test file and print the top.
System.out.println(si2.getTop());
public int getTop() {
return top;
}
public class Driver {
public static void main(String[] args) {
Output:
8. Now implement pop() method that removes the topmost item in the stack and returns
Test cases:
si.print();
si.print();
int it1=si.pop();
si.print();
int it2=si.pop();
si.print();
int i3=si.pop();
si.print();
int i4=si.pop();
si.print();
}
}
Output:
The last call to pop should throw Array out of bounds exception.
9. Now implement the check for lower limit (< 0) and print "can't pop"
message. Run test file. Note that no exception will be thrown this time.
public int pop(){
if(top==-1){
System.out.println("Cant Pop (Stack Underflow) ");
return -1;
}
int item=arr[top];
top--;
return top;
}
}
int it1=si.pop();
si.print();
int it2=si.pop();
si.print();
int i3=si.pop();
si.print();
int i4=si.pop();
}
}
Output:
10. Implement peek() method to return topmost item without removing it from the
stack public int peek() {
Test cases: (a) Write test file to check the working of peek. This also needs <0 check.
StackInt si = newStackInt[5];
System.out.println(si.peek()); si.push(100);
System.out.println(si.peek()); si.push(200);
System.out.println(si.peek()); si.push(300);
System.out.println(si.peek());
Program:
public int peek(){
if(top==-1){
System.out.println("Cant Peek (Stack Underflow) ");
return -1;
}else{
return arr[top];
}
}
Output:
The first peek() call should print "can't peek" message. Other should return properly.
11. You can't check if contents of two stacks are same by using =
=. Test cases:
new StackInt();
si1.push(100);
si2.push(100);
si1.push(200);
si2.push(200);
if (si1 == si2)
else
Run it and check. It will print si1 and si2 are not same. Why?
Because = = operator will only compare 2 addresses. Then how to check the contents?
Output:
12. Implement equals() method which will first compare the top. If not same, return false. If
same, scan through arrays of both stacks to check if each item is one stack is same as an
Test cases:
It will print si1 and si2 are same since top and contents are same.
Program:
public boolean equal(StackInt s2){
if(this.top==s2.top){
for(int i=this.top;i>=0;i--){
if(this.arr[i]!=s2.arr[i]){
return false;
}
}
return true;
}else{
return false;
}
}
Output:
(b) Now write test file to do same number of pushes but contents
si1.push(100);
si2.push(100);
si1.push(200);
si2.push(300);
if (code)
Output:
Program:
public int getMinElement(){
if(top==-1){
System.out.println("Stack UnderFlow Error");
return -1;
}else{
int min=arr[top];
for(int i=top;i>=0;i--){
if(arr[i]<min){
min=arr[i];
}
}
return min;
}
}
public class Driver {
public static void main(String[] args) {
}
}
Output:
14. Implement a copyStack() function to return a duplicate stack of original stack.
Program:
public StackInt copyStack(){
StackInt duplicate=new StackInt(this.arr.length);
if(this.top==-1){
return duplicate;
}
else {
return duplicate;
}
}
public class Driver {
public static void main(String[] args) {
}
}
Output:
Program:
package com.company;
import java.util.*;
for(int i=0;i<init.length();i++){
stack.push(init.charAt(i));
}
for(int i=0;i<finalString.length;i++){
finalString[i]=stack.pop();
}
return Arrays.toString(finalString);
System.out.println("Enter a String");
Scanner sc=new Scanner(System.in);
String st=sc.nextLine();
}
}
Output:
16. Given a stack with push(), pop(), empty() operations, delete the middle element in the
Program:
public void deleteMid(){
if(top==-1){
System.out.println("Stack UnderFlow Error");
}else{
int mid=-1;
mid=(top%2==0)?top/2:(top/2+1);
for(int i=mid;i<=top-1;i++){
arr[i]=arr[i+1];
}
top--;
}
}
Output:
Program:
public void sortStack(){
if(top==-1){
System.out.println("Stack UnderFlow Error");
}
else {
}
public class Driver {
}
}
Output:
Program:
package com.company;
class TwoStacks{
int[] array;
int top1;
int top2;
public TwoStacks(){
array=new int[10];
top1=-1;
top2=array.length;
}
return -1;
}else{
int item=array[top1];
top1++;
if(top1==top2){
top1=-1;
}
return item;
}
}
return -1;
}else{
int item=array[top2];
top2--;
if(top2==top1){
top2=array.length;
}
return item;
}
}
}else{
System.out.println("Elements In Stack-1: ");
for(int i=top1;i>=0;i--){
System.out.println(array[i]);
}
}
}
for(int i=top2;i<array.length;i++){
System.out.println(array[i]);
}
}
}
st.pushStack1(10);
st.pushStack1(20);
st.pushStack1(30);
st.pushStack1(40);
st.pushStack1(50);
st.pushStack1(60);
st.pushStack2(100);
st.pushStack2(200);
st.pushStack2(300);
st.pushStack2(400);
st.pushStack2(500);
st.printStack1();
st.printStack2();
}
Output: