Thanks, quzah. You are right, I'd better show my purpose and make my code more clear next time. I found the problem when I was brushing my teeth this morning, the fault is: while I am trying to insert an element into the list, the 'Position P' I passed to the function may be the last node of the list, at first, the list had only one node: The Header(also the last node). I use PORNEXT to point the next node of that one, is actually NULL, so, the assignment 'PORNEXT->Last = TmpCell' will fail without doubt. I modified the code and it works.
Quzah, thank you for your help .
This is the insert funtion I modified:
Code:
void
Insert( ElementType X, List L, Position P )
{
Position TmpCell, PORNEXT; // original P->Next: PORNEXT
TmpCell = malloc( sizeof( struct Node ) );
if( TmpCell == NULL )
FatalError( "Out of space!!!" );
if(P->Next == NULL)
{
TmpCell->Element = X;
TmpCell->Next = P->Next;
TmpCell->Last = P;
P->Next = TmpCell;
}
else
{
PORNEXT = P->Next;
TmpCell->Element = X;
TmpCell->Next = P->Next;
TmpCell->Last = P;
P->Next = TmpCell;
PORNEXT->Last = TmpCell;
}
}
The insertion sort using linked-list:
Code:
void
InsertionSortList(List L)
{
int num, status;
Position P, P1, P2;
ElementType temp;
FILE *inp;
inp = fopen("data10000.txt", "r");
for(P = Header(L); status != EOF; P = P->Next)
{
status = fscanf(inp, "%d", &num);
Insert(num, L, P);
}
for(P1 = Header(L)->Next; P1->Next != NULL; P1 = P1->Next)
{
temp = P1->Element;
for(P2 = P1; (P2->Last != NULL) && (P2->Last->Element > temp); P2 = P2->Last)
{
P2->Element = P2->Last->Element;
}
P2->Element = temp;
}
}