i am new here and i have this Q to solve it plz help me
Using class stack, write a program to read an expression and checks if the symbols : the parentheses ( ), square brackets [ ] are balanced or not. For example :
(a+b-[c+d)] is not balanced correctly.
(a+b-[c+d]) is balanced
:cry:

Recommended Answers

All 9 Replies

Hi,

Assuming you already have a stack class implemented. Start with an empty stack and just read your string char by char if it is a "(" or "[" push that char to your stack; if it is a ")" or "]" pop a char from your stack and check wether the poped char is corresponding : "(" for ")" and "[" for "]" ; if poped char isn't corresponding or if at the end the stack isn't empty then your string is unbalanced.

Loren Soth

That's easy. Stacks are FILO so if you pop a parenthesis off, then at the oppposite position (the last) would have to be a parenthesis. Show a little effort and I can help more.

thank u for ur replayies,,,,
i've already write the stack class ,but i am confusing in writing the code i need someone who explain how to write it because i don't know how to write it,,,plz

Member Avatar for iamthwee

thank u for ur replayies,,,,
i've already write the stack class ,but i am confusing in writing the code i need someone who explain how to write it because i don't know how to write it,,,plz

Let me see your stack class

i hope it will be correct

public class stack {
private int t, MAX_STACK_SIZE=100;
private int [] S;
public stack()
{ t = -1; S =S = new int [MAX_STACK_SIZE]; }
public boolean isEmpty()
{ return (t == -1); }
public int size()
{ return (t + 1);
}
public void push(int el)
{
if (t == S.length- 1)
System.out.println("Stack is Full");

else
{
t = t + 1;
S[t] = el;
}
}
public int pop() {

if (isEmpty() )
{
System.out.println("Stack is Empty");
return - 1;
}
else
{
t = t - 1;
return S[t + 1];
}
}
public int top()
{
if (isEmpty ())
{
System.out.println("Stack is Empty");
return -1;
}

else return S[t];
}
}

nobody want help me :cry:

Well, I guess it's because we can't just give you a full solution without you even trying to work something out. Otherwise you wouldn't learn anything would you? However, out of good will I've written sample code for this bracket balancing algorithm without the use of a stack class. You will have to implement your own algorithm with a stack for your submission.

public class BracketBalance {
	private static boolean checkBalance (String str, int[] idx) {
		char currentBracket = (idx[0] >= 0 && idx[0] < str.length()) ? str.charAt(idx[0]) : 0;
		while (++idx[0] < str.length()) {
			switch (str.charAt(idx[0])) {
				case '(': case '[': case '{': case '<':
					if (!checkBalance(str, idx))
						return false;
					break;
				case ')':
					return currentBracket == '(';
				case ']':
					return currentBracket == '[';
				case '}':
					return currentBracket == '{';
				case '>':
					return currentBracket == '<';
			}
		}
		return currentBracket == 0;
	}
	public static boolean checkBalance (String str) {
		return checkBalance(str, new int[] {-1});
	}
	public static void main (String[] args) {
		checkBalance("(a+b-[c+d)]"); // returns false
		checkBalance("(a+b-[c+d])"); // returns true
	}
}

Hope this helps.

i have write the programme in c++ and i need your opinion ,,and how come i can convert it to java,,,,plz see it

#include<iostream.h>
class node{


friend class stack;
private:
char data;
node * next;
public:
node(char x,node *n)
{
data=x;
next=n;
}
};


class stack
{
private:
node * top;
public:
stack()
{top=0;}     //constructor
int stackempty()
{
if(top==0)
return 0;
else
return 1;
}
char pop()
{
if (stackempty()==0)
cout<<"Stack is empty"<<endl;
else
{
node * t=top;
char y=top->data;
top=top->next;
cout<<"\n   \t"<<y<<"\t in pop"<<"\n";
delete  t;


return y;
}


}
void push(char dat){
node* t=new node(dat,top);
top=t;


}


void print()
{
for(node* t=top;top!=0;top=top->next)
cout<<top->data<<endl;
}
};
void check(char s[15])
{
stack st;
char t;
int i=0;
while(s!='\0')
{
if(s=='(' || s=='[')
st.push(s);


else    if(s==')' || s==']')
{
t=st.pop();
cout<<"\n   \t"<<s<<"\t "<<i<<"\n";
}
i++;
}
if(st.stackempty()!=0)
cout<<"not balanced"<<endl;
else
cout<<"balanced"<<endl;
}
void main()
{
check("abc*+");
stack s;
s.push('H');
s.push('a');
s.push('y');
s.push('a');
s.pop();
s.print();


}
Member Avatar for iamthwee

Astronox has given a good guideline, if you have your stack class working as expected I don't see what the problem is.


Forget c++, this is java.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.