Data structure, program using stacks!

I really need help with this problem!!

Given a set of different types of paired symbols; determine whether it is balanced or not (the opening and closing version of each type are paired correctly). Any other type of characters may appear in the input will be neglected.
For example:
(**(*[*])****) is balanced, also [{}**[]**()]** is balanced. But ((*{*)*}) is unbalanced as well as [()***[]***{*(*)}.
Write a C++ program that requires a string containing and expression as an input from the user and check for balanced bracket pairs using stack data structure.

Here is my code witch give me nothing when I run it. can anyone please tell me what is wrong with this code?!!

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
using namespace std;

class CustomStack
{
	private:

		char *myStack;
		int size;
		int top;

	public:

		CustomStack(int sizeValue)
		{
			size = sizeValue;
			top = -1;
			myStack = new char[size];
		}

		bool isEmpty()
		{
			if(top == -1)
			{
				return true;
			}
			return false;
		}

		bool isFull()
		{
			if(top == size-1)
			{
				return true;
			}
			return false;
		}

		void push(char element)
		{
			if(!isFull())
			{
				myStack[++top] = element;
			}
		}

		char pop()
		{
			return myStack[top--];
		}

		char peek()
		{
			return myStack[top];
		}
};

void main ()
{
	string x;
	cin>>x;
	CustomStack stack (x.size());

	for (int i=0;i<x.size();i++)
	{
		if ((x[i]=='(') || (x[i] == '[' ) || (x[i]=='{'))
		stack.push(x[i]);

		
			while (stack.isEmpty()==false)
			{

			if ((stack.peek()=='('&&x[i]==')') || (stack.peek()=='['&&x[i]==']') || (stack.peek()=='{'&&x[i]=='}') )

				stack.pop();
			}
	}
		
		 if (stack.isEmpty())
			{
			cout<<"balanced"<<endl;
		 }
		else
		{
			cout<<"unbalanced"<<endl;
		
		}

		system("pause");
}
This needs to be modified .
1
2
3
4
5
6
7
8
for (int i=0;i<x.size();i++)
{
	if ((x[i]=='(') || (x[i] == '[' ) || (x[i]=='{'))
	     stack.push(x[i]);
	else if ( (stack.peek()=='('&&x[i]==')') || (stack.peek()=='['&&x[i]==']') || (stack.peek()=='{'&&x[i]=='}') )
             stack.pop();
}
}

It should work now
Topic archived. No new replies allowed.