2009-05-06 72 views
0

值作为转让的一部分,我需要写两个函数:打印删除从我的链表

  1. 凝聚了表示为链表
  2. 一个函数的两个自然数的函数打印以相同方式表示的数字。

由于某些原因,这两个函数都能够单独完美地工作,但是当我试图在sum函数的结果上使用print函数时,它会在打印函数的开始处改变右值的总和,并打印错误的值。当我使用printf在main中打印相同的值时,没有任何问题。我的代码在下面详细介绍。有任何想法吗?

void main() 
{ 
    int a[1] = { 1 }, 
    b[1] = { 2 }; 
    int * *pa, **pb; 
    List lst1, lst2; 
    List sum; 

    pa = (int * *) malloc(sizeof(int *)); * pa = &a[0]; 
    pb = (int * *) malloc(sizeof(int *)); * pb = &b[0]; 
    lst1 = arrToList(pa, 1); 
    lst2 = arrToList(pb, 1); 
    addNumbers(lst1, lst2, &sum); 
    //printf("%d\n",*(sum.head->dataPtr)); 
    printNumber(sum); 
} 

//a function that recieves a number represented ad a list and prints it 
void printNumber(List num) 
{ 
    ListNode * curr; 
    int currData, 
    i, 
    number; 

    if (isEmptyList(num) == TRUE) 
    printf("the input was an empty list, nothing to print"); 
    else 
    { 
    i = 0; 
    number = 0; 
    curr = num.head; 
    while (curr != NULL) 
    { 
     currData = *(curr - >dataPtr); 
     number = number + currData * ((int) pow(10, i)); 
     curr = curr - >next; 
     i++; 
    } 
    printf("%d \n", number); 
    } 
} 

// a function that sums in list 
// representation two numbers, 
// each represented as a list 
void addNumbers(List n1, List n2, List * sum) 
{ 
    ListNode * currN1; 
    ListNode * currN2; 
    ListNode * currSum; 
    int currN1N2Sum; //stores the sum of the current digits in n1 and n2 
    int carrier, 
    prevCarrier; //current and previous carriers that carries +1 to the 
    next digit of sum 
    if the lst sum was bigger then 9 

    if ((isEmptyList(n1) == TRUE) || (isEmptyList(n2) == TRUE)) 
    printf("bad input =("); 
    else 
    { 
    currN1 = n1.head; 
    currN2 = n2.head; * sum = createEmptyList(); 
    carrier = 0; 
    prevCarrier = 0; 
    while ((currN1 != NULL) && (currN2 != NULL)) 
    { 
     currN1N2Sum = *(currN1->dataPtr) + *(currN2->dataPtr) + prevCarrier; 
     if (currN1N2Sum > 9) 
     { 
     carrier = 1; 
     currN1N2Sum = currN1N2Sum - 10; 
     } 
     currSum = creatNewListNode(& currN1N2Sum, NULL); 
     insertNodeToEnd(sum, currSum); 
     prevCarrier = carrier; 
     carrier = 0; 
     currN1 = currN1 - >next; 
     currN2 = currN2 - >next; 
    } //while ((currL1!=NULL)&&(currL2!=NULL)) 

    while (currN1 != NULL) 
    { 
     currN1N2Sum = *(currN1 - >dataPtr) + prevCarrier; 
     currN1 = currN1 - >next; 
     if (prevCarrier != 0) prevCarrier = 0; 
    } 

    while (currN2 != NULL) 
    { 
     currN1N2Sum = *(currN2 - >dataPtr) + prevCarrier; 
     currN2 = currN2 - >next; 
     if (prevCarrier != 0) prevCarrier = 0; 
    } 
    } // ! ((isEmptyList(n1)==TRUE)||(isEmptyList(n2)==TRUE)) 
} 

这里是代码的其余部分:

typedef struct listNode{ 
int* dataPtr; 
struct listNode* next; 
} ListNode; 

typedef struct list 
{ 
ListNode* head; 
ListNode* tail; 
} List; 

List createEmptyList()//creates and returns an empty linked list 
{ 
    List res; 

    res.head = res.tail = NULL; 

    return res; 
} 

Bool isEmptyList (List lst)//checks if a given list is empty or not 
{ 
    if (lst.head == NULL && lst.tail == NULL) 
     return TRUE; 
    else 
     return FALSE; 
} 

void insertDataToEnd (List * lst, int *dataPtr) //inserts new data to the end of an existing linked list 
{ 
    ListNode * newTail; 
    newTail = creatNewListNode (dataPtr, NULL); 
    insertNodeToEnd(lst,newTail); 
} 

