I am working on an assignment which to create a program that can read a polynomial from a text file using linked list. Which when i tried to return the starting pointer of the linked list "poly_pointer" from the read_poly function things being weird.
The expected output should be -12
But what i got is -10
And if i add one single line of code right before return in read_poly
cout << curr_ptr->coef;
the output would suddenly turns to 2-12 May anyone provide some explanation on why and how to fix this problem?
Polynomial.h
#ifndef _POLYNOMIAL_H_
#define _POLYNOMIAL_H_
using namespace std;
typedef struct poly_node *poly_pointer;
typedef struct poly_node {
int coef;
int expon;
poly_pointer link;
};
poly_pointer addNode(int coef, int expon);
#endif
Polynomial.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "Polynomial.h"
using namespace std;
poly_pointer addNode(int coef, int expon)
{
poly_node a;
poly_pointer ptr = &a;
a.coef = coef;
a.expon = expon;
return ptr;
}
poly_pointer read_poly(const char* fileName)
{
poly_pointer start_ptr, curr_ptr;
start_ptr = curr_ptr = addNode(-1, 6);
curr_ptr = curr_ptr->link = addNode(2, 3);
return start_ptr;
}
main.cpp
#include <iostream>
#include "Polynomial.h"
using namespace std;
int main(void)
{
poly_pointer a, b, d, e, f;
a = read_poly("input1.txt");
cout << a->coef;
cout << a->link->coef;
cout << "\n-eop-";
cin.get();
return 0;
}