I'm coding a project —— MIS for students' information.
I use chaintable to organize the data.I have written a function Sort()
to make the data sorted by stuID
(student's ID).However, the point CNode *p
has not been assigned, it is 0x00000000
still.
Now, look at part of my code below.
class CNode
{
friend class CChainTable;
public:
CStudent GetData()
{ return data; }
CNode* GetNext()
{ return next; }
private:
CStudent data;
CNode *next;
};
class CChainTable
{
private:
CNode *pHead;
CNode *pCur;
public:
CChainTable(): pHead(NULL),pCur(NULL)
{}
~CChainTable()
{
CNode *p=pHead;
while(pHead)
{
pHead=p->next;
delete p;
p=pHead;
}
}
void SetCurToHead()
{ pCur=pHead; }
void Sort()
{
SetCurToHead();
if(pHead == NULL){
cout<<"No data in the system."<<endl;
}else{
char stuID_min[10];
CNode *pCurFront,*pFront;
pCurFront=pHead;
CNode *p=pHead;
for(pCur=pHead;pCur!=NULL;pCurFront=pCur,pCur=pCur->next)
{
strcpy( stuID_min,pCur->data.GetStuID() );
for(pFront=pCur,p=pFront;pCur!=NULL;pFront=p,p=p->next)
{
if( strcmp(stuID_min,p->data.GetStuID() )>0 ) {
strcpy( stuID_min,p->data.GetStuID() );
}
}
pCurFront->next=pCur->next;
pFront->next=p->next;
p->next=pCurFront->next;
pCurFront->next=p;
pCur->next=pFront->next;
pFront->next=pCur;
}
cout<<"Result:"<<endl;
ShowAll(); //Print all the data.
}
}
}; /* Structure composing a table tree. */
Then, look at the information in debugging.
p 0x00000000
p->data {...}
pCur 0x00384128
pFront 0x00384230
stuID_min 0x0012fc74
this 0x0012fed8
pCurFront 0x00384128
How can I use the point CNode *p
successfully?