69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct worker)
struct worker
{
int num;
char name[20];
int salary;
struct worker *next;
};
int main()
{
int n=0;
struct worker *p1,*p2,*head;
p1=p2=(struct worker*)malloc(LEN);
scanf("%d",&p1->num);
scanf("%s",p1->name);
scanf("%d",&p1->salary);
head=NULL;
if(p1->num!=0)
while(1)
{
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct worker*)malloc(LEN);
scanf("%d",&p1->num);
if(p1->num == 0) break;
scanf("%s",p1->name);
scanf("%d",&p1->salary);
}
p2->next=NULL;
while(p1!=NULL)
{
printf("%d,%s,%d\n",p1->num,p1->name,p1->salary);
p1=p1->next;
}
printf("Please input new worker's information\n");
struct worker *newer;
newer=(struct worker*)malloc(LEN);
scanf("%d",&newer->num);
scanf("%s",newer->name);
scanf("%d",&newer->salary);
n++;
p1=head;
if(head==NULL)
{
head=newer;
newer->next=NULL;
}
else
{
while((newer->num > p1->num)&&(p1->next != NULL))
{
p2=p1;
p1=p1->next;
}
if(newer->num <= p1->num)
{
if(p1==head) head=newer;
else p2->next=newer;
newer->next=p1;
}
else
{
p1->next=newer;
newer->next=NULL;
}
}
}
d:\>gcc c.c
d:\>a
1 1 1
2 2 2
0 0 0
1,1,1
2,2,2
Please input new worker's information
1 1 1
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct worker
{
int num;
char name[20];
int salary;
struct worker *next;
};
#define LEN sizeof(struct worker)
int main()
{
int n=0;
struct worker *p1,*p2,*head;
p1=p2=(struct worker*)malloc(LEN);
scanf("%d",&p1->num);
scanf("%s",p1->name);
scanf("%d",&p1->salary);
head=NULL;
if(p1->num!=0)
while(1)
{
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct worker*)malloc(LEN);
scanf("%d",&p1->num);
if(p1->num == 0) break;
scanf("%s",p1->name);
scanf("%d",&p1->salary);
}
p2->next=NULL;
/*-----------------------------------------------------------------*/
struct worker*p; /* 这里该一下 */
p=head;
while(p!=NULL)
{
printf("%d,%s,%d\n",p->num,p->name,p->salary);
p=p->next;
}
/*-----------------------------------------------------------------*/
printf("Please input new worker's information\n");
struct worker *newer;
newer=(struct worker*)malloc(LEN);
scanf("%d",&newer->num);
scanf("%s",newer->name);
scanf("%d",&newer->salary);
n++;
p1=head;
if(head==NULL)
{
head=newer;
newer->next=NULL;
}
else
{
while((newer->num > p1->num)&&(p1->next != NULL))
{
p2=p1;
p1=p1->next;
}
if(newer->num <= p1->num)
{
if(p1==head) head=newer;
else p2->next=newer;
newer->next=p1;
}
else
{
p1->next=newer;
newer->next=NULL;
}
}
/* 这里加一条输出的测试 */
p=head;
while(p!=NULL)
{
printf("%d,%s,%d\n",p->num,p->name,p->salary);
p=p->next;
}
return 0;
}
结果:
1 huang 1000
3 yu 2000
0
1,huang,1000
3,yu,2000
Please input new worker's information
2 xiao 3000
1,huang,1000
2,xiao,3000
3,yu,2000
Press any key to continue
int n=0;
struct worker *p1,*p2,*head;
p1=p2=(struct worker*)malloc(LEN);
scanf("%d",&p1->num);
scanf("%s",p1->name);
scanf("%d",&p1->salary);
head=NULL;
if(p1->num!=0)
while(1)
{
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct worker*)malloc(LEN);
scanf("%d",&p1->num);
if(p1->num == 0) break;
scanf("%s",p1->name);
scanf("%d",&p1->salary);
}
p2->next=NULL;
p1 = head;/*notice*/
while(p1!=NULL)
{
printf("%d,%s,%d\n",p1->num,p1->name,p1->salary);
p1=p1->next;
}
printf("Please input new worker's information\n");
{
struct worker *newer;
newer=(struct worker*)malloc(LEN);
scanf("%d",&newer->num);
scanf("%s",newer->name);
scanf("%d",&newer->salary);
n++;
p1=head;
if(head==NULL)
{
head=newer;
newer->next=NULL;
}
else
{
while((newer->num > p1->num)&&(p1->next != NULL))
{
p2=p1;
p1=p1->next;
}
if(newer->num <= p1->num)
{
if(p1==head) head=newer;
else p2->next=newer;
newer->next=p1;
}
else
{
p1->next=newer;
newer->next=NULL;
}
}
}