一个C语言的问题。好像是链表那里错了,但是调试了好多次都没发现问题,求指导
写了个求最大公约数的程序。结果运行的时候是这种情况调试的时候是这样的
点了中断,再继续,就这样了
但是调试的时候,输出的结果是对的
我也不知道是怎么会,搞了半天没发现错误,求帮忙找下错误,谢谢了!
还有输出信息在这里
然后代码:
程序代码:
/**************************************** * Made by Liang Hanlei * * Copyright © Liang Hanlei Reverse * ****************************************/ #include <stdio.h> #include <stdlib.h> #include <malloc.h> typedef struct NUMBERS { struct NUMBERS * front; int no; int num; struct NUMBERS * next; } NUMBERS; int GreatestCommonDivisor(int, int); //To fig out the greatest common divisor void AddList(int, int, NUMBERS *, NUMBERS *); //To add items to list void ChangeMax(int *, int *); //To make the prvious number larger void FreeList(NUMBERS *); int main(void) { NUMBERS * head = NULL; NUMBERS * num = NULL; NUMBERS * prev = NULL; NUMBERS * interate = NULL; //To interate the list int prevNum; //To store the previous number int remainder; //To store the remainder int numTemp; //To store the integer at moment int count = 0; //To count how many integers are entered in int no; char dig; puts("Please enter some integers to cotinue (with \"Space\" button to dig, with \"Enter\" button to quit entering):"); head = (NUMBERS *) malloc(sizeof(NUMBERS)); prev = head; no = 0; while (scanf("%d%c", &numTemp, &dig)) { if (!count && dig == '\n') { puts("Nothing have entered."); exit(1); } ++no; num = (NUMBERS *) malloc(sizeof(NUMBERS *)); AddList(numTemp, no, prev, num); prev = num; if (dig == '\n') { break; } ++count; } num->next = head->next; head->next->front = num; interate = head->next; prevNum = interate->num; no = head->next->no; while (interate->next->no != no) { interate = interate->next; remainder = interate->num; ChangeMax(&prevNum, &remainder); prevNum = GreatestCommonDivisor(prevNum, remainder); if (prevNum == 1) { break; } } printf("The greatest common divisor is %d\n", prevNum); FreeList(head); return 0; } int GreatestCommonDivisor(int prevNum, int remainder) { int prevRemainder; while (remainder) { prevRemainder = remainder; remainder = prevNum % remainder; prevNum = prevRemainder; } return prevNum; } void AddList(int numTemp, int no, NUMBERS * prev, NUMBERS * num) { num->num = numTemp; num->no = no; prev->next = num; num->front = prev; } void ChangeMax(int * prevNum, int * remainder) { int temp; if (*prevNum < *remainder) { temp = *prevNum; *prevNum = *remainder; *remainder = temp; } } void FreeList(NUMBERS * head) { int no; NUMBERS * prev; no = head->next->front->no; prev = head->next; while (prev->no != no) { free(head); head = prev; prev = prev->next; } free(head); free(prev); }