diff --git a/Queue.java b/Queue/Queue.java similarity index 100% rename from Queue.java rename to Queue/Queue.java diff --git a/README.md b/README.md index 36b53f3..a2c6d59 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,6 @@ This Hactoberfest, we have brought you this repo where you can contribute your J * [Prims Algo](/PrimsAlgorithm.java) * [Randomised Quick Sort](/Randomized_Quick_Sort.java) * [Selection Sort](selectionSort.java) - - +* [Stack](/Stack/Stack.java) +* [Queue](/Queue/Queue.java) ### Happy Coding 👩‍💻👨‍💻 \ No newline at end of file diff --git a/Stack/Stack.java b/Stack/Stack.java new file mode 100644 index 0000000..dbdaab8 --- /dev/null +++ b/Stack/Stack.java @@ -0,0 +1,66 @@ +class Stack +{ + int MAX; + int top; + int a[]; + + boolean isEmpty() + { + return (top < 0); + } + + Stack() + { + this.top = -1; + this.MAX=1000; + this.a = new int[MAX]; + } + + Stack(int cap) + { + this.top=-1; + this.MAX=cap; + this.a = new int[MAX]; + } + + void push(int x) + { + if (top >= (MAX - 1)) + System.out.println("Stack capacity reached"); + else + a[++top] = x; + } + + void pop() + { + if (top < 0) + System.out.println("Stack empty"); + else + top--; + } + + int peek() + { + if (top < 0) { + return 0; + } + else { + int x = a[top]; + return x; + } + } + + public static void main(String[] args) + { + Stack s = new Stack(5); + s.push(5); + s.push(7); + s.push(4); + System.out.println(s.peek()); + s.pop(); + System.out.println(s.peek()); + s.pop(); + s.pop(); + s.pop(); + } +} \ No newline at end of file