I find that whenever my linked list prints, it is a stack, which I do not want. But since I have to design the linked lists myself I was thinking about making a doubly linked list that would be made as the original was, then it would be copied towards the end. I have done a lot of it but there must be an invalid address or something because whenever it tried to execute my computer reports a system error. I have tried debugging with no success... If anyone could figure out the problem that would be great. I have pinpointed the problem to one line (I commented the line).
The code:
Code:
#include <iostream>
using namespace std;
struct node
{
float num;
char opr;
char type;
node* next;
node* prev;
};
void printList(const node*);
//entry* insert(int n, node* h);
//entry* deleteNode(int n, node* h);
//entry* PQInit(entry* h, entry* PQh);
float arrayToFloat(char part[]);
int main()
{
node *temp = 0, *head = 0;//starts out empty
node *PQt = 0, *PQh = 0, *PQp = 0;//Polish queue
float fltval;
int j, n = 0, flag;
char string[80];
cout << "Enter an algebraic equation in reverse polish notation(type 'stop' to end): \n";
cin.getline(string, 80);
cout << "Output from func arrayToInt:\n";
while(string[n] != '\0')
{
char part[15] = {'O'};
for(j = 0, flag = 0; (string[n] != ' ') && (string[n] != '\0'); j++, n++)
{
part[j] = string[n];
if(!isdigit(part[j]))//if character
{
flag = 1;
}//if
}//for
n++;
if(flag == 0)//if part consists of int
{
if(string[n] != '\0')
{
fltval = arrayToFloat(part);//converting array based number to float
cout << fltval << endl;
head = new node;
head->num = fltval;
head->type = 'N';
head->next = temp;
temp = head;
//setting up PQ
PQh = new node;
PQh->next = PQt;
PQt = PQh;
PQp->prev = PQt;//this line causes a problem
PQp = PQh;
}//if
}//if
else
{
if(string[n] != '\0')
{
head = new node;
head->opr = part[0];
head->type = 'O';
head->next = temp;
temp = head;
//setting up PQ
PQh = new node;
PQh->next = PQt;
PQt = PQh;
PQp->prev = PQt;
PQp = PQh;
}//if
}//else
}//while
//PQh = PQInit(head, PQh);
cout << "Output from linked list:\n";
printList(head);
while(head->next != 0)
{
if(head->type == 'N')
PQh->num = head->num;
else
PQh->opr = head->opr;
head = head->next;
PQh = PQh->prev;
}//while
printList(PQh);
return 0;
}//main
/***************************************/
//Prints list of Nodes
void printList(const node* pointer)
{
while(pointer != 0)
{
if(pointer->type == 'N')
cout << pointer->num << endl;
else
cout << pointer->opr << endl;
pointer = pointer->next;
}//while
}//printList
/****************************************/
//Converts an array of ints to a float value
float arrayToFloat(char part[])
{
int L = 0, i, j;
float val = 0, value[15] = {0};
float valtab[5] = {1, 10, 100, 1000, 10000};
// 1 2 3 4 5
for(i = 0; part[i]; i++)
{
value[i] = part[i] - '0';//converting to int from char
}//for
L = strlen(part);
for(i = 0, j = L - 1; i < L; i++, j--)
{
val = val + (value[i] * valtab[j]);
}//for
return val;
}//arrayToFloat