import java.util.*; class GenStack<T>{ T stack[]; int top=-1; GenStack(T stack[]){ this.stack=stack; } void push(T a){ if((top+1)==stack.length){ System.out.println("Stack is full"); } else{ stack[top+1]=a; top=top+1; } } T pop(){ if(top== -1){ System.out.println("Stack is empty"); return null; } else{ T i= stack[top]; top--; return i; } } void display(){ if(top==-1){ System.out.println("Stack is empty"); } else{ for(int i=top;i>-1;i--){ System.out.println(stack[i]); } } } public static void main(String args[]){ Scanner sc= new Scanner(System.in); System.out.print("Enter size of stack"); int n=sc.nextInt(); Integer a[]= new Integer[n]; GenStack<Integer> s1= new GenStack<Integer>(a); s1.push(101); s1.push(102); s1.push(103); s1.push(104); s1.push(105); s1.display(); Integer i =s1.pop(); System.out.println("poped : "+i); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter