Pointers 无法使用堆栈以相反顺序显示单链表的元素?

Pointers 无法使用堆栈以相反顺序显示单链表的元素?,pointers,singly-linked-list,Pointers,Singly Linked List,我已经编写了一个程序来添加两个非常大的整数,其中每个数字都存储在链表的一个节点中。当我运行该程序时,我的display方法不起作用。有人能告诉我display()方法有什么问题吗 #包括 #包括 类型定义结构节点{ 整数位数; 结构节点*链接; }节点; typedef节点*NODEPTR; typedef结构堆栈{ int-arr[100]; int top; }堆叠; int isEmpty(堆栈*s){ 如果(s->top==-1) 返回1; 返回0; } 无效推送(堆栈*s,整数项){

我已经编写了一个程序来添加两个非常大的整数,其中每个数字都存储在链表的一个节点中。当我运行该程序时,我的display方法不起作用。有人能告诉我display()方法有什么问题吗

#包括
#包括
类型定义结构节点{
整数位数;
结构节点*链接;
}节点;
typedef节点*NODEPTR;
typedef结构堆栈{
int-arr[100];
int top;
}堆叠;
int isEmpty(堆栈*s){
如果(s->top==-1)
返回1;
返回0;
}
无效推送(堆栈*s,整数项){
s->arr[++s->top]=物料;
}
int-pop(堆栈*s){
如果(i)是空的
返回-999;
其他的
返回s->arr[s->top--];
}
NODEPTR insertNode(NODEPTR头,int val){
NODEPTR curr,temp;
temp=(NODEPTR)malloc(sizeof(NODE));
温度->数字=val;
temp->link=NULL;
if(head->link==NULL)
返回温度;
否则{
curr=头部->链接;
while(curr->link!=NULL){
当前=当前->链接;
}
当前->链接=温度;
}
回流头;
}
NODEPTR getNumber(NODEPTR头){
int n,i,val;
printf(“在数字中输入数字\n”);
scanf(“%d”和“&n”);
对于(i=1;ilink;
curr2=h2->link;
整数进位=0,和=0;
while(curr1!=NULL&&curr2!=NULL){
总和=进位+当前1->数字+当前2->数字;
如果(总和>9){
sum=sum-10;
进位=1;
}
其他的
进位=0;
h3=插入节点(h3,总和);
总和=0;
}
while(curr1!=NULL){
总和=进位+电流1->数字;
如果(总和>9){
sum=sum-10;
进位=1;
}
其他的
进位=0;
h3=插入节点(h3,总和);
总和=0;
}
while(curr2!=NULL){
总和=进位+电流2->数字;
如果(总和>9){
sum=sum-10;
进位=1;
}
其他的
进位=0;
h3=插入节点(h3,总和);
总和=0;
}
返回h3;
}
无效显示(节点接收头){
堆栈s;//堆栈存储数字,然后显示
s、 top=-1;
NODEPTR curr=头部->链接;
while(curr!=NULL){
推送(&s,curr->digit);//将数字推送到堆栈中
当前=当前->链接;
}
而(!isEmpty(&s))
printf(“%d”,pop(&s));//从堆栈中弹出数字,以便数字以正确的顺序显示
}
int main(){
节点受体h1、h2、h3;
h1=(NODEPTR)malloc(sizeof(NODE));//存储第一个数字的链表的伪/头节点
h1->link=NULL;
h2=(NODEPTR)malloc(sizeof(NODE));//存储第二个数字的链表的伪/头节点
h2->link=NULL;
h3=(NODEPTR)malloc(sizeof(NODE));//存储两个数字之和的链表的伪/头节点
h3->link=NULL;
h1=getNumber(h1);//从用户处获取第一个数字
h2=getNumber(h2);//从用户处获取第二个数字
h3=添加(h1,h2,h3);//添加两个数字并将其存储在h3链表中
printf(“第一个数字是:”);
display(h1);//显示第一个数字
printf(“第二个数字是:”);
display(h2);//显示第二个数字
printf(“数字之和为:”);
display(h3);//显示总和
返回0;
}
运行程序时,我得到的display()方法的输出为:

第一个数字是:第二个数字是:数字之和是:

#include<stdio.h>
#include<stdlib.h>
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->top==-1)
  return 1;
 return 0;
}
void push(STACK* s,int item){
 s->arr[++s->top]=item;
}
int pop(STACK* s){
 if(isEmpty(s))
  return -999;
 else
  return s->arr[s->top--];
}
NODEPTR insertNode(NODEPTR head,int val){
 NODEPTR curr,temp;
 temp=(NODEPTR)malloc(sizeof(NODE));
 temp->digit=val;
 temp->link=NULL;
 if(head->link==NULL)
  return temp;
 else{
  curr=head->link;
  while(curr->link!=NULL){
   curr=curr->link;
  }
  curr->link=temp;
 }
 return head;
}
NODEPTR getNumber(NODEPTR head){
 int n,i,val;
 printf("enter the number of digits in the number\n");
 scanf("%d",&n);
 for(i=1;i<=n;i++){
  printf("enter the digit at %dth position(in increasing power of   10)\n",i);
  scanf("%d",&val);
  head=insertNode(head,val);
 }
 return head;
}
NODEPTR add(NODEPTR h1,NODEPTR h2,NODEPTR h3){
 NODEPTR curr1,curr2;
 curr1=h1->link;
 curr2=h2->link;
 int carry=0,sum=0;
 while(curr1!=NULL && curr2!=NULL){
  sum=carry+curr1->digit+curr2->digit;
  if(sum>9){
   sum=sum-10;
   carry=1;
  }
  else
   carry=0;
  h3=insertNode(h3,sum);
  sum=0;
 }
 while(curr1!=NULL){
  sum=carry+curr1->digit;
  if(sum>9){
  sum=sum-10;
  carry=1;
 }
 else
  carry=0;
 h3=insertNode(h3,sum);
 sum=0;
}
while(curr2!=NULL){
 sum=carry+curr2->digit;
 if(sum>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->link;
 while(curr!=NULL){
  push(&s,curr->digit);//pushing digits to the stack 
  curr=curr->link;
 }
 while(!isEmpty(&s))
  printf("%d",pop(&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->link=NULL;
 h2=(NODEPTR)malloc(sizeof(NODE));//dummy/header node for the linked list that stores the second number 
 h2->link=NULL;
 h3=(NODEPTR)malloc(sizeof(NODE));//dummy/header node for linked list that stores the sum of both the numbers
 h3->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;
}