先谢谢各位大佬
我搜了一些资料,知道是因为ptr是NULL才会出现这种情况(应该吧)但是不知道怎么改,求救
先上码
#include <iostream>
using namespace std;
typedef struct node {
int xishu;
int zhishu;
node* next;
}node;
node *start1 = NULL;
node *start2 = NULL;
node *create1(struct node *);
node *create2(struct node *);
node *add(struct node*);
node *display(struct node*);
int main()
{
create1(start1);
//create1(start2);
display(start1);
}
node* create1(struct node* start)
{
struct node* ptr, * new_node;
int coefficient, index;
/×art = new node;
cout << "Enter 0 to end when you entering coefficient"<<endl;
cout << "Enter the coefficient and index"<<endl;
cin >> coefficient;
cin >> index;
//new_node = new node;
while (coefficient != 0) {
new_node = new node;
new_node -> xishu = coefficient;
new_node -> zhishu = index;
if (start == NULL)
{
new_node -> next = NULL;
start = new_node;
}
else {
ptr = start;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = new_node;
new_node->next = NULL;
}
cout << "Enter the coefficient and index" << endl;
cin >> coefficient;
cin >> index;
}
return start;
}
node *display(struct node *start)
{
struct node* ptr;
ptr = start;
while (ptr != NULL);
{
cout << ptr->xishu << "x^" << ptr->zhishu << endl;
ptr->next = ptr;
}
return start;
}
问题如下