Hi all,
I wrote the following code to add two very long positive integers... Numbers are stored in doubly linked list....But it gives segmentation fault... Can someone please check it and help....
Code:
#include<stdio.h>
#include<malloc.h>
#define MAX 100000
struct node
{
int info;
struct node *right;
struct node *left;
};
typedef struct node node;
struct decision
{
char big;
char opr;
char sign_of_result;
};
typedef struct decision decision;
struct stackNode
{
char info;
struct stackNode *next;
};
typedef struct stackNode stackNode;
struct stack
{
stackNode *top;
};
typedef struct stack stack;
node* getNode(void);
void getNumber(node*);
void insertLeft(node*,int);
void insertRight(node*,int);
void freeNode(node*);
decision* compare(node*,node*);
void traverse(node*);
void add(node*,node*,decision*);
void push(stack*,char);
char pop(stack*);
int empty(stack*);
int main()
{
node *p,*q;
decision *d;
d=(decision*)malloc(sizeof(decision));
p=getNode();
p->right=p;
p->left=p;
q=getNode();
q->right=q->left=q;
printf("\nEnter first number: ");
getNumber(p);
/*...Using gdb I came to know that here is the error....
printf("\nEnter second number: ");
getNumber(q);
d=compare(p,q);
if(d->opr=='a')
add(p,q,d);
else
diff(p,q,d);
return 0;
}
node* getNode(void)
{
node *ptr;
ptr=(node*)malloc(sizeof(node));
ptr->left=NULL;
ptr->right=NULL;
return ptr;
}
void insertLeft(node *ptr,int info)
{
node *temp=getNode();
temp->info=info;
temp->left=ptr->left;
ptr->left->right=temp;
ptr->left=temp;
temp->right=ptr;
}
void insertRight(node *ptr,int info)
{
node *temp=getNode();
temp->info=info;
temp->right=ptr->right;
ptr->right->left=temp;
ptr->right=temp;
temp->left=ptr;
printf("\nNumber being entered: %d\n",info);
}
void getNumber(node *ptr)
{
int i=1,num=0,iter_count=0,count=0;
char ch,sign;
stack *s;
s->top=NULL;
ch=getchar();
if(ch=='-')
sign=ch;
else
{
push(s,ch);
sign='+';
}
while((ch=getchar())!='\n')
push(s,ch);
while(!empty(s))
{printf("\n\nfunc called\n\n");
while(!empty(s) && iter_count!=4)
{
ch=pop(s);
num=num+(ch-'0')*i;
iter_count++;
i*=10;
}
iter_count=0;
count++;
insertRight(ptr,num);
num=0;
i=1;
}
if(sign=='-')
ptr->info=count*-1;
else
ptr->info=count;
printf("\n\nfnction successfully executed\n\n");
}
void freeNode(node *ptr)
{
ptr->left->right=ptr->right;
ptr->right->left=ptr->left;
ptr->right=NULL;
ptr->left=NULL;
free(ptr);
}
decision* compare(node *p,node *q)
{
node *r=p;
node *s=q;
decision *d;
d=(decision*)malloc(sizeof(decision));
d->big='p';
d->opr='a';
d->sign_of_result='+';
if(p->info>0 && q->info>0)
return d;
else if(p->info<0 && q->info<0)
{
d->sign_of_result='-';
return d;
}
else
d->opr='s';
if(abs(p->info)==abs(q->info))
{
r=r->left;
s=s->left;
while(r->info==s->info)
{
r=r->left;
s=s->left;
}
if(r!=p && s!=q)
if(r->info<s->info)
{
d->big='q';
d->sign_of_result='+';
}
}
else if(abs(p->info)<abs(q->info))
{
d->big='q';
d->sign_of_result='-';
}
return d;
}
void traverse(node *ptr)
{
printf("Result is: ");
if(ptr->info<0)
printf("-");
node *p=ptr->right;
while(p!=ptr)
{
printf("%d",p->info);
p=p->right;
}
printf("\n\n");
}
void add(node *p,node *q,decision *d)
{
node *r=getNode();
node *s,*t;
int count=0,carry=0,total=0;
r->left=r->right=r;
s=p->right;
t=q->right;
while(s!=p && t!=q)
{
total=s->info+t->info+carry;
carry=total/MAX;
total=total%MAX;
insertRight(r,total);
count++;
s=s->right;
t=t->right;
}
while(s!=p)
{
total=s->info+carry;
carry=total/MAX;
total=total%MAX;
insertRight(r,total);
count++;
s=s->right;
t=t->right;
}
while(t!=q)
{
total=t->info+carry;
carry=total/MAX;
total=total%MAX;
insertRight(r,total);
count++;
s=s->right;
t=t->right;
}
if(carry!=0)
{
insertRight(r,carry);
count++;
}
if(d->sign_of_result=='-')
r->info=count*-1;
else
r->info=count;
traverse(r);
}
void push(stack *s,char info)
{
stackNode *ptr=(stackNode*)malloc(sizeof(stackNode));
ptr->next=NULL;
ptr->info=info;
if(s->top==NULL)
s->top=ptr;
else
{
ptr->next=s->top;
s->top=ptr;
}
}
char pop(stack *s)
{
char info=s->top->info;
stackNode *ptr=s->top;
s->top=s->top->next;
ptr->next=NULL;
free(ptr);
return info;
}
int empty(stack *s)
{
return(s->top==NULL?1:0);
}