回复 6楼 longwu9t
#include<stdio.h>
#include<stdlib.h>
#define NULL 0
struct data
{
int num;
char name[20];
int tel[20];
struct data *next;
};
struct data *put_onelist(struct data *head)
{
struct data *p1;
int i;
if(head==NULL)
printf(" \n\n\n\n\t\tThis is empty list!\n\n\n\n");
else
{
printf("The serial number of input to output data node :
");
scanf("%d",&i);
for(p1=head;p1->num!=i;p1=p1->next);
printf(" Name: %s\n Tel: %s\n",p1->name,p1->tel);
}
}
struct data *add_data(struct data *head)
{
struct data *q1,*q2;
int a;
if(head==NULL)
{
printf("\n\n\n\n\t\tThis is empty list!\n\n\n\n");
}
else
{
printf("Input you need add data's num: ");
scanf("%d",&a);
for(q1=head;q1->num!=a-1;q1=q1->next);
q2=(struct data*)malloc(sizeof(struct data));
q2->num=a;
printf("Please Input name : ");
scanf("%s",q2->name);
printf("
Input tel : ");
scanf("%s",q2->tel);
q2->next=q1->next;
q1->next=q2;
}
}
struct data *Out_all_list( struct data *head)
{
struct data *lp;
if(head!=NULL)
{
lp=head;
while(lp!=NULL)
{
printf("\n num:\t%d\n name:\t%s\n tel:\t%s\n",lp->num,lp->name,lp->tel);
lp=lp->next;
}
printf("\n\n\n");
}
else
{
printf("\n\n\n\n\t\tThis is empty list!\n\n\n\n");
}
}
struct data *build_list(void)
{
struct data *head,*p,*q;
int n=1;
p=q=head=(struct data*)malloc(sizeof(struct data));
printf("Please input %d num:\t",n);
scanf("%d",&(p->num));
if(p->num==0)
{
printf("Please input num agine!\n");
}
printf("
Input %d name:\t",n);
scanf("%s",&p->name);
printf("
Input %d tel:\t",n);
scanf("%s",&(p->tel));
n++;
while(p->num!=0)
{
p=(struct data*)malloc(sizeof(struct data));
printf("please input %d num:\t",n);
scanf("%d",&(p->num));
if(p->num==0)
p->next=NULL;
else
{
printf("
Input %d name:\t",n);
scanf("%s",&p->name);
printf("
Input %ld tel:\t",n);
scanf("%s",&(p->tel));
q->next=p;
q=p;
}
n++;
}
return (head);
}
void main()
{
struct data *head=NULL;
char ch;
while(1)
{
fflush(stdin);
printf("\n\n\t--------- welcome to use the data management system ---------\n\n");
printf("\t\t(a) The input data \n\n\t\t(b) Output a data node\n\n\t\t(c) add a data \n\n\t\t(d) Output all data \n\n\t\t(e) exit \n\n");
printf("\t-------------------------------------------------------------\n");
printf("\n\tPlease enter the serial number and press enter to confirm: ");
scanf("%c",&ch);
switch(ch)
{
case 'a':
{
system("cls");
head=build_list();
break;
}
case 'b':
{
system("cls");
put_onelist(head);
break;
}
case 'c':
{
system("cls");
add_data(head);
break;
}
case 'd':
{
system("cls");
Out_all_list(head);
break;
}
case 'e':
{
return 0;
}
default:
printf("\tError!please input the serial number.\n");
}
getch();
system("cls");
}
}