所以我一直在练习链接列表,我编写了一个可以完美运行的代码,然后又编写了另一个与我的第一个代码几乎相同但目标不同的代码,问题是当我运行第二个代码时循环然后停止。 (问题实际上是使用了malloc)
这是我的第一个代码:
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <iomanip>
struct student
{
string name;
int age;
double grade;
student *next;
};
student *head;
void createList(int n)
{
student *newNode,*temp;
int i;
string name_temp;
int age_temp;
double grade_temp;
head=(student *) malloc(sizeof(student));
if(head==NULL)
{
cout<<("Unable to alocate memory");
}
cout<<"Enter student's initials: ";
cin>>name_temp;
cout<<"Enter student's age: ";
cin>>age_temp;
cout<<"Enter Student's grade";
cin>>grade_temp;
head->name=name_temp;
head->age=age_temp;
head->grade=grade_temp;
head->next=NULL;
temp=head;
for(i=2;i<=n;i++)
{
newNode=(student *)malloc(sizeof(student));
if(newNode==NULL)
{
cout<<"unable to allocate memory";
break;
}
cout<<"Enter the data of node "<<i<<":\n";
cout<<"Enter student's initials: ";
cin>>name_temp;
cout<<"Enter student's age: ";
cin>>age_temp;
cout<<"Enter Student's grade";
cin>>grade_temp;
newNode->name=name_temp;
newNode->age=age_temp;
newNode->grade=grade_temp;
newNode->next=NULL;
temp->next=newNode;
temp=temp->next;
}
}
void traverseList()
{
student *cond;
if(head==NULL)
{
cout<<"List is empty";
return;
}
cond=head;
cout<<"NAME:\t\t\tAGE:\t\tGRADE:\n";
while(cond!=NULL)
{
cout<<cond->name<<"\t\t\t"<<cond->age<<"\t\t"<<fixed<<setprecision(2)<<cond->grade<<endl;
cond=cond->next;
}
}
int main() {
int n;
cout <<"Enter the total Number of Student Record: ";
cin>>n;
createList(n);
cout<<"\nData in the list\n";
traverseList();
return 0;
}
这是我的第二个代码:
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <iomanip>
struct staff
{
string name,pos;
double sal;
staff *next;
};
staff *admin;
void createList(int n)
{
staff *nodes,*temp;
int i;
string name_temp;
string pos_temp;
double sal_temp;
admin=(staff *)malloc(sizeof(staff));//
if(admin==NULL)
{
cout<<"unable to allocate memory";
}
cout<<"Enter Employee's name: ";
cin>>name_temp;
cout<<"Enter Employee's Position: ";
cin>>pos_temp;
cout<<"Enter Employee's Salary: ";
cin>>sal_temp;
admin->name=name_temp;
admin->pos=pos_temp;
admin->sal=sal_temp;
admin->next=NULL;
temp=admin;
for(i=2;i<=n;i++)
{
nodes=(staff*)malloc(sizeof(staff));
if(nodes==NULL)
{
cout<<"Unable to allocate memory";
}
cout<<"Enter the data of node "<<i<<":\n";
cout<<"Enter Employee's name: ";
cin>>name_temp;
cout<<"Enter Employee's Position: ";
cin>>pos_temp;
cout<<"Enter Employee's Salary: ";
cin>>sal_temp;
nodes->name=name_temp;
nodes->pos=pos_temp;
nodes->sal=sal_temp;
nodes->next=NULL;
temp->next=nodes;
temp=temp->next;
}
}
void viewList()
{
double total=0;
staff *check;
if(admin==NULL)
{
cout<<"No list";
}
check=admin;
cout<<"NAME:\t\tPosition\t\tSalary\n";
while(check!=NULL)
{
cout<<check->name<<"\t\t"<<check->pos<<"\t\t"<<fixed<<setprecision(2)<<check->sal<<endl;
total=total+check->sal;
cout<<check->name<<"\t\t\t\ttotal: "<<total<<endl;
check=check->next;
}
}
int main()
{
int n;
cout<<"Enter Number of employees: ";
cin>>n;
createList(n);
cout<<"\nin the list is\n";
viewList();
return 0;
}