why this program return null nodes?? thanks
Code:
void CopyStudRecordsFromArrayToLinkedList(StudNodePtr *ppList, STUDENT A[],int total)
{ //Pseudocode:
// Declare a "lastnode" pointer, initialized as NULL
// For each record in the array
// Create a new node and setup the node content
// If this is the first node, update *ppList
// Otherwise append it to the end of the linked list (ie. after "lastnode")
// Update "lastnode" to point to the new node
//Your task: Finish this function
StudNodePtr p, pLast, pRtn=NULL;
int i;
for (i=0;i<total;i++)
{ if (p==NULL)
{ printf("Cannot create new node\n");
exit(-1);
}
p = (StudNodePtr)malloc(total*sizeof(struct StudNode));
p->data=A[i];
p->next=NULL;
if (pRtn==NULL)
{ pRtn = p;
pLast = p;
}
else
{ pLast->next=p;
pLast = p;
}
}
void main()
{
#define TOTAL 10
STUDENT A[TOTAL];
int i;
StudNodePtr pList=NULL;
//Setup the data
for (i=0;i<TOTAL;i++)
{ A[i].StudentID=50000000+i;
A[i].ProgramCode=4000+i%3;
}
CopyStudRecordsFromArrayToLinkedList(&pList, A, TOTAL);
}