- #1
rohanprabhu
- 414
- 2
I've tried to implement a list within a class. I did this so that I can make 2 or more objects, where each object has a list as it's member. Here is the code to it:
And i accept data by:
Whenever additionalNode is called, it shows 'Creating new node..', and 'new node created...' messages, as I've written in the additionalNode function [for error checking]. but, after that, when the program tries to run the Insert_End function, the program fives an error, 'The program has encountered a problem and needs to close' and closes.
Please help...
Code:
struct coeffecient {
int num;
int den;
};
struct polynomial {
float power;
coeffecient coeff;
int polarity;
};
/* Linked List definitions and implementation */
struct Node {
polynomial info;
Node *next;
};
class algebraList {
public:
Node *start, *newptr, *save, *ptr, *rear;
void Insert_End(Node *np) {
if(start == NULL) {
start = rear = np;
} else {
rear->next = np;
rear = np;
}
}
void Display(Node *np) {
while(np != NULL) {
cout<<np->info.power<<" -> ";
np = np->next;
}
cout<<"\n";
}
void additionalNode(polynomial x) {
newptr = createNewNode(x);
if(newptr != NULL) {
cout<<"new node created..."; getch();
} else {
cout<<"error creating node.."; getch(); exit(1);
}
cout<<"inserting node in list..."; getch();
Insert_End(newptr);
}
Node * createNewNode(polynomial n) {
ptr = new Node;
ptr->info = n;
ptr->next = NULL;
return ptr;
}
};
And i accept data by:
Code:
polynomial x;
cout<<"\n Enter power for the new node: ";
cin>>x.power;
cout<<"creating new node.. "; getch();
listOne.additionalNode(x);
Whenever additionalNode is called, it shows 'Creating new node..', and 'new node created...' messages, as I've written in the additionalNode function [for error checking]. but, after that, when the program tries to run the Insert_End function, the program fives an error, 'The program has encountered a problem and needs to close' and closes.
Please help...