Generic stack Integer,double user-defined arrays and linked list
Wed May 29 2024 19:26:45 GMT+0000 (Coordinated Universal Time)
Saved by
@exam123
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);
}
}
content_copyCOPY
Comments