C 不太有用的错误——作业中的分段错误(核心转储)
编译包含此特定函数的程序时C 不太有用的错误——作业中的分段错误(核心转储),c,segmentation-fault,C,Segmentation Fault,编译包含此特定函数的程序时 /* * 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 l
/*
* 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
char *tok;
head = NULL;
for( i = 0; i < numDigits; i++ )
{
tok = strtok( stringInt, NULL ); // Tokenize the string by each char
n = atoi( tok[i] ); // 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.
}
我尝试了几种不同的方法(大部分是在黑暗中拍摄)来消除警告,但没有效果。谁能给我指出正确的方向吗
---///---
下面是一个原始的、愚蠢的问题,左边是因为我不想成为那个家伙---
我正在试着调试CS1的作业,这样我就可以完成它并开始做一些有趣的事情(比如我的CLisp研究),但我遇到了一个无法克服的错误。现在,我知道作业还没有完成(也就是说,即使我让它运行,它也不会做我想做的事情),但我不想得到帮助(此外,那会有什么乐趣?)。如果有人能帮我弄清楚为什么下面的代码在编译和执行时会产生无用的(对我来说)分段错误(内核转储)
,那就太棒了
/*
* File: struct integer.c
* Description: Assignment in using Linked Lists
* in order to perform struct integer addition and subtraction.
*
* Created on September 1, 2010, 11:38 AM
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Constant Definitions
#define ADD 1
#define SUB 2
// Output file
char *fName = "out.txt";
FILE *outFile;
/*
* Create a prototype of a single
* node in the Linked List.
* Each node will represent a single
* integer comprising one part of a struct integer.
*/
struct integer
{
int digit;
struct integer *next;
};
// Function Prototypes
struct integer* read_integer( char *stringInt );
struct integer* add( struct integer *p, struct integer *q );
struct integer* subtract( struct integer *p, struct integer *q);
int compare( struct integer *p, struct integer *q );
void print( struct integer *p );
// Main function
int main( )
{
//Variable initialization
/*
* Initialize pointers to the linked lists.
* One, *head, will always point to the
* first element, the head, of the list.
* The other element, *curr, will point to the
* node currently being accessed, and will be
* used to traverse the list.
*/
struct integer* pHead;
struct integer* qHead;
struct integer* tHead; // Used to contain the total
int numOps, oper, i;
const char *fileName = "struct integer.txt";
char bigintstr[200];
FILE *inputFile;
// Open output file
outFile = fopen(fName, "a+");
// Open up the input file for reading
inputFile = fopen(fileName, "r");
// Read in the number of operations to be performed
fscanf(inputFile, "%d", &numOps);
/*
* For each operation that must be performed,
* construct a linked list for each of the
* struct integers in the file. Then, perform the operation
* indicated by the digit preceding them.
*/
for( i = 0; i < numOps; i++ )
{
// Read in the number that dictates operation
fscanf(inputFile, "%d", &oper);
// Read in the first struct integer into a string
fscanf(inputFile, "%s", bigintstr);
/*
* Pass the struct integer string to read_integer()
* in order to construct a linked list out of it.
*/
pHead = read_integer( bigintstr );
// Read in second struct integer into a string
fscanf(inputFile, "%s", bigintstr);
/*
* Pass the struct integer str to read_integer()
* in order to construct a linked list out of it.
*/
qHead = read_integer( bigintstr );
/*
* Depending on the operation to be performed,
* call the corresponding function.
*/
switch( oper )
{
case ADD:
tHead = add( pHead, qHead );
break;
case SUB:
tHead = subtract( pHead, qHead );
break;
default:
printf("Invalid operation parameter.\n");
}
print( pHead ); // Print out the first struct integer
fprintf(outFile, " + ");
print( qHead ); // Print out the second struct integer
fprintf(outFile, " = ");
print( tHead ); // Print out the sum/difference struct integer
fprintf(outFile, "\n"); // Move to next line for next instruction set
}
fclose(inputFile);
//system(PAUSE);
return 0;
}
// Function Definitions
/*
* 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
char *tok;
head = NULL;
for( i = 0; i < numDigits; i++ )
{
tok = strtok( stringInt, NULL ); // Tokenize the string by each char
n = atoi( tok[i] ); // 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.
}
/*
* 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 )
{
while( p )
{
fprintf(outFile, "%d", p->digit);
p = p->next;
}
}
/*
* Function add
*
* @Paramater STRUCT* Integer
* @Parameter STRUCT* Integer
*
* Takes two linked lists representing
* big integers stored in reversed order,
* and returns a linked list containing
* the sum of the two integers.
*
* @Return STRUCT* Integer
*
* TODO Comment me
*/
struct integer* add( struct integer *p, struct integer *q )
{
int carry = 0;
struct integer *sHead, *sCurr;
struct integer *pHead, *qHead;
pHead = p;
qHead = q;
sHead = NULL;
while( p )
{
sCurr = ( struct integer* ) malloc (sizeof(struct integer));
sCurr->digit = p->digit + q->digit + carry;
sCurr->next = sHead;
sHead = sCurr;
carry = 0;
/*
* If the current digits sum to greater than 9,
* create a carry value and replace the current
* value with value mod 10.
*/
if( sCurr->digit > 9 )
{
carry = 1;
sCurr->digit = sCurr->digit % 10;
}
/*
* If the most significant digits of the numbers
* sum to 10 or greater, create an extra node
* at the end of the sum list and assign it the
* value of 1.
*/
if( carry == 1 && sCurr->next == NULL )
{
struct integer *sCarry = ( struct integer* ) malloc (sizeof(struct integer));
sCarry->digit = 1;
sCarry->next = NULL;
sCurr->next = sCarry;
}
p = p->next;
q = q->next;
}
return sHead;
}
/*
* Function subtract
*
* @Parameter STRUCT* Integer
* @Parameter STRUCT* Integer
*
* Takes two linked lists representing struct integers.
* Traverses through the lists, subtracting each
* digits from the subsequent nodes to form a new
* struct integer, and then returns the newly formed
* linked list.
*
* @Return STRUCT* Integer
*
* TODO Comment me
*/
struct integer* subtract( struct integer *p, struct integer *q )
{
int carry = 0;
struct integer *dHead, *dCurr;
struct integer *pHead, *qHead;
pHead = p;
qHead = q;
dHead = NULL;
while( p )
{
dCurr = (struct integer*) malloc (sizeof(struct integer));
dCurr->digit = p->digit - q->digit - carry;
dCurr->next = dHead;
dHead = dCurr;
if( dCurr->digit < 0 )
{
dCurr->digit += 10;
carry = 1;
}
if( dCurr->next == NULL && carry == 1 )
{
struct integer *dCarry = (struct integer*) malloc (sizeof(struct integer));
dCarry->digit = -1;
dCarry->next = NULL;
dCurr->next = dCarry;
}
p = p->next;
q = q->next;
}
return dHead;
}
/*
* Function compare
*
* @Parameter STRUCT* Integer
* @Parameter STRUCT* Integer
*
* Takes in two linked lists representing struct integers.
* Traverses the lists one at a time, comparing the
* digits.
*
* Case: p < q
* @Return -1
*
* Case: p == q
* @Return 0
*
* Case: p > q
* @Return 1
*
* TODO Comment me
*/
int compare( struct integer *p, struct integer *q )
{
struct integer *pHead, *qHead;
int comp = 0;
pHead = p;
qHead = q;
while( p )
{
if( p->digit > q->digit )
{
comp = 1;
}
if( p->digit < q->digit )
{
comp = -1;
}
p = p->next;
q = q->next;
}
return comp;
}
/*
*文件:struct integer.c
*说明:使用链接列表中的指定
*以执行结构整数加法和减法。
*
*创建于2010年9月1日上午11:38
*/
#包括
#包括
#包括
//恒定定义
#定义添加1
#定义子2
//输出文件
char*fName=“out.txt”;
文件*输出文件;
/*
*创建单个对象的原型
*链接列表中的节点。
*每个节点将代表单个节点
*包含结构整数的一部分的整数。
*/
结构整数
{
整数位数;
结构整数*下一步;
};
//功能原型
结构整数*读取整数(字符*stringInt);
结构整数*add(结构整数*p,结构整数*q);
结构整数*减法(结构整数*p,结构整数*q);
int比较(结构整数*p,结构整数*q);
无效打印(结构整数*p);
//主要功能
int main()
{
//变量初始化
/*
*初始化指向链接列表的指针。
*一个,*头,总是指向
*第一个元素,列表的头。
*另一个元素*curr将指向
*当前正在访问的节点,并且将
*用于遍历列表。
*/
结构整数*pHead;
结构整数*qHead;
struct integer*tHead;//用于包含总数
int numOps,oper,i;
const char*fileName=“struct integer.txt”;
char-bigintstr[200];
文件*输入文件;
//打开输出文件
outFile=fopen(fName,“a+”);
//打开输入文件进行读取
inputFile=fopen(文件名,“r”);
//读入要执行的操作数
fscanf(输入文件“%d”和numOps);
/*
*对于必须执行的每个操作,
*为每个
*然后,执行该操作
*由前面的数字表示。
*/
对于(i=0;idigit=n;//当前节点的数字分配给n
curr->next=head;//移动到列表中的下一个节点。
head=curr;//将头向上移动到列表的前面。
}
return head;//返回指向列表中第一个节点的指针。
}
/*
*函数打印
*
*@Parameter结构*Integer
*
*给定一个链表,将遍历
*一次打印一个节点,
*包含结构整数的数字,该结构整数
*链表表示。
*
*TODO:打印到文件
*/
无效打印(结构整数*p)
{
while(p)
{
fprintf(输出文件,“%d”,p->数字);
p=p->next;
}
}
/*
*函数添加
*
*@Paramater结构*Integer
*@Parameter结构*Integer
*
*取两个链表
/*
* File: struct integer.c
* Description: Assignment in using Linked Lists
* in order to perform struct integer addition and subtraction.
*
* Created on September 1, 2010, 11:38 AM
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Constant Definitions
#define ADD 1
#define SUB 2
// Output file
char *fName = "out.txt";
FILE *outFile;
/*
* Create a prototype of a single
* node in the Linked List.
* Each node will represent a single
* integer comprising one part of a struct integer.
*/
struct integer
{
int digit;
struct integer *next;
};
// Function Prototypes
struct integer* read_integer( char *stringInt );
struct integer* add( struct integer *p, struct integer *q );
struct integer* subtract( struct integer *p, struct integer *q);
int compare( struct integer *p, struct integer *q );
void print( struct integer *p );
// Main function
int main( )
{
//Variable initialization
/*
* Initialize pointers to the linked lists.
* One, *head, will always point to the
* first element, the head, of the list.
* The other element, *curr, will point to the
* node currently being accessed, and will be
* used to traverse the list.
*/
struct integer* pHead;
struct integer* qHead;
struct integer* tHead; // Used to contain the total
int numOps, oper, i;
const char *fileName = "struct integer.txt";
char bigintstr[200];
FILE *inputFile;
// Open output file
outFile = fopen(fName, "a+");
// Open up the input file for reading
inputFile = fopen(fileName, "r");
// Read in the number of operations to be performed
fscanf(inputFile, "%d", &numOps);
/*
* For each operation that must be performed,
* construct a linked list for each of the
* struct integers in the file. Then, perform the operation
* indicated by the digit preceding them.
*/
for( i = 0; i < numOps; i++ )
{
// Read in the number that dictates operation
fscanf(inputFile, "%d", &oper);
// Read in the first struct integer into a string
fscanf(inputFile, "%s", bigintstr);
/*
* Pass the struct integer string to read_integer()
* in order to construct a linked list out of it.
*/
pHead = read_integer( bigintstr );
// Read in second struct integer into a string
fscanf(inputFile, "%s", bigintstr);
/*
* Pass the struct integer str to read_integer()
* in order to construct a linked list out of it.
*/
qHead = read_integer( bigintstr );
/*
* Depending on the operation to be performed,
* call the corresponding function.
*/
switch( oper )
{
case ADD:
tHead = add( pHead, qHead );
break;
case SUB:
tHead = subtract( pHead, qHead );
break;
default:
printf("Invalid operation parameter.\n");
}
print( pHead ); // Print out the first struct integer
fprintf(outFile, " + ");
print( qHead ); // Print out the second struct integer
fprintf(outFile, " = ");
print( tHead ); // Print out the sum/difference struct integer
fprintf(outFile, "\n"); // Move to next line for next instruction set
}
fclose(inputFile);
//system(PAUSE);
return 0;
}
// Function Definitions
/*
* 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
char *tok;
head = NULL;
for( i = 0; i < numDigits; i++ )
{
tok = strtok( stringInt, NULL ); // Tokenize the string by each char
n = atoi( tok[i] ); // 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.
}
/*
* 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 )
{
while( p )
{
fprintf(outFile, "%d", p->digit);
p = p->next;
}
}
/*
* Function add
*
* @Paramater STRUCT* Integer
* @Parameter STRUCT* Integer
*
* Takes two linked lists representing
* big integers stored in reversed order,
* and returns a linked list containing
* the sum of the two integers.
*
* @Return STRUCT* Integer
*
* TODO Comment me
*/
struct integer* add( struct integer *p, struct integer *q )
{
int carry = 0;
struct integer *sHead, *sCurr;
struct integer *pHead, *qHead;
pHead = p;
qHead = q;
sHead = NULL;
while( p )
{
sCurr = ( struct integer* ) malloc (sizeof(struct integer));
sCurr->digit = p->digit + q->digit + carry;
sCurr->next = sHead;
sHead = sCurr;
carry = 0;
/*
* If the current digits sum to greater than 9,
* create a carry value and replace the current
* value with value mod 10.
*/
if( sCurr->digit > 9 )
{
carry = 1;
sCurr->digit = sCurr->digit % 10;
}
/*
* If the most significant digits of the numbers
* sum to 10 or greater, create an extra node
* at the end of the sum list and assign it the
* value of 1.
*/
if( carry == 1 && sCurr->next == NULL )
{
struct integer *sCarry = ( struct integer* ) malloc (sizeof(struct integer));
sCarry->digit = 1;
sCarry->next = NULL;
sCurr->next = sCarry;
}
p = p->next;
q = q->next;
}
return sHead;
}
/*
* Function subtract
*
* @Parameter STRUCT* Integer
* @Parameter STRUCT* Integer
*
* Takes two linked lists representing struct integers.
* Traverses through the lists, subtracting each
* digits from the subsequent nodes to form a new
* struct integer, and then returns the newly formed
* linked list.
*
* @Return STRUCT* Integer
*
* TODO Comment me
*/
struct integer* subtract( struct integer *p, struct integer *q )
{
int carry = 0;
struct integer *dHead, *dCurr;
struct integer *pHead, *qHead;
pHead = p;
qHead = q;
dHead = NULL;
while( p )
{
dCurr = (struct integer*) malloc (sizeof(struct integer));
dCurr->digit = p->digit - q->digit - carry;
dCurr->next = dHead;
dHead = dCurr;
if( dCurr->digit < 0 )
{
dCurr->digit += 10;
carry = 1;
}
if( dCurr->next == NULL && carry == 1 )
{
struct integer *dCarry = (struct integer*) malloc (sizeof(struct integer));
dCarry->digit = -1;
dCarry->next = NULL;
dCurr->next = dCarry;
}
p = p->next;
q = q->next;
}
return dHead;
}
/*
* Function compare
*
* @Parameter STRUCT* Integer
* @Parameter STRUCT* Integer
*
* Takes in two linked lists representing struct integers.
* Traverses the lists one at a time, comparing the
* digits.
*
* Case: p < q
* @Return -1
*
* Case: p == q
* @Return 0
*
* Case: p > q
* @Return 1
*
* TODO Comment me
*/
int compare( struct integer *p, struct integer *q )
{
struct integer *pHead, *qHead;
int comp = 0;
pHead = p;
qHead = q;
while( p )
{
if( p->digit > q->digit )
{
comp = 1;
}
if( p->digit < q->digit )
{
comp = -1;
}
p = p->next;
q = q->next;
}
return comp;
}
tok = strtok( stringInt, NULL ); // Tokenize the string by each char
n = atoi( tok[i] ); // Convert char to an integer
n = stringInt - '0'; // Convert char to an integer
/*
* File: struct integer.c
* Description: Assignment in using Linked Lists
* in order to perform struct integer addition and subtraction.
*
* Created on September 1, 2010, 11:38 AM
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Constant Definitions
#define ADD 1
#define SUB 2
// Output file
char *fName = "out.txt";
FILE *outFile;
/*
* Create a prototype of a single
* node in the Linked List.
* Each node will represent a single
* integer comprising one part of a struct integer.
*/
struct integer
{
int digit;
struct integer *next;
};
// Function Prototypes
struct integer* read_integer( char *stringInt );
struct integer* add( struct integer *p, struct integer *q );
struct integer* subtract( struct integer *p, struct integer *q);
int compare( struct integer *p, struct integer *q );
void print( struct integer *p );
// Main function
int main( )
{
//Variable initialization
/*
* Initialize pointers to the linked lists.
* One, *head, will always point to the
* first element, the head, of the list.
* The other element, *curr, will point to the
* node currently being accessed, and will be
* used to traverse the list.
*/
struct integer* pHead;
struct integer* qHead;
struct integer* tHead; // Used to contain the total
int numOps, oper, i;
const char *fileName = "struct_integer.txt";
char bigintstr[200];
FILE *inputFile;
// Open output file
outFile = fopen(fName, "a+");
// Open up the input file for reading
inputFile = fopen(fileName, "r");
// Read in the number of operations to be performed
fscanf(inputFile, "%d", &numOps);
/*
* For each operation that must be performed,
* construct a linked list for each of the
* struct integers in the file. Then, perform the operation
* indicated by the digit preceding them.
*/
for( i = 0; i < numOps; i++ )
{
// Read in the number that dictates operation
fscanf(inputFile, "%d", &oper);
// Read in the first struct integer into a string
fscanf(inputFile, "%s", bigintstr);
/*
* Pass the struct integer string to read_integer()
* in order to construct a linked list out of it.
*/
pHead = read_integer( bigintstr );
// Read in second struct integer into a string
fscanf(inputFile, "%s", bigintstr);
/*
* Pass the struct integer str to read_integer()
* in order to construct a linked list out of it.
*/
qHead = read_integer( bigintstr );
/*
* Depending on the operation to be performed,
* call the corresponding function.
*/
switch( oper )
{
case ADD:
tHead = add( pHead, qHead );
break;
case SUB:
tHead = subtract( pHead, qHead );
break;
default:
printf("Invalid operation parameter.\n");
}
print( pHead ); // Print out the first struct integer
fprintf(outFile, " + ");
print( qHead ); // Print out the second struct integer
fprintf(outFile, " = ");
print( tHead ); // Print out the sum/difference struct integer
fprintf(outFile, "\n"); // Move to next line for next instruction set
}
fclose(inputFile);
fclose(outFile);
//system(PAUSE);
return 0;
}
// Function Definitions
/*
* 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
char *tok;
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.
}
void reverse (struct integer **p){
if((*p)->next==0) return;
struct integer *i=*p,*j;
while(i->next){
j=i;
i=i->next;
}//i is now the tail;
i->next=j;
j->next=0;
reverse(p);
*p=i;
}
/*
* 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);
while( p )
{
fprintf(outFile, "%d", p->digit);
p = p->next;
}
reverse(&head);
}
/*
* Function add
*
* @Paramater STRUCT* Integer
* @Parameter STRUCT* Integer
*
* Takes two linked lists representing
* big integers stored in reversed order,
* and returns a linked list containing
* the sum of the two integers.
*
* @Return STRUCT* Integer
*
* TODO Comment me
*/
struct integer* add( struct integer *p, struct integer *q )
{
int carry = 0;
struct integer *sHead, *sCurr;
struct integer *pHead, *qHead;
pHead = p;
qHead = q;
sHead = NULL;
while( p )
{
sCurr = ( struct integer* ) malloc (sizeof(struct integer));
sCurr->digit = p->digit + q->digit + carry;
sCurr->next = sHead;
sHead = sCurr;
carry = 0;
/*
* If the current digits sum to greater than 9,
* create a carry value and replace the current
* value with value mod 10.
*/
if( sCurr->digit > 9 )
{
carry = 1;
sCurr->digit = sCurr->digit % 10;
}
/*
* If the most significant digits of the numbers
* sum to 10 or greater, create an extra node
* at the end of the sum list and assign it the
* value of 1.
*/
if( carry == 1 && sCurr->next == NULL )
{
struct integer *sCarry = ( struct integer* ) malloc (sizeof(struct integer));
sCarry->digit = 1;
sCarry->next = NULL;
sCurr->next = sCarry;
}
p = p->next;
q = q->next;
}
reverse(&sHead);
return sHead;
}
/*
* Function subtract
*
* @Parameter STRUCT* Integer
* @Parameter STRUCT* Integer
*
* Takes two linked lists representing struct integers.
* Traverses through the lists, subtracting each
* digits from the subsequent nodes to form a new
* struct integer, and then returns the newly formed
* linked list.
*
* @Return STRUCT* Integer
*
* TODO Comment me
*/
struct integer* subtract( struct integer *p, struct integer *q )
{
int carry = 0;
struct integer *dHead, *dCurr;
struct integer *pHead, *qHead;
pHead = p;
qHead = q;
dHead = NULL;
while( p )
{
dCurr = (struct integer*) malloc (sizeof(struct integer));
dCurr->digit = p->digit - q->digit - carry;
dCurr->next = dHead;
dHead = dCurr;
if( dCurr->digit < 0 )
{
dCurr->digit += 10;
carry = 1;
}
if( dCurr->next == NULL && carry == 1 )
{
struct integer *dCarry = (struct integer*) malloc (sizeof(struct integer));
dCarry->digit = -1;
dCarry->next = NULL;
dCurr->next = dCarry;
}
p = p->next;
q = q->next;
}
return dHead;
}
/*
* Function compare
*
* @Parameter STRUCT* Integer
* @Parameter STRUCT* Integer
*
* Takes in two linked lists representing struct integers.
* Traverses the lists one at a time, comparing the
* digits.
*
* Case: p < q
* @Return -1
*
* Case: p == q
* @Return 0
*
* Case: p > q
* @Return 1
*
* TODO Comment me
*/
int compare( struct integer *p, struct integer *q )
{
struct integer *pHead, *qHead;
int comp = 0;
pHead = p;
qHead = q;
while( p )
{
if( p->digit > q->digit )
{
comp = 1;
}
if( p->digit < q->digit )
{
comp = -1;
}
p = p->next;
q = q->next;
}
return comp;
}
void reverse (struct integer **p){
if((*p)->next==0) return;
struct integer *i=*p,*j;
while(i->next){
j=i;
i=i->next;
}//i is now the tail;
// 1->2->3->4->NIL
i->next=j;
// 1 -> 2 -> 3 <-> 4
j->next=0;
// 1 -> 3 -> 3 <- 4
// |
// v
// NIL
reverse(p);// p looks like 1 -> 2 -> 3 now recurse
*p=i;
}
n = atoi( tok[i] );
n = tok[i] - '0';
for (i = 0; i < numDigits; i++) {
n = stringInt[i] - '0';
// etc
}