I'm still very new to programming and I've never done linked lists before. Here's the assignment: http://www.geocities.com/blueninja83/hugeintegers.html /* Note there picture of the reversed linked list is missing - it's suppose to have the above number (say it was 594) in boxes (representing the link list ) it the order 495 (backwards) so that 5 is next to the null character meaning it is your head at the end. */
I can't figure out how to just add single digits in a linked list. Like there are two linked lists and I can't figure out how to add one from one list to another from another list. I use a char data type because I heard from someone that that was the only way to read a huge number one digit at a time. My program compiles but gives a segmentation fault (which I think is caused by a pointer not pointing to the correct item or somthing?). Here's the codeAny help would be greatly appreciated.Code:#include <stdio.h> #include <stdlib.h> struct integerNode{ char value; struct integerNode *next; }; typedef struct integerNode ELEMENT; /* Just easily refrences the structure */ typedef struct integerNode *integer; /* Used to build up the linked list */ integer ReadInteger(); void PrintInteger(integer n); int compare(integer n1, integer n2); integer AddTwoIntegers(integer n1, integer n2); integer SubtractIntegers(integer n1, integer n2); integer RecycleInteger(integer n); main(void) { ELEMENT *n1; /* The first large integer */ ELEMENT *n2; /* The second large integer */ ELEMENT *n; /* The answer after the addition or subtraction */ int i=0; while(i!=3) { printf("1)Add\n2)Subtract\n3)Quit\n"); printf("Please enter the number of the option you wish to perform\n"); scanf("%d", &i); getchar(); switch(i) { case 1: printf("Please enter your first of two large integers\n"); n1=ReadInteger(); printf("Please enter your second of two large integers\n"); n2=ReadInteger(); AddTwoIntegers(n1, n2); PrintInteger(n); case 2: SubtractIntegers(n1, n2); case 3: exit(0); } } } integer ReadInteger( ) { char ch; ELEMENT *head = NULL; /* The head of the linked list always has a null char hence the null*/ ELEMENT *new; /* A pointer to the linked list */ while((ch=getchar())!='\n') { if( (ch>='0' ) && (ch<='9') ){ new=malloc(sizeof(ELEMENT)); new->value=ch; new->next=head; head=new; } } return head; printf("\n"); } integer AddTwoIntegers(integer n1, integer n2) { int val1, val2, total; ELEMENT *new; /*A pointer structure for the an answer slot/node */ ELEMENT *head; /* A node for the head of the answer link list */ while((n1!=NULL) && (n2!=NULL)) { new=malloc(sizeof(ELEMENT)); val1=n1->value-48; val2=n2->value-48; total=val1+val2; new->value=total; new->next=head; head=new; n1=n1->next; n2=n2->next; } /*printf("%d", total); */ } integer SubtractIntegers(integer n1, integer n2) { } integer RecycleInteger(integer n) { } void PrintInteger(integer n) { if(n->next!=NULL) { PrintInteger(n->next); printf("%c", n->value); } else printf("%c", n->value); }