-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacking.java
55 lines (55 loc) · 1.43 KB
/
stacking.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package javaapplication1;
import java.util.Scanner;
public class stacking {
Node top;
public void stack(){
this.top=null;
}
public void push(String data){
Node temp=new Node(data);
temp.next=top;
top=temp;
}
public void pop(){
if(top==null) System.out.println("stack is empty");
else top=top.next;
}
public void peek(){
if(top==null) System.out.println("stack is empty");
else System.out.println((String)top.data);
}
public boolean isempty(){
if(top==null) return true;
return false;
}
public int size(){
Node temp=top;
int count=0;
while(temp!=null){
count++;
temp=temp.next;
}
return count;
}
public void print(){
Node temp=top;
while(temp!=null){
System.out.print(temp.data+" ");
temp=temp.next;
}
System.out.println();
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
stacking s=new stacking();
s.stack();
System.out.println("enter a expression:");
String data=sc.nextLine();
for(int i=0;i<data.length();i++){
if("(".equals(data.charAt(i))) s.push("(");
if(")".equals(data.charAt(i))) s.pop();
}
if(s.isempty()) System.out.println("expression is balanced");
else System.out.println("expression is not balanced");
}
}