发布网友 发布时间:2022-05-13 15:38
共1个回答
热心网友 时间:2023-10-14 18:29
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define F-1
#define T 1
struct Address
{
char name[20];
char number[12];
char address[20];
struct Address*next;
};
typedef struct Address*node;
int init(node*head);
int creat_tail(node head);
int insert_index(node head);
int length(node head);
int query_name(node head);
int delete_address(node head);
void print(node head);
int main()
{
node head;
init(&head);
int x;
do
{
printf("0:exit\n");
printf("1:creat_tail\n");
printf("2:insert_index\n");
printf("3:query_name\n");
printf("4:delete_address\n");
printf("5:print\n");
printf("please select\n");
scanf("%d",&x);
switch(x)
{
case 0:
exit(0);
case 1:
creat_tail(head);
break;
case 2:
insert_index(head);
break;
case 3:
query_name(head);
break;
case 4:
delete_address(head);
break;
case 5:
print(head);
break;
default:
exit(0);
}
}
while(1);
return 0;
}
int delete_address(node head)
{
char address[20];
printf("please input the address you want to delete\n");
scanf("%s",address);
while(head->next!=NULL)
{
if(strcmp(head->next->address,address)==0)
{
node temp=head->next;
head->next=head->next->next;
free(temp);
}
else
{
head=head->next;
}
}
return T;
}
int query_name(node head)
{
char name[20];
int count=0,index=0;
printf("please input the name you want\n");
scanf("%s",name);
while(head->next!=NULL)
{
if(strcmp(head->next->name,name)==0)
{
count++;
printf("%d.name:%s number:%s address:%s\n",index+1,head->next->name,head->next->number,head->next->address);
}
head=head->next;
index++;
}
if(count==0)
{
printf("not found\n");
}
return T;
}
int length(node head)
{
int count=0;
while(head->next!=NULL)
{
head=head->next;
count++;
}
return count;
}
int insert_index(node head)
{
int index;
printf("please input the index you want to add\n");
scanf("%d",&index);
if(index<0||index>=length(head))
{
printf("out of range\n");
return F;
}
node newnode=(node)malloc(sizeof(struct Address));
if(NULL==newnode)
{
return F;
}
int i;
for(i=0;i<index;i++)
{
head=head->next;
}
printf("please input the name,number,address\n");
printf("when you input 0 0 0,exit\n");
scanf("%s%s%s",newnode->name,newnode->number,newnode->address);
if(strcmp(newnode->name,"0")!=0)
{
newnode->next=head->next;
head->next=newnode;
}
return T;
}
void print(node head)
{
int count=0;
while(head->next!=NULL)
{
count++;
printf("%d.name:%s number:%s address:%s\n",count,head->next->name,head->next->number,head->next->address);
head=head->next;
}
}
int creat_tail(node head)
{
do
{
node newnode=(node)malloc(sizeof(struct Address));
if(NULL==newnode)
{
return F;
}
printf("please input the name,number,address\n");
printf("when you input 0 0 0,exit\n");
scanf("%s%s%s",newnode->name,newnode->number,newnode->address);
if(strcmp(newnode->name,"0")!=0)
{
newnode->next=NULL;
while(head->next!=NULL)
{
head=head->next;
}
head->next=newnode;
}
else
{
break;
}
}
while(1);
return T;
}
int init(node*head)
{
node newnode=(node)malloc(sizeof(struct Address));
if(NULL==newnode)
{
return F;
}
strcpy(newnode->name,"0");
strcpy(newnode->number,"0");
strcpy(newnode->address,"0");
newnode->next=NULL;
(*head)=newnode;
return T;
}