SURE HERE IS THE CODE it took some adjustments in the sum function simple stufff but it drained my brain.lol!
Code:
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int num;
struct node*next;
}node,*nodeptr;//STRUCTURE DECLARED
//FUNCTIONS DECLARED//
void sl();
nodeptr sum(nodeptr,nodeptr);
nodeptr makenode(int);
void print(nodeptr);
nodeptr makelist(char[]);
nodeptr reverse1(nodeptr);
nodeptr sum(nodeptr,nodeptr);
//============================================================//
int main(){
char list1[50],list2[50];//declaration of char array
nodeptr List1=NULL,List2=NULL,sumlist=NULL;
printf("enter integers for list1 : ");
scanf("%s",&list1);//inputs into the array
printf("Enter integers for List2 : ");
scanf("%s",&list2);
List1=makelist(list1);//creates a link list with the values from the array
List2=makelist(list2);
sl();
sl();
printf("List 1 : ");
print(List1);
sl();
printf("List 2 : ");
print(List2);
sl();
printf("__________________");
sl();
printf("SUM : ");
print(reverse1(sum(List1,List2)));
sl();
sl();
system("pause");
return 0;
}
//==================================================//
nodeptr makenode(int n){
nodeptr np=(nodeptr)malloc(sizeof(node));
np->num=n;
np->next=NULL;
return np;
}//creates a node for the list
//================================================//
void print(nodeptr np){
while (np!=NULL){
printf("%d",np->num);
np=np->next;
}
}
//==============================================//
nodeptr makelist(char list1[]){
nodeptr makenode(int),np,top,last;
int i=0;
char c = list1[i];//char at the 1st position of the array
int n;
top=NULL;
while(c!='\0'){//while char is not the end of the aray
n = c - '0';//makes the char into an int
np=makenode(n);//makes the node for the int
if(top==NULL)top=np;
else last->next=np;
last=np;
c = list1[++i];//increments the list
}
return top;
}
//================================================//
void sl(){
printf("\n");
} //just skips a line.
//================================================//
nodeptr reverse1(nodeptr top){//reverses the list
nodeptr newtop=NULL,temp=NULL;
while(top!=NULL){
temp=top;
top=top->next;
temp->next=newtop;
newtop=temp;
}
return newtop;
}
//===============================================//
nodeptr sum(nodeptr l1,nodeptr l2){
nodeptr l3=NULL,last,r1=NULL,r2=NULL,np,mp;
int n,num1,num2,carr=0;
r1=reverse1(l1);//reverses the list to add the numbers
r2=reverse1(l2);
while(r1!=NULL || r2!=NULL){
if (r1 != NULL){ num1 = r1 -> num; r1 = r1 -> next;}
else num1 = 0;
if (r2!=NULL){num2 = r2 -> num; r2 = r2 -> next;}
else num2=0;
n=num1 + num2 + carr;
carr = 0;
if(n<10) np=makenode(n);
else {
np=makenode(n-10);
carr = 1;
}
if(l3==NULL)l3=np;
else last->next=np;
last=np;
}
if(carr==1)last->next=makenode(carr);
return l3;
}