×
NOTE!
Click on MENU to Browse between Subjects...
Advertisement
DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY
(Effective from the academic year 2018 -2019)
SEMESTER - IV
Course Code 18CSL47
CIE Marks 40
Number of Contact Hours/Week 0:2:2
SEE Marks 60
Total Number of Lab Contact Hours 36
Exam Hours 03
Advertisement
Experiments 1B
Write a Java program to implement the Stack using arrays. Write Push(), Pop() & Display()
methods to demonstrate its working
Advertisement
Advertisement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | //package oneB; import java.util.Scanner; class arraystack { int a[],top,max,i; arraystack(int n) //using constructor { max=n; a=new int[max]; top=-1; } void push(int ele) { if(top==max-1) { System.out.println("----Stack OVER FLOW - Elements in the array are :"+max+" ----"); } else { a[++top]=ele; } } void pop() { if(top==-1) { System.out.println("----Stack Underflow----"); } else { System.out.println("Poped Element is :"+a[top--]); } } void display() { if(top==-1) { System.out.println("----STACK UNDERFLOW - No Element to display----"); return; } System.out.println("Elements in the stack are as follows :"); int p=top; for(i=p;i>=0;i--) { System.out.println("ELEMENT :"+a[i]); } } } public class Stack { public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.println("Enter the Size of the array"); int n=scan.nextInt(); boolean done=false; arraystack stk =new arraystack(n); do { System.out.println("Stack Operation "); System.out.println("1.Push\n2.Pop\n3.Display\n4.Exit"); System.out.println("Enter Your Choice"); int choice=scan.nextInt(); switch(choice) { case 1:System.out.println("Enter the Elements to be instered"); stk.push(scan.nextInt()); break; case 2:stk.pop(); break; case 3:stk.display(); break; case 4:done=true; break; default : System.out.println("You Have Entered the Wrong Choice"); break; } } while(!done); scan.close(); } } |
Advertisement
Advertisement
Fig 1.2: Output .
×
Note
Please Share the website link with Your Friends and known Students...
-ADMIN
-ADMIN
×
Note
Page Number is specified to navigate between Pages...
T = Text book
QB = Question Bank
AS = Amswer Script
-ADMIN
T = Text book
QB = Question Bank
AS = Amswer Script
-ADMIN
Advertisement