|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这个是复制单链表的程序,后半部分有问题,不知道错在哪里,请指点下
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
struct workers{
char name[20];
int wage;
struct workers *next;
}*l,*tail,*New,*ntail,*p,*q,*r;
char name[20];
int wage;
int i=0;
int size=sizeof(struct workers);
l=tail=NULL;
printf("Please input workers' name and wage:\n");
scanf("%s",name);
scanf("%d",&wage);
p=(struct workers *)malloc(size);
strcpy(p->name,name);
p->wage=wage;
p->next=NULL;
l=tail=p;
while(i<2)
{
p=(struct workers *)malloc(size);
if(tail->next==NULL)
{
tail->next=p;
tail=p;
}
printf("Please input workers' name and wage:\n");
scanf("%s",name);
scanf("%d",&wage);
strcpy(tail->name,name);
tail->wage=wage;
tail->next=NULL;
i++;
}
for(p=l;p;p=p->next)
printf("%s%d",p->name,p->wage);
New=ntail=NULL;p=l;
q=(struct workers *)malloc(size);
ntail=New=q;
while(p){
strcpy(ntail->name,p->name);
ntail->wage=p->wage;
if(ntail->next==NULL)
{
q=(struct workers *)malloc(size);
ntail->next=q;
ntail=q;
p=p->next;
strcpy(ntail->name,p->name);
ntail->wage=p->wage;
}
}
r=New;
for(r=New;r->name;r=r->next)
printf("%s %d",r->name,r->wage);
} |
|