Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Stack/InfixEvaluator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import java.util.Scanner;
class InfixEvaluator
{
static int pow(int a,int b)
{
int r=1;
for(int j=0;j<b;j++)
{
r=r*a;
}
return r;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter Post Form:");
String y =in.nextLine();
int n = y.length();
StackInt s=new StackInt(n);
for(int i= 0;i<n;i++)
{
char x = y.charAt(i);
if((x=='+') || (x=='-') || (x=='*') || (x=='/') || (x=='^'))
{
if(s.getTop()==-1)
System.out.println("Stack is Empty");
else
{
int b=s.pop();
int a=s.pop();
switch(x)
{
case '+':
s.push(a+b);
break;
case '-':
s.push(a-b);
break;
case '*':
s.push(a*b);
break;
case '/':
s.push(a/b);
break;
case '^':
s.push(pow(a,b));
break;
default:
System.out.println("Invalid Character");
break;
}
}
}
else if(x==' ')
{
int o=0;
}
else
{
s.push(Integer.parseInt(String.valueOf(x)));
System.out.println(Integer.parseInt(String.valueOf(x)));
}
}
System.out.println("Evaluation Value"+s.peek());
}
}
58 changes: 58 additions & 0 deletions Stack/Paranthesis.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.util.Scanner;
class Paranthesis
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String a = in.next();
int n = a.length();
StackChar s = new StackChar(n);
int flag=1;
for(int i= 0;i<n;i++)
{
char x = a.charAt(i);
if((x=='(') || (x=='{') || (x=='['))
s.push(x);
else if(x==')')
{
if(s.peek()=='(')
s.pop();
else
{
flag=0;
break;
}
}
else if(x=='}')
{
if(s.peek()=='{')
s.pop();
else
{
flag=0;
break;
}
}
else if(x==']')
{
if(s.peek()=='[')
s.pop();
else
{
flag=0;
break;
}
}
}
if(flag==0)
System.out.println("Not Balanced");
if(flag==1)
{
if(s.getTop()== -1)
System.out.println("Balanced");
else
System.out.println("Not Balanced");
}

}
}
66 changes: 66 additions & 0 deletions Stack/PostFix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import java.util.Scanner;
class PostFix
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String a = in.next();
int n = a.length();
char[] b=new char[a.length()];
int j=0;
StackChar s = new StackChar(n);
for(int i= 0;i<n;i++)
{
char x = a.charAt(i);
if(x=='(')
s.push(x);
else if((x=='+') || (x=='-') || (x=='*') || (x=='/') || (x=='^'))
{
if(s.getTop()== -1)
s.push(x);
else
{
char k = s.peek();
if(s.prec(x)>s.prec(k))
s.push(x);
else
{
while(s.prec(x)<=s.prec(k))
{
b[j]=s.pop();
j++;
k=s.peek();
}
s.push(x);
}
}

}
else if(x==')')
{
while(s.peek()!='(')
{
b[j]=s.pop();
j++;
}
s.pop();

}
else
{
b[j]=x;
j++;
}
}
while(s.getTop()!= -1)
{
b[j]=s.pop();
j++;
}
for(int i=0;i<n;i++)
{
System.out.print(b[i]);
}

}
}
67 changes: 67 additions & 0 deletions Stack/StackChar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@


import java.util.Scanner;

public class StackChar
{
public int top;
Scanner in = new Scanner(System.in);
char[] a= new char[12];
StackChar(int sz)
{
top=-1;
a = new char[sz];

}
public void push(char x)
{
if(top>=(a.length-1))
System.out.println("Stack is Full");
else
{
top++;
a[top]=x;
}
}
public char pop()
{
if(top==(-1))
{
return 0;
}
else
{
return a[top--];
}
}
public int getTop()
{
return top;
}
public char peek()
{
if(top==-1)
{
return 0;
}
else
return a[top];
}
public int prec(char x)
{
if(x=='+')
return 1;
else if(x=='-')
return 1;
else if(x=='*')
return 2;
else if(x=='/')
return 2;
else if(x=='^')
return 3;
else
return 0;

}

}
117 changes: 117 additions & 0 deletions Stack/StackInt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@


import java.util.Scanner;
public class StackInt
{
private int[] arr = new int[5];
private int top=-1;
Scanner in = new Scanner(System.in);
public StackInt()
{
arr =new int[10];
top = -1;
}
public StackInt(int sz)
{
arr = new int[sz];
top= -1;
}
public int getTop()
{
return top;
}
public int[] getArr()
{
return arr;
}
public void readArr(int n)
{
for(int i=0;i<n;i++)
{
arr[i]=in.nextInt();
top++;
}
}
public void printArr()
{
for(int i=0;i<=top;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
}
public void push(int x)
{
if(top>=(arr.length-1))
System.out.println("Stack is Full");
else
{
top++;
arr[top]=x;
}
}
public int pop()
{
if(top==(-1))
{
System.out.println("Can't pop. Stack is empty");
return 0;
}
else
{
return arr[top--];
}
}
public int peek()
{
if(top==-1)
{
System.out.println("Can't peek");
return 0;
}
else
return arr[top];
}
public boolean equals(StackInt a)
{
int flag=1;
if(this.top==a.top)
{
for(int i=0;i<=top;i++)
{
if(this.arr[i]!=a.arr[i])
flag=0;

}
if(flag==0)
return false;
else
{
return true;
}
}
else
return false;
}
public int getm()
{
int m=arr[0];
for(int i=0;i<=top;i++)
{
if(arr[i]<m)
m=arr[i];
}
return m;
}
public StackInt copy()
{
StackInt a= new StackInt();
a.top=this.top;
for(int i=0;i<=top;i++)
{
a.arr[i]=this.arr[i];
}
return a;
}

}