Also Read
Java Program to Implement Stack using Array
A stack is a conceptual data structure that contains a collection of homogenous elements. Stack follows the mechanism of FILO (First In Last Out) and LIFO (Last In First Out), which means that whichever element is inserted at the first number in the stack will be removed at the end and the element inserted at the end will be removed first.
In today's article we will implement the stack with the help of array. And at the same time we will also see infix to postfix conversion with the help of Stack Class. First we understand the 3 basic operations that are used in the stack.
Push: The push function inserts the data value into the stack.
Pop: The pop function returns the data value that is at the top of the stack and removes it from the stack.
Peek: The pop function returns the data value that is at the top of the stack.
Infix to Postfix Conversion using Stack
public class Stack {
char[] arr = new char[30];
int index=-1;
public void push(char v) {
if(index==arr.length-1) {
System.out.println("Stack is full!");
}
else {
index++;
arr[index]=v;
}
}
public char pop() {
if(index==-1) {
return '$';
}else {
return arr[index--];
}
}
public char peek() {
if(index==-1) {
return '$';
}
else {
return arr[index];
}
}
}
public class Main {
//main method
public static void main(String[] args)
{
infixToPostfix("5+(((2+3)*5)/7)-3");
}
//This method will convert infix expression to postfix expression.
public static void infixToPostfix(String s) {
Stack obj = new Stack();
String output = "";
for(int i=0;i<s.length();i++) {
char ch = s.charAt(i);
if(ch>='0'&&ch<='9') {
output=output+" "+ch;
}
else if(ch=='+'){
char op = obj.peek();
if(op=='$'||op=='+'||op=='(') {
obj.push(ch);
}
else {
while(op!='$'&& op!='+'&& op!='(') {
output=output+" "+obj.pop();
op=obj.peek();
}
obj.push(ch);
}
}
else if(ch=='-'){
char op = obj.peek();
if(op=='$'||op=='-'||op=='(') {
obj.push(ch);
}
else {
while(op!='$'&& op!='-' && op!='(') {
output=output+" "+obj.pop();
op=obj.peek();
}
obj.push(ch);
}
}
else if(ch=='*'){
char op = obj.peek();
if(op=='*'||op=='+'||op=='-'||op=='$'||op=='(') {
obj.push(ch);
}else {
while(op!='*'&& op!='+'&& op!='-'&& op!='$' && op!='(')
{
output+=" "+obj.pop();
op=obj.peek();
}
obj.push(ch);
}
}
else if(ch=='/'){
char op = obj.peek();
if(op=='/'||op=='+'||op=='-'||op=='$'||op=='(') {
obj.push(ch);
}else {
while(op!='/'&& op!='+'&& op!='-'&& op!='$' && op!='(')
{
output+=" "+obj.pop();
op=obj.peek();
}
obj.push(ch);
}
}
else if(ch=='(') {
obj.push(ch);
}
else if(ch==')') {
char op = obj.peek();
while(op!='(') {
output+=" "+obj.pop();
op=obj.peek();
}
op=obj.pop();
}
}
while(obj.peek()!= '$') {
output+=" "+obj.pop();
}
output=output.trim();
System.out.println("Infix expression : "+s);
System.out.println("Postfix expression : "+output);
}
}
I am also giving you the download link of the entire file here, if you need it, you can download it.
if any issue comes, you can comment us or you can watch a video on this topic on our YouTube Channel.
0 Comments