回复 4楼 jack10141
可以自己稍微限制点啊!
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
char data;
struct node *pnext;
}Node,*PNode;
void creat_list(PNode *pphead)
{
PNode ptemp,ptail;
ptemp=ptail=NULL;
int input=0,i=0;
while(1)
{
scanf("%c",&input);
if (input=='\n')
{
break;
}
ptemp=(PNode)malloc(sizeof(Node));
if ( NULL == ptemp)
{
printf("create_list1开辟结点出错\n");
}
ptemp->data=input;
ptemp->pnext=NULL;
if(*pphead==NULL)
{
ptail=*pphead=ptemp;
}
else
{
ptail->pnext=ptemp;
ptail=ptemp;
}
}
}
void Reverse(PNode *pphead)
{
PNode p1,p2;
p1=*pphead;
*pphead=NULL;
while(p1!=NULL)
{
p2=p1->pnext;
p1->pnext=*pphead;
*pphead=p1;
p1=p2;
}
}
void DumpList(PNode phead)
{
while(phead!=NULL)
{
printf("%c",phead->data);
phead=phead->pnext;
}
printf("\n");
}
PNode Add(PNode phead1,PNode phead2)
{
int carry_flag=0;
PNode phead3,*pr=NULL;
char pt;
for(;phead1!=NULL&&phead2!=NULL;phead1=phead1->pnext,phead2=phead2->pnext)
{
printf("hello\n");
pt=phead1->data+phead2->data+carry_flag-48;
printf("1");
if(pt>=58)
{
pt=pt-10;
carry_flag=1;
printf("1");
}
else
{
carry_flag=0;
printf("2");
}
//phead3=phead3->pnext;
phead3=(PNode)malloc(sizeof(Node));
phead3->data=pt;
phead3->pnext=NULL;
*pr=phead3;
pr=&(*pr)->pnext;
}
/*
if(phead1==NULL&&phead2!=NULL)
{
while(phead2!=NULL)
{
pt=phead2->data+carry_flag;
if(pt>=58)
{
pt=pt-10;
carry_flag=1;
}
else
{
carry_flag=0;
}
phead2=phead2->pnext;
//phead3=phead3->pnext;
phead3=(PNode)malloc(sizeof(Node));
phead3->data=pt;
phead3->pnext=NULL;
*pr=phead3;
pr=&(*pr)->pnext;
}
}
else if(phead1!=NULL&&phead2==NULL)
{
while(phead1!=NULL)
{
phead3=(PNode)malloc(sizeof(Node));
phead3->data=phead1->data+carry_flag;
if(phead3->data>=58)
{
phead3->data=phead3->data-10;
carry_flag=1;
}
else
{
carry_flag=0;
}
phead1=phead1->pnext;
//phead3=phead3->pnext;
phead3->pnext=NULL;
*pr=phead3;
pr=&(*pr)->pnext;
}
}
else
if(phead1==NULL&&phead2==NULL)
{
phead3=(PNode)malloc(sizeof(Node));
if(carry_flag==1)
phead3->data=carry_flag+48;
}
Reverse(&phead3);
return phead3;*/
}
int main( )
{
PNode phead1,phead2,phead3;
phead1=phead2=phead3=NULL;
printf("please input the data to the list1\n");
creat_list(&phead1);
DumpList(phead1);
Reverse(&phead1);
DumpList(phead1);
printf("please input the data to the list2\n");
creat_list(&phead2);
DumpList(phead2);
Reverse(&phead2);
DumpList(phead2);
phead3=Add(phead1,phead2);
DumpList(phead3);
return 0;
}
我的这个程序是怎么回事啊?帮我看哈啊!