#include<stdio.h>
#include<stdlib.h>
#define SIZE 10
#define size 20
typedef struct node1
{
char name1[SIZE];
char gender1;
int age1;
char position[size];
struct node1 *next1;
}employee;
typedef struct node2
{
char name[SIZE];
char gender;
int age;
char site[size];//the house's numbers
struct node2 *next2;
}customer;
int compare(char *s1,char*s2)
{
int re;
for(int i=0;i<SIZE-1;i++)
{
if(s1[i]==s2[i])
{
re=1;
continue;
}
else
{
re=0;
break;
}
}
if(re==0)
{
return 0;
}
else
{
return 1;
}
}
void copy(char *a,char *b)
{
for(size_t i=0;(a[i]=b[i])!='\0';i++)
{
;
}
}
void search(customer *Searchptr,char *name1ptr)//传递Startptr->next2
{
while(Searchptr==NULL)
{
printf("链表为空\n");
}
while(Searchptr!=NULL&&compare(Searchptr->name,name1ptr)==0)
{
Searchptr=Searchptr->next2;
}
if(Searchptr!=NULL)
{
printf("%s",Searchptr->name);
printf(" %c",Searchptr->gender);
printf(" %d",Searchptr->age);
printf(" %s",Searchptr->site);
}
}
void change(customer *Changeptr,char *nameptr)//传递Startptr->next2
{
int a;//年龄
char s[size];//住房地址
while(Changeptr!=NULL&&compare(Changeptr->name,nameptr)==0)
{
Changeptr=Changeptr->next2;
}
if(Changeptr!=NULL)
{
printf("\n-------------------------------\n"
" |please input site of customer|\n"
" -------------------------------\n");
scanf("%s",s);
printf(" ------------------------------\n"
" |please input age of customer|\n"
" ------------------------------\n");
scanf("%d",&a);
Changeptr->age=a;
copy(Changeptr->site,s);
}
}
void input(employee *Emptr,char *Name,char Gender,int Age,char *Position)//insert customer data
{
Emptr=malloc(sizeof(employee));
Emptr->next1=NULL;
employee *emptr=malloc(sizeof(employee));
if(emptr!=NULL)
{
Emptr->next1=emptr;
copy(emptr->name1,Name);
emptr->gender1=Gender;
emptr->age1=Age;
copy(emptr->position,Position);
emptr->next1=Emptr->next1;
}
}//number is employee's numbers;input employee data
void insert (customer *Cusptr,char Name[],char Gender,int Age,char Site[])//insert customer data
{
customer *newptr=malloc(sizeof(customer));
if(newptr!=NULL)
{
Cusptr->next2=newptr;
copy(newptr->name,Name);
newptr->gender=Gender;
newptr->age=Age;
copy(newptr->site,Site);
newptr->next2=Cusptr->next2;
}
}
void Delete(customer *Cusptr,char *Name)
{
if(compare(Cusptr->name,Name))
{
customer *tempptr=Cusptr;
Cusptr=Cusptr->next2;
free(tempptr);
}
else
{
customer *preptr=Cusptr;
customer *curptr=Cusptr->next2;
while(curptr!=NULL&&compare(curptr->name,Name)==0)
{
preptr=curptr;
curptr=curptr->next2;
}
if(curptr!=NULL)
{
customer *temptr=curptr;
preptr->next2=curptr->next2;
free(temptr);
}
}
}
int main(void)
{
employee *startptr=NULL;
customer *Startptr=NULL;
unsigned int choice;
char p[size];//职位
char n1[SIZE];//名字
char g;//性别
int a;//年龄
char s[size];//住房地址
char N[SIZE];//想要删掉的客户的名字
printf("****************************************\n"
"* input 1 to insert customer data *\n"
"* input 2 to delete customer data *\n"
"* input 3 to change customer data *\n"
"* input 4 to choose service object *\n"
"* input 5 to insert employee data *\n"
"* input 6 to search customer *\n"
"* input 7 to end *\n"
"****************************************\n");//service object----服务对象
printf("input your choice from 1 to 7\n");
scanf("%u",&choice);
getchar();
while(choice!=7)
{
switch(choice)
{
case 1:
printf("\n-------------------------------\n"
" |please input name of customer|\n"
" -------------------------------\n");
scanf("%s",n1);
printf(" -------------------------------\n"
" |please input site of customer|\n"
" -------------------------------\n");
scanf("%s",s);
getchar();
printf(" ------------------------------\n"
" |please input age of customer|\n"
" ------------------------------\n");
scanf("%d",&a);
getchar();
printf(" ---------------------------------\n"
" |please input gender of customer|\n"
" ---------------------------------\n");
scanf("%c",&g);
getchar();
insert(Startptr,n1,g,a,s);
break;
case 2:
printf(" --------------------------------------------------\n"
" |please input name of customer you want to delete|\n"
" --------------------------------------------------\n");
scanf("%s",N);
Delete(Startptr->next2,N);
break;
case 3:
printf(" --------------------------------------------------\n"
" |please input name of customer you want to change|\n"
" --------------------------------------------------\n");
scanf("%s",N);
change(Startptr->next2,N);
break;
case 4:
break;
case 5:
printf(" -------------------------------\n"
" |please input name of employee|\n"
" -------------------------------\n");
scanf("%s",n1);
printf(" -----------------------------------\n"
" |please input position of employee|\n"
" -----------------------------------\n");
scanf("%s",p);
printf(" ------------------------------\n"
" |please input age of employee|\n"
" ------------------------------\n");
scanf("%d",&a);
printf(" ---------------------------------\n"
" |please input gender of employee|\n"
" ---------------------------------\n");
scanf("%c",&g);
input(startptr,n1,g,a,p);
break;
case 6:
scanf("%s",N);
search(Startptr,N);
break;
}
printf("input your choice from 1 to 7\n");
scanf("%u",&choice);
getchar();
}
}
这是错误的运行结果,就是:输入1以后可以运行,但是运行完一的部分,这个循环就自动跳出去了。
这个代码的目的是:用户包括工作人员employee和客户customer)管理,利用链表,修改(change函数)删除(Delete函数)查找(search函数)插入(input函数插入工作人员的信息,insert函数插入客户的信息)