I need to write a program that will read in a polish string and calculate the value of the string, using 2 linked list, a polish queue and evaluation stack.
Here is what I got so far:
#include < iostream>
using std:: cin;
using std:: cout;
using std:: endl;
struct node
{
int num;
char op;
char type;
node *next;
};
int main()
{
char s[50];
node *head = new node; //Polish queue
node *temp = head;
node *h = new node; // Evaluation stack
node *t = h;
cout <<"Enter RPN: ";
cin >> s;
while (s[0] != '=')
{
if (isdigit (s[0]))
{
int x = atoi (s);
temp->num = x;
temp->type = 'n';
temp->next = new node;
temp = temp->next;
}
else
{
temp->op = s[0];
temp->type = 'o';
temp->next = new node;
temp = temp->next;
}
cin >> s;
}
temp = 0;
while (head)
{
if (head->type == 'o')
{
char x = head->op;
int arg2 = h->num;
int arg1 = h->next->num;
t = h->next->next->next;
delete h;
h = t;
if (x == '+')
{
t->num = arg2 + arg1;
}
if (x == '-')
{
t->num = arg2 - arg1;
}
if (x == '*')
{
t->num = arg2 * arg1;
}
if (x == '/')
{
t->num = arg2 / arg1;
}
}
if (head->type == 'n')
{
t->num = temp->num;
t->next = new node;
t = temp->next;
t->next = 0;
}
}
cout << t->num;
cout << endl << "Press ENTER to exit...";
cin.get();
cin.get();
return 0;
}
The program does not work (access violation). I think the problem is with the evaluation stack... do I have to create another structure in order to create second linked list?