谁能帮忙debuging一下,我只知道在特定的情况下会出错,但不知道为什么
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int compare(MailNode *cursorPoint,MailNode *maxPoint)
{
if (maxPoint->year<cursorPoint->year){
return 1;
}
else{
return 0;
}
if (maxPoint->month<cursorPoint->month){
return 1;
}
else{
return 0;
}
if (maxPoint->day<cursorPoint->day){
return 1;
}
else{
return 0;
}
if (maxPoint->hour<cursorPoint->hour){
return 1;
}
else{
return 0;
}
if (maxPoint->minute<cursorPoint->minute){
return 1;
}
else{
return 0;
}
if (maxPoint->second<cursorPoint->second){
return 1;
}
else{
return 0;
}
}
int sort(MailNode *messageList){
MailNode *cursorPoint, *preCursorPoint;
MailNode *maxPoint=NULL, *preMaxPoint;
MailNode *prePoint;
MailNode *temp;
//loop n-2 times the list
currentmailnum=
for(prePoint = messageList;prePoint->next->next != NULL;
prePoint = prePoint->next){
if (maxPoint == NULL){
maxPoint = prePoint;
preMaxPoint = prePoint;
}
else{
maxPoint = prePoint->next;
preMaxPoint = prePoint;
}
for(cursorPoint = maxPoint->next,preCursorPoint = maxPoint;
cursorPoint != NULL;
preCursorPoint = cursorPoint,cursorPoint = cursorPoint ->next){
//if current is big than maxpoint
if(compare(cursorPoint,maxPoint) == 1){
//maxpoint=biger one
maxPoint = cursorPoint;
preMaxPoint = preCursorPoint;
}
}
//printf("%d", maxPoint->year);
if(maxPoint != prePoint->next){
if(prePoint->next!= preMaxPoint){
temp = prePoint->next->next;
prePoint->next->next = maxPoint->next;
maxPoint->next = temp;
preMaxPoint->next = prePoint->next;
prePoint->next = maxPoint;
}
else {
prePoint->next->next = maxPoint->next;
maxPoint->next = prePoint->next;
prePoint->next = maxPoint;
}
}
}
return 0;
}
MailNode *reverse(MailNode *messageList,int mailnum)
{
MailNode *temptail = NULL;
MailNode *temp;
while( messageList != NULL ) {
temp = messageList->next;
messageList->next = temptail;
temptail = messageList;
messageList = temp;
}
return( temptail );
}
// Print the list. The heard is messageList and the tail is NULL.
void printlist(MailNode *messageList, int currentmailnum)
{
int i=1;
MailNode *temp;
// use temporary point *temp read the list from messageList to NULL
for(temp=messageList;temp!=NULL;temp=temp->next){
temp->msgNum = i;
//"->" ponit to current mail
if (i==currentmailnum){
printf("->");
}
else{
printf(" ");
}
printSynopsis( temp );
i++;
}
}
int main(int argc, char* argv[])
{
MailNode *messageList = NULL; // list of messages
MailNode *temp=NULL;
MailNode *lastmessageList=NULL;
FILE *fp = NULL;
int currentmailnum=1;
int mailnum=0;
Boolean showlist=TRUE;
char lastcommand;
char c;
char command[MAXLLENGTH]; // mail client command
char *search;
case 'o': case 'O':
sort(messageList);
if(showlist){
printlist(messageList,currentmailnum);
}
else{
printcurruntmail(currentmailnum,messageList);
}
break;
reverse: case 'r': case 'R':
messageList=reverse(messageList,mailnum);
if(showlist){
printlist(messageList,currentmailnum);
}
else{
printcurruntmail(currentmailnum,messageList);
}
lastcommand='r';
break;
return 0;
}
我可以单独进行case r,或者进行case o并且重复多少遍都没有问题。我也可以先进行case o,紧接着进行case r,但是我却不能进先进行case r,紧接着进行case o.只要先运行case r在运行case o就会有一个死循环。打开gdb看,好像是在sort(messageList)那个函数里的
for(cursorPoint = maxPoint->next,preCursorPoint = maxPoint;
cursorPoint != NULL;
preCursorPoint = cursorPoint,cursorPoint = cursorPoint ->next){
//if current is big than maxpoint
if(compare(cursorPoint,maxPoint) == 1){
//maxpoint=biger one
maxPoint = cursorPoint;
preMaxPoint = preCursorPoint;
}
}
一直转。请问为什么啊