void insertNodeToEnd (List * lst, ListNode * newTail)//insert an existing node to an existing linked list 
{ 
    if (isEmptyList(*lst) == TRUE) 
     insertNodeToStart (lst,newTail); 
    else 
    { 
     (*lst).tail -> next = newTail; 
     newTail->next = NULL; 
     (*lst).tail = newTail; 
    } 
} 


ListNode * creatNewListNode (int * dataPtr, ListNode * next)//inserts new node in an existing linked list 
{ 
    ListNode * res; 

    res = (ListNode *) malloc (sizeof(ListNode)); 

    res -> dataPtr = dataPtr; 
    res -> next  = next; 

    return res; 
} 

void insertNodeToStart (List * lst, ListNode * newHead)//inserts node to the begining of a given linked list 
{ 
    if (isEmptyList(*lst) == TRUE) 
    { 
     (*lst).head = newHead; 
     (*lst).tail = newHead; 
     newHead -> next = NULL; 
    } 
    else 
    { 
     newHead -> next = (*lst).head; 
     (*lst).head = newHead; 
    } 
} 
+0

当添加2 + 1时,你会得到什么输出?你说什么“改变”? – ParoXoN 2009-05-06 17:18:20

+0

你的代码很难编译。没有称为Bool的类型,或arrToList的定义。 – dirkgently 2009-05-06 17:53:57

回答

5

bug是在功能的addNumbers。 当您添加一个节点来存储总和时,您将一个指针传递给作为本地变量(存储在堆栈中)的变量currN1N2Sum。当addNumbers函数终止时,局部变量的存储空间被设置为空闲。在该位置找到的值将保持不变,因此只要存储未被重用,该值显然是有效的。

这就是为什么你有印象addNumbers功能是正确的。当调用printNumber函数时,存储将被覆盖,并在那里找到不同的值。

这解释了你的错误。

addNumbers还有另一个问题。当您尝试添加两位数字时,currN1N2Sum的内容将被新值覆盖。

你应该做的是分配一个缓冲区(malloc)并将包含在currN1N2Sum中的值存储到它中。将指针传递给缓冲区到新节点中。

顺便说一句:你可能会改变(* lst)。头在lst->头。它会使你的代码更具可读性。

0

我们需要看到更多的代码:你如何定义的数据保持节点结构,如何添加节点等

以下行是可疑的:

number=number+currData*((int)pow(10,i)); 

说,你有123存储为1,2,和3个节点:

number = 0; 
    number = 0 + 1 * 1 = 1; 
    number = 1 + 2 * 10 = 21; 
    number = 21 + 3 * 100 = 321; 

但是,如果你保存是3,2,1个节点上,您会得到:

number = 0; 
    number = 0 + 3 * 1 = 3; 
    number = 3 + 2 * 10 = 23; 
    number = 23 + 1 * 100 = 123; 

这是你的问题?

+0

问题是,通过调试,我知道该值在函数的开头就已经丢失,甚至在行之前: if(isEmptyList(num)== TRUE) – Lior 2009-05-06 17:13:59

+0

向我们展示List的定义,整个代码。 – dirkgently 2009-05-06 17:16:29

0

我不是,如果这是没有看到的createNewListNode()执行情况的问题或没有,但这里的一些思考:
哪里dataPtr S IN的sum列表指点你从addNumbers()调用返回后?

+0

dataPtr从addnumbers()返回后指向0x0012fdf4。它在进入打印功能时指向相同的地址,但是该值更改为-858993460,并且currData,i和数字具有相同的值。 – Lior 2009-05-06 17:32:22

0

我觉得你可能会把事情弄乱了......你在addNumbers中分配列表'sum'的方式似乎很奇怪。 (我如果它打破东西也不会感到惊讶...)

尝试做了这些改变:

在主:

List *sum; 
<...> 
addNumbers(lst1,lst2,sum); //Note the absence of the reference operator & 
printNumbers(*sum); 

(或者,改变printNumbers接受(表*)而不是(List))。

希望这有助于XD


编辑:

尝试拨打电话到的addNumbers之前分配 '和'()。

lst1 = arrToList(pa, 1); 
lst2 = arrToList(pb, 1); 
sum = createEmptyList(); 

我还是认为你的数据结构是一个有点古怪的方式:S

0

您遇到了createEmptyList的问题。你在那里声明一个名为res的列表并返回结构,但是这个函数返回的那一刻结构就不再有效了。 尝试使用malloc(对于结构体),然后将指针返回给调用者。您在* sum开头使用此函数。

这是一个和chmike发现的类似的bug,所以你最好修复两者。