I'm almost done with this assignment, and it's killing me. This is my THIRD post about three different sections of this, and I'm honestly embarrassed that I'm struggling this much with the assignment.
The assignment itself is to make a program that performs addition and subtraction of big integers using linked lists (and I'm slowly starting to hate linked lists, outside of Lisp). Everything seems to be working now, save for the actual addition and subtraction. I'm not sure if it is the arithmetic functions, because they were sort of working before (but never 100%), but it doesn't hurt to check with the S/O community (normally I wouldn't ask for this much help on an assignment because I prefer to figure things out on my own, but this has been an awful and hectic week, and the deadline is fast approaching).
The arithmetic functions I've written are as follows, can anyone help me pick out what is wrong?
/*
* 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;
reverse( &sCurr );
sCurr->next = sCarry;
reverse( &sCurr );
}
p = p->next;
if( q->next ) q = q->next;
else q->digit = 0;
}
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 borrow = 0;
struct integer *dHead, *dCurr;
struct integer *pHead, *qHead;
pHead = p;
qHead = q;
dHead = NULL;
while( p )
{
dCurr = (struct integer*) malloc (sizeof(struct integer));
if( q )
{
dCurr->digit = p->digit - q->digit - borrow;
}
else
{
dCurr->digit = p->digit - borrow;
}
dCurr->next = dHead;
if( dCurr->digit < 0 )
{
dCurr->digit += 10;
borrow = 1;
}
dHead = dCurr;
p = p->next;
if( q->next) q = q->next;
}
return dHead;
}
The sample output should look like this:
8888888888 + 2222222222 = 11111111110
10000000000 – 9999999999 = 1
10000000000 – 9999999999 = 1
but instead, it looks like this:
8888888888 + 2222222222 = 1111111110
10000000000 - 9999999999 = 10000000001
10000000000 - 9999999999 = 10000000001
EDIT The entire program, in its current form as of 3:30PM EST, is available here for reference, or in case these functions are not the issue.
else q->digit = 0;
You are changing the argument inside the function.
Try changing your functions to accept const arguments and recompile.
struct integer* add( const struct integer *p, const struct integer *q )
struct integer* subtract( const struct integer *p, const struct integer *q )
The part that reads
if( dCurr->next == NULL && carry == 1 )
{
struct integer *dCarry = (struct integer*) malloc (sizeof(struct integer));
dCarry->digit = -1;
dCarry->next = NULL;
dCurr->next = dCarry;
}
looks a bit out of place. From the code above, dCurr->next is set to be the digits we've already calculated in earlier loops, so it is only NULL on the first digit. I think you meant to check p->next instead.
I am assuming that the condition len(p) >= len(q) holds for this function. If not, you will have to do something about handling where it doesn't hold (running out of p nodes before you run out of q nodes). I also assume the digits are in the list from least significant digit to most significant one. If not, you may need to reverse p and q before you process them.
One other thing I can't figure out is how you handle negative numbers. Or even if you are supposed to handle them. It is not easy to add to a structure like this, because a naive approach of adding something to the end would not work when subtracting: when q is negative, you will go to all the trouble of subtracting q from p, and then discover that you should have added instead.
In function compare() you "walk" p and afterwards try to walk it again.
int compare( struct integer *p, struct integer *q )
{
/* ... */
while( p )
{
pCount++;
p = p->next;
}
p is now NULL
/* ... */
while( p )
{
/* ... */
}
The while loop never runs.
Related
I have this struct:
typedef struct node {
struct node *m_Next;
int id;
} NODE;
And I need to split linked list in half. If it can't be split into two same halves, I want to remove the last one from the bigger list.
Example: The whole list: {1,2,3,4,5}; What I need: A:{1,2} B:{3,4} (The last one is discarded)
I have this function to separate the list:
void split(NODE *src, NODE **p1, NODE **p2) {
int len = get_length(src);
if (len < 2) {
*p1 = NULL;
*p2 = NULL;
return;
}
struct node *current = src;
int c = (len - 1) / 2;
for (int i = 0; i < c; i++) {
current = current->m_Next;
}
*p1 = src;
*p2 = current->m_Next;
current->m_Next = NULL;
}
It works fine with even lists, but when I try to separate something with 7 structs, I have two problems:
a) The last one doesn't point to NULL
b) It shuffles the data somehow (I expect: A:{1,2,3} B:{4,5,6} | I get: A:{1,2,3} B:{5,4,7} for example)
Could anyone please help me splitting the list correctly and adding the even/odd condition?
I already have the function to delete the last struct:
deleteNode(struct TSoldier *firstNode)
I just don't use it currently, because the split function is bugged.
Thanks :)
First of all, it is worth noting that the following code probably causes a memory leak:
if (len < 2) {
*p1 = NULL;
*p2 = NULL;
return;
}
If the number of nodes is equal to 1, then, unless you keep some other reference to this node, the memory will be leaked. You probably have such a reference outside the function, but you are probably discarding this reference and only keeping the values written to p1 and p2, which means the memory is leaked.
Therefore, assuming that you allocated the node with malloc, you will probably want to add the line
free( src );
in order to prevent the memory leak, or use your function deleteNode.
As already pointed out in the other answer, the line
int c = (len - 1) / 2;
is wrong. It should be:
int c = len / 2 - 1;
At the end of your function split, if the number of nodes is odd, you must add code to discard the final node, for example like this:
if ( len % 2 == 1 )
{
current = *p2;
for (int i = 0; i < c; i++) {
current = current->m_Next;
}
free( current->m_Next );
current->m_Next = NULL;
}
To determine the last node of the first half, instead of int c = (len - 1) / 2; you should use this formula that works for even and odd lengths:
int c = len / 2 - 1;
Similarly, to drop the last node if the length is odd and greater than 1:
if (len & 1) {
NODE *node = src;
for (int i = 2; i < len; i++) {
node = node->m_Next;
}
deleteNode(node->m_Next);
node->m_Next = NULL;
}
Here is an alternative approach using the fast and slow scan trick:
void split(NODE *src, NODE **p1, NODE **p2) {
NODE *last = src;
*p1 = *p2 = NULL;
if (src && src->m_Next) {
NODE *slow = src;
NODE *fast = src;
while (fast->m_Next && fast->m_Next->m_Next) {
slow = slow->m_Next;
fast = fast->m_Next->m_Next;
}
*p1 = src;
*p2 = slow->m_Next;
slow->m_Next = NULL;
last = fast->m_Next; // last will be non NULL if length is odd
fast->m_Next = NULL;
}
if (last) {
deleteNode(last); // drop the last node if required
}
}
I am using a priority queue with a double as the priority. I am guessing this is the cause of the issues. I used these numbers first with no issues.
34.365681
34.481879
34.539832
36.715120
I then used these numbers and had a segmentation fault.
45.411042
40.481879
37.702110
38.951187
struct PRIORITYQUEUE
{
int x_pq;
int y_pq;
double heuristic_pq;
int priority;
int info;
struct PRIORITYQUEUE *next;
}*start, *q, *temp, *new;
typedef struct PRIORITYQUEUE *N;
void insert(int x, int y, double heuristic)
{
int item;
double itprio;
//new = ( N* ) malloc( sizeof( N ) );
new = malloc( sizeof( N ) );
itprio = heuristic;
new->x_pq = x;
new->y_pq = y;
new->heuristic_pq = itprio;
if ( start == NULL || itprio < start->heuristic_pq )
{
new->next = start;
start = new;
}
else
{
q = start;
while ( q->next != NULL && q->next->heuristic_pq <= itprio )
q = q->next;
new->next = q->next;
q->next = new;
}
}
void del()
{
if ( start == NULL )
{
printf( "\nQUEUE UNDERFLOW\n" );
}
else
{
new = start;
printf( "\nDELETED ITEM IS %d\n", new->info );
start = start->next;
free( start );
}
}
void display()
{
temp = start;
if ( start == NULL )
printf( "QUEUE IS EMPTY\n" );
else
{
printf( "QUEUE IS:\n" );
while ( temp != NULL )
{
printf( "\t x is %d y is %d[heuristic=%lf] \n", temp->x_pq, temp->y_pq, temp->heuristic_pq );
temp = temp->next;
}
}
}
Your problem lies with this code:
typedef struct PRIORITYQUEUE *N;
:
new = malloc( sizeof( N ) );
The type N is a pointer to that structure of yours, not the structure itself. That means that sizeof(N) is likely to be much smaller than the structure, meaning that you're not allocating enough memory.
You could see this by inserting this immediately after the allocation:
printf ("%zd %zd\n", sizeof (N), sizeof (struct PRIORITYQUEUE));
and you'll probably see a series of lines of the form 4 32 or 8 32, showing that, while you've allocated four or eight bytes, you need 32.
That's what's causing your crashes. Now, as to how to fix it, it's simply making sure you allocate enough space for the structure and this can be done with either of:
new = malloc (sizeof (struct PRIORITYQUEUE));
new = malloc (sizeof (*N));
But the one I prefer is:
new = malloc (sizeof (*new));
The reason I prefer it is that it ties the allocation quantity to the variable you using. While the earlier two will handle any changes to the structure size, this one will even survive declaring new as a totally different structure without having to change information in multiple places.
By that I mean, if you change the type of new thus:
struct FASTER_PRIO_Q *new;
then you would be required to change the allocation statements as well for the first two cases. Not so for the third.
I'm working on a homework assignment for CS1, and I almost have it finished but errors keep popping up in relation to a few functions I've tried to implement. The assignment is the classic addition and subtraction of big integers using linked lists. My issue isn't with any of mathematical functionality of the program, but rather getting the linked lists to print properly when finished. I'm pretty sure most of the problems reside within stripLeadingZeros(); the functions are as follows.
/*
* 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;
}
The output I am currently getting for the whole program is:
888888888 + 222222222 = 11111111
000000000 - 999999999 = 000000001
000000000 - 999999999 = 000000001
whereas the expected output is
8888888888 + 2222222222 = 11111111110
10000000000 – 9999999999 = 1
10000000000 – 9999999999 = 1
Any help anyone could give would just be awesome; I've been working on this for so long that if I had any hair I'd have pulled it out by now.
EDIT My read_integer function is as follows:
/*
* 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.
}
Simulate stripLeadingZeros() on "0004".
It does not work. Also you ignored an edge case: what if it is only "0". You must not strip the only 0 in that case.
Correct code:
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? Also it should not strip last 0
if( p->digit == 0 && p->next != NULL)
{
struct integer *pNext;
pNext = p->next;
// Deallocate the node
free( p );
// Try to strip zeros on pointer to the next node and return that pointer
return stripLeadingZeros(pNext);
}
return p;
}
Consider the control flow of this function:
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;
}
What happens when p starts with a zero? It enters the if statement, removes the one leading zero, and returns. It does not recurse because you've already returned within the if statement. This means that stripLeadingZeros will remove at most one zero.
Now what happens when p starts with a one? It skips the if statement, but it does recurse. This is also wrong, because once have seen a one, you want to stop removing zeroes, as they are no longer leading.
So what this function is actually doing is removing the first zero it encounters, leading or not, and then stopping. That's not what you want it to do.
You want to recurse after you have removed a zero, and only after you have removed a zero, so move the recursive call into the if statement. In other words, replace return pNext; with return stripLeadingZeros(pNext);, and remove the recursion from outside the loop.
You can improve your reverse function by reversing your original list into another list:
void reverse(struct integer** p)
{
struct integer* old = *p;
struct integer* new = NULL;
while(old != NULL)
{
struct integer* oldNext = old->next;
old->next = new;
new = old;
old = oldNext;
}
*p = new;
}
stripLeadingZeros( nodeptr s )
{
if(s!=NULL)
stripLeadingZeros(s->next);
if((s!=NULL)&&s->data==0&&on)
flg=1;
if((s!=NULL)&&(s->data!=0)&&flg)
on=0,flg=0,s->next=NULL;
if(flg)
s->next=NULL;
}
here's my code to strip leading zeros initial values of on and flg are 1 and 0 respectively .
http://programmingconsole.blogspot.in/2013/10/all-basic-calculator-functions-on-large.html
in your current version of stripLeadingZeros you can replace the while loop with an if statement that the result will be the same. Maybe that is the problem.
while (1) {
/* ... */
return 0; /* this "infinite loop" only runs once */
}
compare with
if (1) {
/* ... */
return 0;
}
When compiling a program containing this particular function,
/*
* 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.
}
I get the warning:
bigintII.c: In function ‘read_integer’:
bigintII.c:167: warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast
//usr/include/stdlib.h:148: note: expected ‘const char *’ but argument is of type ‘char’
I've tried a couple of different ways (that were mostly shots in the dark) to get rid of the warning, but to no avail. Can anybody point me in the right direction?
---///---
Original, dumb question below here, left because I don't want to be that guy.---
I'm trying to debug my assignment for CS1 so I can finish it and move onto something interesting (like working on my CLisp studies), but I've run into an error I can't get past. Now, I know the assignment isn't complete yet (i.e., it isn't going to do what I want it to even if I get it to run) but I don't want help with that (besides, what fun would that be?). If someone could just help me figure out why the following code, when compiled and executed, produces the useless (to me) Segmentation fault (core dumped), well, that'd be awesome.
/*
* 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;
}
I'm sorry that there's a lot to wade through, I'm just trying to get in the habit of over-documenting my code because I like to go back and read through stuff I've written to review concepts. The actual program itself is fairly straightforward, though (if not broken, haha).
compiling it with gcc gives this:
test.c: In function ‘read_integer’:
test.c:165: warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast
/usr/include/stdlib.h:148: note: expected ‘const char *’ but argument is of type ‘char’
this is 164/165
tok = strtok( stringInt, NULL ); // Tokenize the string by each char
n = atoi( tok[i] ); // Convert char to an integer
should be
n = stringInt - '0'; // Convert char to an integer
the tok variable is now pointless. it doesn't crash anymore with me, and it does find the correct answer(1 + 1 = 2),
furthermore you forgot to close outputFile at the end of your program.
and you apparently open the outfile in append mode(wich isn't bad, it just caught me by surprise)
then there's another inconsistency, your add function reverses the order in which your numbers are stored wich means that the input numbers are printed in reverse order whilst the anwer is printed in the correct order.
I fixed those now:
/*
* 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;
}
i wrote reverse for you:
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;
}
Your problem is caused by passing the wrong thing to atoi. atoi expects a string, which as you know in C is a char pointer. You're passing a single char, which is not a char pointer. atoi attempts to treat the value of the character as a memory address, which is almost certainly an address you're not allowed to access, so your program goes bang.
To get the numerical value of a char containing an ASCII digit, subtract '0' from it, ie change
n = atoi( tok[i] );
to
n = tok[i] - '0';
But you don't need strtok:
for (i = 0; i < numDigits; i++) {
n = stringInt[i] - '0';
// etc
}
As a part of an assignment, I need to write two functions:
a function that sums up two natural numbers represented as a linked list
a functions that prints a number represented in the same way.
for some reason, both function work perfectly fine separately, but when I try to use the print function on the result of the sum function, it changes the value of the sum right in the beginning of the print function , and prints the wrong value. when I use printf to print the same value in the main, there is no problem. my code is detailed below. any ideas?
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))
}
here is the rest of the code:
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;
}
}
The bug is in the function addNumbers.
When you add a node to store the sum you pass a pointer to the variable currN1N2Sum which is a local variable (stored on the stack). When the addNumbers function terminates, the storage of the local variable is set free. The value found at that location will remain unchanged and thus apparently valid as long as the storage is not reused.
This is why you had the impression the addNumbers function was correct. When calling the printNumber function the storage is overwritten and you find a different value in there.
This explain your bug.
There is another problem with addNumbers. When you will try to add two digit numbers, the content of the currN1N2Sum will be overwritten by a new value.
What you should do is allocate a buffer (malloc) and store the value contained into currN1N2Sum into it. Pass the pointer to the buffer into the new node.
BTW: you may change (*lst).head in lst->head. It will make your code more readable.
We need to see some more code: how you define the data structure for holding nodes, how you add nodes etc.
The following line is suspect:
number=number+currData*((int)pow(10,i));
Say, you have 123 stored as 1, 2, and 3 nodes:
number = 0;
number = 0 + 1 * 1 = 1;
number = 1 + 2 * 10 = 21;
number = 21 + 3 * 100 = 321;
But if you store is as 3, 2, and 1 nodes you'd get:
number = 0;
number = 0 + 3 * 1 = 3;
number = 3 + 2 * 10 = 23;
number = 23 + 1 * 100 = 123;
Is this your problem?
I'm not if this is an issue or not without seeing the implementation of createNewListNode(), but here's something to think about:
Where are the dataPtrs in the sum list pointing after you return from the addNumbers() call?
You've got a problem with createEmptyList. you declare there a list called res and return the structure but the minute this function returns that structure is not valid any more.
try using malloc (for the struct) and then return the pointer to the caller. You use this function in the beginning with *sum.
This is a similar bug to what chmike found so you better fix both.
I think you might be messing things up pointer-wise... The way you're allocating the list 'sum' in addNumbers seems very, very odd. (And I would not be surprised if it's breaking things...)
Try making these changes:
In main:
List *sum;
<...>
addNumbers(lst1,lst2,sum); //Note the absence of the reference operator &
printNumbers(*sum);
(Alternatively, change printNumbers to accept a (List *) instead of (List)).
Hope this helped XD
EDIT:
Try allocating 'sum' before making calls to addNumbers().
lst1 = arrToList(pa, 1);
lst2 = arrToList(pb, 1);
sum = createEmptyList();
I still think the way that your data structures are a little weird :S