Assignment 5.2 Stack Handling



import java.util.*;
import java.io.*;

class stack<T>{
	ArrayList<T> A;
	int top = -1;
	int size;


	stack(int size){
		this.size = size;
		this.A = new ArrayList<T>(size);
	}

	void push(T x){
		if(top+1 == size){
			System.out.print("<Full-5> ");
		}
		else {
			top++;
			if(A.size() > top){
				A.set(top, x);
			}
			else {
				A.add(x);
			}
		}
	}

	void pop(){
		if(top==-1){
			System.out.print("<Empty>");
		}
		else {
			System.out.print(A.get(top)+" ");
			top--;
		}
	}
}

public class stackAss{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		stack<String> s1 = new stack<String>(5);

		while(true){
			// System.out.println("Enter Your Choice: 1 -> Push  2-> Pop  0-> exit");
			int ch = sc.nextInt();
			try{
				if(ch == 0){
					System.exit(1);
				}
				else if (ch == 1) {
					// System.out.println("Enter Your Choice Stack element: ");
					String num = sc.next();
					s1.push(num);	
				} 
				else if (ch == 2) {
					s1.pop();
				}
			}
			catch(Exception e){

			}
		}
	}
}






Post a Comment

0 Comments