title: Unable to display elements of singly linked list in reverse order using stack?
body: <p>I have written the program to add two integer numbers of very large sizes wherein each digit is stored in a node of linked list.When i run the program my display method is not working.Can anyone tell me what is wrong with my display() method?</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  typedef struct node{
   int digit;
   struct node* link;
  }NODE;
  typedef NODE* NODEPTR;
  typedef struct stack{
   int arr[100];
   int top;
  }STACK;
  int isEmpty(STACK* s){
   if(s-&gt;top==-1)
    return 1;
   return 0;
  }
  void push(STACK* s,int item){
   s-&gt;arr[++s-&gt;top]=item;
  }
  int pop(STACK* s){
   if(isEmpty(s))
    return -999;
   else
    return s-&gt;arr[s-&gt;top--];
  }
  NODEPTR insertNode(NODEPTR head,int val){
   NODEPTR curr,temp;
   temp=(NODEPTR)malloc(sizeof(NODE));
   temp-&gt;digit=val;
   temp-&gt;link=NULL;
   if(head-&gt;link==NULL)
    return temp;
   else{
    curr=head-&gt;link;
    while(curr-&gt;link!=NULL){
     curr=curr-&gt;link;
    }
    curr-&gt;link=temp;
   }
   return head;
  }
  NODEPTR getNumber(NODEPTR head){
   int n,i,val;
   printf("enter the number of digits in the number\n");
   scanf("%d",&amp;n);
   for(i=1;i&lt;=n;i++){
    printf("enter the digit at %dth position(in increasing power of   10)\n",i);
    scanf("%d",&amp;val);
    head=insertNode(head,val);
   }
   return head;
  }
  NODEPTR add(NODEPTR h1,NODEPTR h2,NODEPTR h3){
   NODEPTR curr1,curr2;
   curr1=h1-&gt;link;
   curr2=h2-&gt;link;
   int carry=0,sum=0;
   while(curr1!=NULL &amp;&amp; curr2!=NULL){
    sum=carry+curr1-&gt;digit+curr2-&gt;digit;
    if(sum&gt;9){
     sum=sum-10;
     carry=1;
    }
    else
     carry=0;
    h3=insertNode(h3,sum);
    sum=0;
   }
   while(curr1!=NULL){
    sum=carry+curr1-&gt;digit;
    if(sum&gt;9){
    sum=sum-10;
    carry=1;
   }
   else
    carry=0;
   h3=insertNode(h3,sum);
   sum=0;
  }
  while(curr2!=NULL){
   sum=carry+curr2-&gt;digit;
   if(sum&gt;9){
    sum=sum-10;
    carry=1;
   }
  else
   carry=0;
  h3=insertNode(h3,sum);
  sum=0;
  }
  return h3;
  }
  void display(NODEPTR head){
   STACK s;//stack to store digits of the number and then display
   s.top=-1;
   NODEPTR curr=head-&gt;link;
   while(curr!=NULL){
    push(&amp;s,curr-&gt;digit);//pushing digits to the stack 
    curr=curr-&gt;link;
   }
   while(!isEmpty(&amp;s))
    printf("%d",pop(&amp;s));//poping the digits from stack so that the number appears in correct order 
  }
  int main(){
   NODEPTR h1,h2,h3;
   h1=(NODEPTR)malloc(sizeof(NODE));//dummy/header node for linked list that stores the first number 
   h1-&gt;link=NULL;
   h2=(NODEPTR)malloc(sizeof(NODE));//dummy/header node for the linked list that stores the second number 
   h2-&gt;link=NULL;
   h3=(NODEPTR)malloc(sizeof(NODE));//dummy/header node for linked list that stores the sum of both the numbers
   h3-&gt;link=NULL;
   h1=getNumber(h1);//to get first number from user
   h2=getNumber(h2);//to get second number from user
   h3=add(h1,h2,h3);//to add both the numbers and store it in the h3 linked list
   printf("First number is:");
   display(h1);//to display the first number
   printf("second number is:");
   display(h2);//to display the second number
   printf("sum of the numbers is:");
   display(h3);//to display the sum
   return 0;
  }
  </code></pre>
  
  <p>On running the program the output of display() method I get is:</p>
  
  <p>first number is:second number is:sum of numbers is:</p>