【发布时间】:2010-09-12 14:05:16
【问题描述】:
我正在处理 CS1 的家庭作业,我几乎完成了,但与我尝试实现的一些功能相关的错误不断出现。赋值是使用链表对大整数进行经典的加法和减法。我的问题不在于程序的任何数学功能,而是让链接列表在完成后正确打印。我很确定大部分问题都存在于stripLeadingZeros();功能如下。
/*
* Function stripLeadingZeros
*
* @Parameter STRUCT** Integer
*
* Step through a linked list, recursively unlinking
* all leading zeros and making the first
* non-zero integer the head of the list.
*/
struct integer* stripLeadingZeros( struct integer *p )
{
// Are we at the end of the list?
if( p == NULL ) return NULL;
// Are we deleting the current node?
if( p->digit == 0 )
{
struct integer *pNext;
pNext = p->next;
// Deallocate the node
free( p );
// Return the pointer to the next node
return pNext;
}
// Recurse to make sure next node is not 0
p->next = stripLeadingZeros( p->next );
return p;
}
---///---
/*
* Function print
*
* @Parameter STRUCT* Integer
*
* Given a linked list, will traverse through
* the nodes and print out, one at a time,
* the digits comprising the struct integer that the
* linked list represents.
*
* TODO: Print to file
*/
void print( struct integer *p )
{
struct integer *head = p;
reverse( &p );
p = stripLeadingZeros( p );
while( p )
{
fprintf(outFile, "%d", p->digit);
p = p->next;
}
reverse( &head );
}
---///---
/*
* Function reverse
*
* @Parameter STRUCT** Integer
*
* Recursively reverses a linked list by
* finding the tail each time, and linking the
* tail to the node before it.
*/
void reverse (struct integer **p)
{
/*
* Example p: 1->2->3->4->NULL
*/
if( (*p)->next == NULL ) return;
struct integer *pCurr = *p, *i, *pTail;
// Make pCurr into the tail
while( pCurr->next )
{
i = pCurr;
pCurr = pCurr->next;
}
// Syntactic Sugar
pTail = pCurr;
pTail->next = i;
/*
* p now looks like:
* 1->2->3<->4
*/
i->next = NULL;
/*
* p now looks like:
* 1 -> 2 -> 3 <- 4
* |
* v
* NULL
*/
reverse( p ); // Recurse using p: 1 -> 2 -> 3;
*p = i;
}
我目前得到的整个程序的输出是:
888888888 + 222222222 = 11111111
000000000 - 999999999 = 000000001
000000000 - 999999999 = 000000001
而预期的输出是
8888888888 + 2222222222 = 11111111110
10000000000 – 9999999999 = 1
10000000000 – 9999999999 = 1
任何人都可以提供的任何帮助都很棒;我已经为此工作了很长时间,如果我有任何头发,我现在已经把它拔掉了。
EDIT我的read_integer函数如下:
/*
* Function read_integer
*
* @Parameter CHAR* stringInt
*
* Parameter contains a string representing a struct integer.
* Tokenizes the string by each character, converts each char
* into an integer, and constructs a backwards linked list out
* of the digits.
*
* @Return STRUCT* Integer
*/
struct integer* read_integer( char* stringInt )
{
int i, n;
struct integer *curr, *head;
int numDigits = strlen( stringInt ); // Find the length of the struct integer
head = NULL;
for( i = 0; i < numDigits; i++ )
{
n = stringInt[i] - '0'; // Convert char to an integer
curr = (struct integer *) malloc (sizeof( struct integer )); // Allocate memory for node
curr->digit = n; // Digit of current node is assigned to n
curr->next = head; // Move to the next node in the list.
head = curr; // Move head up to the front of the list.
}
return head; // Return a pointer to the first node in the list.
}
【问题讨论】:
-
@Andrew,与昨天的问题相同的 cmets 适用:您遇到的错误是什么以及代码在哪里?你有所有警告吗?编译器说什么?您尝试了什么... 清楚而系统地写下这些内容将使您有机会几乎自己找到错误
-
哈哈哈天哪,对不起。我的睡眠几乎为零。我修复了这个问题,包括输出与预期输出。
-
嗨,只是对您的 stripLeadingZeros 函数的猜测,实际上您在 while 循环中只删除了一个零,因为您通过在每个循环循环中返回 pNext 而跳出函数,没有任何条件。第二个猜测(这可能是错误的 - 取决于您的实现):您在删除前导零之前反转列表,因此如果数字处于“正确”顺序,则从错误的一端删除零。
-
如果您在第一个循环后返回,为什么在
stripLeadingZeros中使用 while 循环而不是 if? -
@Cristian 实际上我刚刚从
if切换到while只是为了看看它是否有效。 ://
标签: c linked-list