C program reversed linked list -


im trying write program in c adds big numbers linked list. used reverse add numbers, cant reverse again. should used several times(iow, looped ask numbers until exit)

#include <stdlib.h> #include <stdio.h> #include <string.h> #include "function.c"  int main() {     char n1[500];     int lensum, len;     printf("welcome! \nthis program performs addition of big whole numbers can contain upto maximum of 500 digits.");     printf("\nenter first number: ");      scanf("%s", n1);     len = strlen(n1);     node *root = create(n1[0]);     node *head = root;     root, head = createlist(n1,root,head,len);     root=head;      printf("enter second number: ");     scanf("%s", n1);     len = strlen(n1);     node *root2 = create(n1[0]);     node *head2 = root2;     root2, head2 = createlist(n1,root2,head2,len);     root2=head2;      root=head;     printf("\nyour first number is:\t ");         while(root!=null){         printf("%d\t",root->digit);         root=root->next;     }      root2=head2;     printf("\nyour second number is: ");         while(root2!=null){         printf("%d\t",root2->digit);         root2=root2->next;     }     printf("\ncalculating sum:\n");      root=head;     root2=head2;     node *sum = create('0');     node *headsum = sum;     sum,headsum = addintegers(root, root2, sum, headsum);      printf("\nthe sum : ");     sum=headsum;         while(sum->next!=null){         printf("%d",sum->digit);         sum=sum->next;     }      printf("\n");      sum = headsum;     printf("\nthe number ");     saveresult(sum);     printf("has been saved in file 'results.txt'\n");      root, head = reverselinkedlist(sum, headsum);     printdigit(root);     root=head;     printf("\n\n\t ");         while(root!=null){         printf("%d\t",root->digit);         root=root->next;     }      free(root);     free(root2);     //free(sumdigit);//     return 0; } 

function.h:

#ifndef function.h #define function.h  typedef struct node {   int digit;   struct node *next; }node; node* create(char digit); node* createlist(char number[500], node* root,node* head, int length); node* addintegers(node* root, node* root2, node* sum, node* headsum); #endif 

function.c:

#include <stdlib.h> #include <stdio.h> #include <string.h> #include "function.h"  node* createlist(char number[500], node* root,node* head, int length){     int i;     i=0;     for(i=1;i<=length-1;i++) {         root = (node *)malloc(sizeof(node));         root->digit = number[i]-'0';         root->next  = head;         head = root;     }     return root, head; } void printdigit(node* root){         while(root!=null){         printf("%d",root->digit);         root=root->next;     } } node* addintegers(node* root, node* root2, node* sum, node* headsum){     int carry = 0;     int sumdigit = 0;     while((root!=null || root2!=null)) {         if (root==null || root2==null){             if (root == null){                 sumdigit=root2->digit +carry;                 root2=root2->next;                 goto add;}             if (root2 == null){                 sumdigit = root->digit + carry;                 root=root->next;                 goto add;             }         }         else{         sumdigit = root->digit + root2-> digit + carry;         }         root2 = root2->next;         root = root->next;         add:         sum = (node *)malloc(sizeof(node));         sum->digit = sumdigit%10;         sum->next  = headsum;         headsum = sum;          if (sumdigit > 9){             carry = 1;}         else{             carry = 0;         }     }     if (carry == 1){         sum = (node *)malloc(sizeof(node));         sum->digit = 1;         sum->next  = headsum;         headsum = sum;     }     return sum, headsum; }  node* create(char digit) {   node *root = (node *) malloc( sizeof(node));   if (root == null){     printf("error\n");     exit(1);   }   root->digit = digit-'0';   root->next = null; //default null   return root; }  void saveresult(struct node *sum) {     file * fp = fopen ("result.txt", "w+");     while(sum->next != null)     {         printf("%d", sum->digit);         fprintf(fp, "%d", sum->digit);         sum = sum->next;     }     fclose(fp);     printf(" "); }  node* reverselinkedlist(node *root, node* head){     node* reversed = create(root->digit);     node* reversedtail = reversed;     while(root!=null){         //printf("%d", root->digit);         root = root->next;         reversed->digit = root;         reversed->next =head->next;         reversed = reversed->next;         head = root;     }     reversed = reversedtail;     return reversed, reversedtail; } 

as question not specific issue you're facing, below points taken care,

in function reverse(),

return reversed, reversedtail; return reversedtail.

you trying call function main()

root, head = reverselinkedlist(sum, headsum); again head return values. incorrect. should using assignment 1 member @ time.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -