How to evaluate postfix expression with linked lists instead of integers

Ok so I am working on a program where I have to convert an infix expression to a postfix expression, then evaluate it using stacks.

I know how to convert it to postfix, and I know how to evaluate a postfix expression when the infix expression is something like 6+2*3

The problem is that the expressions are going to be like this a+b*c
where a,b, and c are names of data sets (txt files in the case) and '+' means union (all words in both data sets) and '*' means intersection (only the words that are in both data sets). I also know how to do these operations, but what I don't know how to do is make a function to do these things for me.

I am using Linked Lists to store the data from each file.

I know that when you are evaluating an expression like 6+2*3, you would check each character and if it's an operand, you push it to the stack, then when you find an operator you pop two from the stack and perform the operation on them. I just don't understand how you can do this with data sets.

I can't just pass the sets to my evaluate function, because I'm not going to know how many are going to be in the expression (It could be a+b*c, or it could be (a+b)*(c*d)+e)
I'm also going to need to store the result of each expression as it could be used in future expressions.

I just can't seem to figure this out.

Sorry if I explained this strangely, I am pretty tired right now :/

EDIT: I am using an array of linked lists to hold each data set, and then I have a parallel array that holds the names of each data set. That way I can search for a specific data set.

This is what I currently have (I know it's a mess with all the commented out code and output statements):

exp is the expression to be evaluated, set[] is the array of linked lists that contains the data sets, and fnames is the parallel array that holds the names of those data sets.

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
LinkedList<string> Evaluate(string exp, LinkedList<string> set[], string fnames[])
{
	LinkedList<string> result;

	Stack<LinkedList<string> > temp(100);
	int index1, index2;

	Stack<string> stack(exp.length());
		int size = exp.length();

		for (int i = 0; i < size; ++i)
		{
			//if(isalpha(exp[i]))
				//stack.push(string(1, exp[i]));
			int ind = bSearch(fnames, 3, string(1,exp[i]));
			cout << "ind: " << ind << endl << "String: " << string(1,exp[i]) << endl;

			//cout << endl << "Result: " << endl;
						//result.displayF();
						//cout << endl;


			if (ind >= 0)
				temp.push(set[ind]);
			else if(!result.isEmpty())
			{
				temp.push(result);
				//result.DeleteList();
			}
			else if (exp[i] == '+' || exp[i] == '*')
			{

				//string val1 = stack.top();
				LinkedList<string> val1 = temp.top();
				//stack.pop();
				temp.pop();
				cout << "Val1: " << endl;
				val1.displayF();
				//cout << "Val 1: " << val1 << endl;
				//string val2 = stack.top();
				LinkedList<string> val2 = temp.top();
				cout << "Val2: " << endl;
				val2.displayF();
				//cout << "Val 2: " << val2 << endl;
				//stack.pop();
				temp.pop();

				//index1 = bSearch(fnames, 3, val1);
				//index2 = bSearch(fnames, 3, val2);

				switch (exp[i])
				{
				  case '+':
					  //stack.push(val1+val2);
					  //temp.push(set[index1].Union(set[index2]));
					  temp.push(val1.Union(val2));
					  result = temp.top();
					 
					  //cout << "Here: " << endl;
					  //(V1.Union(V2)).displayF();
					  break;
//		  	      case '-':
//		  	    	  stack.push(val2 - val1);
//		  	    	  break;
			      case '*':
			    	  //stack.push(val2 * val1);
			    	  //temp.push(set[index1].Intersection(set[index2]));
			    	  temp.push(val1.Intersection(val2));
			    	  result = temp.top();
			    	  
			    	  //cout << "Here: " << endl;
			    	  //(V1.Intersection(V2)).displayF();
			    	  break;
//				  case '/':
//					  stack.push(val2/val1);
//					  break;
		        }
				//result = temp.top();
				cout << "===========" << endl;
				result.displayF();
				cout << "===========" << endl;

			}
		}
		return temp.top();

	//return result;
}


I was using the cout and displayF() statements to try to figure out where the problem was. I know I need to learn to use the debugger, but this way seems easier and faster for me.
The commented out code are just things that I have tried in the past and didn't work.

Here is the output I get from it:

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
ind: 0
String: A
ind: 1
String: B
ind: 2
String: C
ind: -1
String: *
Val1: 
alphA
dog
Val2: 
cat
dog
bird
===========
dog
===========
ind: -1
String: +

Postfix: ABC*+ //This is the expression it is evaluating

Here is the result: 
dog
Last edited on
I just got rid of anything that had to do with result and now it seems to work, at least with the expression I was testing it with (A+B*C or ABC*+). I am going to try it with more complicated expressions and see if it still works.

I think what happened there was I thought that result would solve a problem I was having, but I later figure out the problem (trying to pop from the old stack that I was no longer using instead of temp) and forgot that that meant result was unnecessary.
Last edited on
Topic archived. No new replies allowed.