Skip to content

Commit fcc02fd

Browse files
committed
Updated stack basic algos 1
1 parent a1e9d16 commit fcc02fd

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

Stack/ArrayImplementation.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package SummerTrainingGFG.Stack;
2+
3+
/**
4+
* @author Vishal Singh
5+
*/
6+
public class ArrayImplementation {
7+
static class Stack{
8+
private final int size;
9+
private final int[] arr;
10+
private int top;
11+
Stack(int size){
12+
this.size = size;
13+
arr = new int[size];
14+
}
15+
void push(int data){
16+
if(top == size){
17+
System.out.println("Overflow");
18+
}
19+
else{
20+
arr[top] = data;
21+
top++;
22+
}
23+
}
24+
int pop(){
25+
if(top == 0){
26+
System.out.println("Underflow");
27+
return -1;
28+
}
29+
else{
30+
int res = arr[top-1];
31+
top--;
32+
return res;
33+
}
34+
}
35+
void print(){
36+
if(top == -1) {
37+
System.out.println("No element present");
38+
}
39+
else{
40+
for (int i = 0; i < top; i++) {
41+
System.out.print(arr[i]+" ");
42+
}
43+
System.out.println("");
44+
}
45+
}
46+
}
47+
public static void main(String[] args) {
48+
Stack s = new Stack(3);
49+
s.push(5);
50+
s.push(4);
51+
s.push(3);
52+
s.print();
53+
s.push(31);
54+
System.out.println(s.pop());
55+
System.out.println(s.pop());
56+
System.out.println(s.pop());
57+
System.out.println(s.pop());
58+
s.push(1);
59+
s.print();
60+
}
61+
}

Stack/BalancedParenthesis.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package SummerTrainingGFG.Stack;
2+
import java.util.*;
3+
/**
4+
* @author Vishal Singh
5+
*/
6+
public class BalancedParenthesis {
7+
static boolean check(char a,char b){
8+
return (a=='{' && b == '}')||(a=='[' && b == ']')||(a=='(' && b == ')');
9+
}
10+
static boolean isBalancedParenthesis(String str){
11+
Stack<Character> stack = new Stack<>();
12+
for (int i = 0; i < str.length(); i++) {
13+
if(str.charAt(i) == '{' || str.charAt(i) == '[' || str.charAt(i) == '('){
14+
stack.push(str.charAt(i));
15+
}
16+
else{
17+
if(stack.isEmpty()){
18+
return false;
19+
}
20+
else if(check(str.charAt(i),stack.peek())){
21+
return false;
22+
}
23+
else{
24+
stack.pop();
25+
}
26+
}
27+
}
28+
return stack.empty();
29+
}
30+
public static void main(String[] args) {
31+
String parenthesis = "{[([{}])]}"; // TRUE
32+
String parenthesis1 = "{[}]}"; // FALSE
33+
System.out.println(isBalancedParenthesis(parenthesis));
34+
System.out.println(isBalancedParenthesis(parenthesis1));
35+
}
36+
}

Stack/LinkedListStack.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package SummerTrainingGFG.Stack;
2+
3+
/**
4+
* @author Vishal Singh
5+
*/
6+
public class LinkedListStack {
7+
static class Node{
8+
int data;
9+
Node next;
10+
Node(int data){
11+
this.data = data;
12+
}
13+
}
14+
static class List{
15+
Node head;
16+
void insertEnd(int data){
17+
Node temp = new Node(data);
18+
temp.next = head;
19+
head = temp;
20+
}
21+
int pop(){
22+
if(head == null){
23+
return -1;
24+
}
25+
int data = head.data;
26+
head = head.next;
27+
return data;
28+
}
29+
void print(){
30+
Node temp = head;
31+
while(temp != null){
32+
System.out.print(temp.data+" ");
33+
temp = temp.next;
34+
}
35+
System.out.println("");
36+
}
37+
}
38+
static class Stack {
39+
private final List list = new List();
40+
void push(int data){
41+
list.insertEnd(data);
42+
}
43+
int pop(){
44+
int temp = list.pop();
45+
return temp;
46+
}
47+
void print(){
48+
list.print();
49+
}
50+
}
51+
52+
public static void main(String[] args) {
53+
Stack s = new Stack();
54+
s.push(5);
55+
s.push(4);
56+
s.push(3);
57+
s.push(31);
58+
s.print();
59+
System.out.println(s.pop());
60+
System.out.println(s.pop());
61+
System.out.println(s.pop());
62+
System.out.println(s.pop());
63+
s.push(1);
64+
s.print();
65+
}
66+
}

0 commit comments

Comments
 (0)