From d970e5a6686760062dae5e9eb3138650c6b84528 Mon Sep 17 00:00:00 2001 From: Shaurya0237 <71341282+Shaurya0237@users.noreply.github.com> Date: Fri, 28 Oct 2022 23:12:20 +0530 Subject: [PATCH] Create StackUsingArray.java --- StackUsingArray.java | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 StackUsingArray.java diff --git a/StackUsingArray.java b/StackUsingArray.java new file mode 100644 index 0000000..00fa668 --- /dev/null +++ b/StackUsingArray.java @@ -0,0 +1,46 @@ +package Stacks; + +public class StackUsingArrays { + +private int[] data; +private int top; + +public static final int Default_Capacity=10; + +public StackUsingArrays throws Exception () + { + this.(Default_Capacity); + } +public StackUsingArrays(int capacity) throws Exception() + { if(capacity<1) + throw new Exception("Invalid Capacity"); + this.data=new int[capacity]; + top=-1; + } +public int size() +{ + return this.top+1; +} + +public boolean isEmpty() { + return this.size()==0; +} + +public void push(int value) { + if(this.size()==data.length) + throw new Exception("Stack is Full"); + this.top++; + this.data[this.top]=value; +} + +public int top() throws Exception{ + if(this.size()==0) + throw new Exception("Stack is Empty"); + int rv=this.data[this.top]; + this.data[this.top]=0; + this.top--; + return rv; +} + +} +