Problem Statement:
Create a class Stack to perform stack operations push and pop. Overload the operation
so that integers, floating point and strings elements can be processed.
Input and Output Requirements:
The program reads integer, floating point and string values from the keyboard. It then push and pop different values in to the stack and shows its status.
Algorithm:
Begin
Define a class stack with instance variables stackint (array of integers), stackfloat (array of floating point numbers) and stackstring (array of string variables).
Implement overloaded push() method for inserting values into the different k stackarray.
Implement overloaded pop() method inorder to delete items from the corresponding stack arrays.
Show the result using display() method.
End
Program:
import java.io.*; import java.util.*; public class stack extends stackoperation { public static void main(String args[]) { int choice; stackoperation s= new stackoperation(); Scanner in = new Scanner(System.in); s.inputelements(); do { System.out.println("*****Menu*****"); System.out.println("1.Push Integer"); System.out.println("2.Push Float"); System.out.println("3.Push String"); System.out.println("4.Pop integer "); System.out.println("5.Pop Float "); System.out.println("6.Pop String"); System.out.println("7.Display"); System.out.println("8.Exit"); System.out.println("\nEnter Choice...."); choice = in.nextInt(); switch(choice) { case 1: { //push Integer.... int x1; if(s.top1==s.num) System.out.println("Stack Full"); else { System.out.println("Enter number:"); x1 = in.nextInt(); s.push(x1); } break; } case 2: { //push float.... float x2; if(s.top2 == (s.num)) System.out.println("Stack Full"); else { System.out.println("Enter number:"); x2 = in.nextFloat(); s.push(x2); } break; } case 3: { //push String.... String x3; if(s.top3==s.num) System.out.println("Stack Full"); else { System.out.println("Enter String:"); x3 = in.next(); s.push(x3); } break; } case 4: { //pop Integer..... int x1; if(s.top1 == 0) { x1 = s.pop1(); System.out.println("Poped integer number:"+ x1); break; } else System.out.println("Stack Empty"); } case 5: { //pop float..... if(s.top2 == 0) { float x2; x2 = s.pop2(); System.out.println("Poped float number:"+ x2); break; } else System.out.println("Stack Empty"); } case 6: { //pop String..... if(s.top3 == 0) { String x3; x3 = s.pop3(); System.out.println("Poped String:"+ x3); break; } else System.out.println("Stack Empty"); } case 7: { int i; System.out.println("Integer Elements are..."); for(i=0;i<s.top1;i++) { System.out.println(" \t"+ s.array1[i]); } System.out.println("Float Elements are..."); for(i=0;i<s.top2;i++) { System.out.println(" \t"+ s.array2[i]); } System.out.println("Strings are..."); for(i=0;i<s.top3;i++) { System.out.println(" \t"+ s.array3[i]); } } default: { break; } } }while(choice!=7); } } class stackoperation { public int num,top2=0,top1=0,top3=0, temp; int array1[]= new int[30]; float array2[] = new float[30]; String array3[] = new String[30]; Scanner input = new Scanner(System.in); void inputelements() { System.out.println("Enter the no of elements:"); num = input.nextInt(); } void push(int x) { array1[top1]=x; top1++; } int pop1() { int x; x = array1[top1]; top1--; return x; } void push(float x) { array2[top2]=x; top2++; } float pop2() { float x; x = array2[top2--]; return x; } void push(String x) { array3[top3]=x; top3++; } String pop3() { String x; x = array3[top3--]; return x; } } |
Output:
Enter the no of elements: 4
*****Menu*****
1.Push Integer
2.Push Float
3.Push String
4.Pop integer
5.Pop Float
6.Pop String
7.Display
8.Exit
Enter Choice….1
Enter number:55
*****Menu*****
1.Push Integer
2.Push Float
3.Push String
4.Pop integer
5.Pop Float
6.Pop String
7.Display
8.Exit
Enter Choice….1
Enter number: 66
*****Menu*****
1.Push Integer
2.Push Float
3.Push String
4.Pop integer
5.Pop Float
6.Pop String
7.Display
8.Exit
Enter Choice….2
Enter number:45.12
*****Menu*****
1.Push Integer
2.Push Float
3.Push String
4.Pop integer
5.Pop Float
6.Pop String
7.Display
8.Exit
Enter Choice…. 2
Enter number: 77.89
*****Menu*****
1.Push Integer
2.Push Float
3.Push String
4.Pop integer
5.Pop Float
6.Pop String
7.Display
8.Exit
Enter Choice….3
Enter String: good
*****Menu*****
1.Push Integer
2.Push Float
3.Push String
4.Pop integer
5.Pop Float
6.Pop String
7.Display
8.Exit
Enter Choice….3
Enter String:bad
*****Menu*****
1.Push Integer
2.Push Float
3.Push String
4.Pop integer
5.Pop Float
6.Pop String
7.Display
8.Exit
Enter Choice….7
Integer Elements are…
55
66
Float Elements are…
45.12
77.89
Strings are…
good
bad
*****Menu*****
1.Push Integer
2.Push Float
3.Push String
4.Pop integer
5.Pop Float
6.Pop String
7.Display
8.Exit
Enter Choice….8
Leave a Reply