#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 30
#define OK 1
#define ERROR 0
typedef char ElementType;//用ElementType代替int
typedef int Status;
typedef struct node
{
char name[MAX_SIZE];//名字
char num[MAX_SIZE];//号码
char addr[MAX_SIZE];//地址
struct node * next;
}Node;
Status init(Node**head);
Status inserthead(Node*head, ElementType *name, ElementType *addr, ElementType *num);
Status deletename(Node*head, ElementType *name);
Status deletenum(Node*head, ElementType *num);
Status deleteaddr(Node*head, ElementType *addr);
void queryname(Node*head, ElementType *name);
void querynum(Node*head, ElementType *num);
void queryaddr(Node*head, ElementType *addr);
Status updatename(Node*head, ElementType *name);
Status updateaddr(Node*head, ElementType *add);
Status updatenum(Node*head, ElementType *num);
Status name_sort(Node* head, ElementType* name);
void txt(Node* head);
void print(Node*head);
int main()
{
Node*head;
int ret = init(&head);
if (ret == ERROR)
{
return -1;
}
printf("********************************************************");
printf("\n\n\t选择你需要操作的功能\n");
printf("\n");
printf("\t\t\t0.[增加通讯信息]\n");
printf("\t\t\t1.[根据电话号码来删除需要删除的信息]\n");
printf("\t\t\t2.[根据地址来删除需要删除的信息]\n");
printf("\t\t\t3.[根据名字来删除需要删除的信息]\n");
printf("\t\t\t4.[根据电话号码来查找需要的信息]\n");
printf("\t\t\t5.[根据地址来查找需要的信息]\n");
printf("\t\t\t6.[根据名字来查找需要的信息]\n");
printf("\t\t\t7.[根据电话号码来修改其他的信息]\n");
printf("\t\t\t8.[根据地址来修改其他的信息]\n");
printf("\t\t\t9.[根据名字来修改其他的信息]\n");
printf("\t\t\t10.[退出]\n");
printf("\t\t\t11.[导入文件]\n");
printf("\n\n");
printf("********************************************************\n");
int n;
char name[20];
char addr[20];
char num[20];
int flag = 1;
while(flag == 1)
{
printf("选择你需要操作的功能号(0 - 10): ");
scanf("%d", &n);
switch(n)
{
case 0:
{
printf("请输入要添加的用户姓名,地址和电话:");
scanf("%s %s %s",name, addr, num);
inserthead(head, name, addr, num);
name_sort(head, name);
print(head);
break;
}
case 1:
{
printf("请输入电话来删除该信息:");
scanf("%s", num);
deletenum(head, num);
print(head);
break;
}
case 2:
{
printf("请输入地址来删除该信息:");
scanf("%s", addr);
deleteaddr(head, addr);
print(head);
break;
}
case 3:
{
printf("请输入名字来删除该信息:");
scanf("%s", name);
deletename(head, name);
print(head);
break;
}
case 4:
{
printf("请输入电话号码来查找:");
scanf("%s", num);
querynum(head, num);
// print(head);
break;
}
case 5:
{
printf("请输入地址来查找:");
scanf("%s", addr);
queryaddr(head, addr);
// print(head);
break;
}
case 6:
{
printf("请输入名字来查找:");
scanf("%s", name);
queryname(head, name);
//print(head);
break;
}
case 7:
{
printf("请输入电话号码来修改其他信息:");
scanf("%s", num);
updatenum(head, num);
print(head);
break;
}
case 8:
{
printf("请输入地址来修改其他信息:");
scanf("%s", addr);
updatenum(head, addr);
print(head);
break;
}
case 9:
{
printf("请输入名字来修改其他信息:");
scanf("%s", name);
updatenum(head, name);
print(head);
break;
}
case 10:
{
flag = 2;
break;
}
case 11:
{
txt(head);
break;
}
}
}
return 0;
}
void txt(Node* head)
{
FILE* file = fopen("tongxunlu.txt","w+");
if (NULL == file)
{
perror("1");
exit(1);
}
while (head->next != NULL)
{
fprintf(file, "name = %s addr = %s num = %s\n", head->next->name, head->next->addr, head->next->num);
head = head -> next;
}
fclose(file);
}
Status init(Node**head)//初始化头节点
{
Node*new = (Node*)malloc(sizeof(Node));
if (NULL == new)
{
return ERROR;
}
*head = new;
new -> next = NULL;
return OK;
}
Status inserthead(Node*head, ElementType *name, ElementType *addr, ElementType *num)//头插
{
Node * new = (Node*)malloc(sizeof(Node));
if (NULL == new)
{
return ERROR;
}
strcpy(new -> name, name);
strcpy(new -> addr, addr);
strcpy(new -> num, num);
if (head->next == NULL)
{
head->next = new;
new->next = NULL;
}
else
{
new->next = head->next;
head->next = new;
}
}
Status deletename(Node*head, ElementType *name)//删除联系人
{
while (head -> next != NULL)
{
if (strcmp(head -> next -> name, name) == 0)
{
Node*temp = head -> next;
head -> next = head -> next -> next;
free(temp);
temp = NULL;
}
else
{
head = head -> next;
}
}
}
Status deletenum(Node*head, ElementType *num)
{
while (head -> next != NULL)
{
if (strcmp(head -> next -> num, num) == 0)
{
Node*temp = head -> next;
head -> next = head -> next -> next;
free(temp);
temp = NULL;
}
else
{
head = head -> next;
}
}
}
Status deleteaddr(Node*head, ElementType *addr)
{
while (head -> next != NULL)
{
if (strcmp(head -> next -> addr, addr) == 0)
{
Node*temp = head -> next;
head -> next = head -> next -> next;
free(temp);
temp = NULL;
}
else
{
head = head -> next;
}
}
}
void queryname(Node*head, ElementType *name)//查询联系人
{
int index = 1;
int temp = 0;
while (head -> next != NULL)
{
if (strcmp(head -> next -> name, name) == 0)
{
printf("index = %d, name = %s\n", index, name);
temp++;
}
index++;
head = head -> next;
}
if (temp == 0)
{
printf("%s is not in this list\n", name);
}
}
void querynum(Node*head, ElementType *num)
{
int index = 1;
int temp = 0;
while (head -> next != NULL)
{
if (strcmp(head -> next -> num, num) == 0)
{
printf("index = %d, num = %s\n", index, num);
temp++;
}
index++;
head = head -> next;
}
if (temp == 0)
{
printf("%s is not in this list\n", num);
}
}
void queryaddr(Node*head, ElementType *addr)
{
int index = 1;
int temp = 0;
while (head -> next != NULL)
{
if (strcmp(head -> next -> addr, addr) == 0)
{
printf("index = %d, addr = %s\n", index, addr);
temp++;
}
index++;
head = head -> next;
}
if (temp == 0)
{
printf("%s is not in this list\n", addr);
}
}
Status updatename(Node*head, ElementType *name)
{
int index = 1;
int temp = 0;
while (head -> next != NULL)
{
if (strcmp(head -> next -> name, name) == 0)
{
printf("请输入你要修改的地址或电话号码:");
scanf("%s", head->next->addr);
scanf("%s", head->next->num);
// strcpy(head -> next -> num, num);
//strcpy(head -> next -> addr, addr);
temp++;
}
index++;
head = head -> next;
}
if (temp == 0)
{
printf("%s is not in this list\n", name);
}
}
Status updateaddr(Node*head, ElementType *addr)
{
int index = 0;
int temp = 0;
while (head -> next != NULL)
{
if (strcmp(head -> next -> addr, addr) == 0)
{
printf("请输入你要修改的名字或电话号码:");
scanf("%s", head->next->name);
scanf("%s", head->next->num);
//strcpy(head -> next -> num, num);
//strcpy(head -> next -> name, name);
temp++;
}
index++;
head = head -> next;
}
if (temp == 0)
{
printf("%s is not in this list\n", addr);
}
}
Status updatenum(Node*head, ElementType *num)//修改联系人
{
int index = 0;
int temp = 0;
while (head -> next != NULL)
{
if (strcmp(head -> next -> num, num) == 0)
{
printf("请输入你要修改的地址或名字:");
scanf("%s", head->next->addr);
scanf("%s", head->next->name);
// strcpy(head -> next -> addr, addr);
// strcpy(head -> next -> name, name);
temp++;
}
index++;
head = head -> next;
}
if (temp == 0)
{
printf("%s is not in this list\n", num);
}
}
void print(Node*head)//打印
{
Node *tmp = head->next;
if(tmp == NULL)
{
return ;
}
while (tmp != NULL)
{
printf("name = %s\n", tmp->name);
printf("addr = %s\n", tmp->addr);
printf("num = %s\n", tmp->num);
tmp = tmp->next;
}
}
Status name_sort(Node* head, ElementType* name)//排序
{
Node*temp = NULL;
ElementType* small;
if(head->next == NULL)
{
printf("Can't sort,Linklist is empty!\n");
return 0;
}
while (head->next != NULL)
{
temp = head->next;
while (temp->next != NULL)
{
if (strcmp(head->next->name, temp->next->name) < 0)
{
strcpy(small, temp->next->name);
strcpy(temp->next->name, head->next->name);
strcpy(head->next->name, small);
strcpy(small, head->next->addr);
strcpy(head->next->addr, temp->next->addr);
strcpy(temp->next->addr, small);
strcpy(small, temp->next->num);
strcpy(temp->next->num,head->next->num);
strcpy(head->next->num, small);
}
temp = temp->next;
}
head = head->next;
}
}
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 30
#define OK 1
#define ERROR 0
typedef char ElementType;//用ElementType代替int
typedef int Status;
typedef struct node
{
char name[MAX_SIZE];//名字
char num[MAX_SIZE];//号码
char addr[MAX_SIZE];//地址
struct node * next;
}Node;
Status init(Node**head);
Status inserthead(Node*head, ElementType *name, ElementType *addr, ElementType *num);
Status deletename(Node*head, ElementType *name);
Status deletenum(Node*head, ElementType *num);
Status deleteaddr(Node*head, ElementType *addr);
void queryname(Node*head, ElementType *name);
void querynum(Node*head, ElementType *num);
void queryaddr(Node*head, ElementType *addr);
Status updatename(Node*head, ElementType *name);
Status updateaddr(Node*head, ElementType *add);
Status updatenum(Node*head, ElementType *num);
Status name_sort(Node* head, ElementType* name);
void txt(Node* head);
void print(Node*head);
int main()
{
Node*head;
int ret = init(&head);
if (ret == ERROR)
{
return -1;
}
printf("********************************************************");
printf("\n\n\t选择你需要操作的功能\n");
printf("\n");
printf("\t\t\t0.[增加通讯信息]\n");
printf("\t\t\t1.[根据电话号码来删除需要删除的信息]\n");
printf("\t\t\t2.[根据地址来删除需要删除的信息]\n");
printf("\t\t\t3.[根据名字来删除需要删除的信息]\n");
printf("\t\t\t4.[根据电话号码来查找需要的信息]\n");
printf("\t\t\t5.[根据地址来查找需要的信息]\n");
printf("\t\t\t6.[根据名字来查找需要的信息]\n");
printf("\t\t\t7.[根据电话号码来修改其他的信息]\n");
printf("\t\t\t8.[根据地址来修改其他的信息]\n");
printf("\t\t\t9.[根据名字来修改其他的信息]\n");
printf("\t\t\t10.[退出]\n");
printf("\t\t\t11.[导入文件]\n");
printf("\n\n");
printf("********************************************************\n");
int n;
char name[20];
char addr[20];
char num[20];
int flag = 1;
while(flag == 1)
{
printf("选择你需要操作的功能号(0 - 10): ");
scanf("%d", &n);
switch(n)
{
case 0:
{
printf("请输入要添加的用户姓名,地址和电话:");
scanf("%s %s %s",name, addr, num);
inserthead(head, name, addr, num);
name_sort(head, name);
print(head);
break;
}
case 1:
{
printf("请输入电话来删除该信息:");
scanf("%s", num);
deletenum(head, num);
print(head);
break;
}
case 2:
{
printf("请输入地址来删除该信息:");
scanf("%s", addr);
deleteaddr(head, addr);
print(head);
break;
}
case 3:
{
printf("请输入名字来删除该信息:");
scanf("%s", name);
deletename(head, name);
print(head);
break;
}
case 4:
{
printf("请输入电话号码来查找:");
scanf("%s", num);
querynum(head, num);
// print(head);
break;
}
case 5:
{
printf("请输入地址来查找:");
scanf("%s", addr);
queryaddr(head, addr);
// print(head);
break;
}
case 6:
{
printf("请输入名字来查找:");
scanf("%s", name);
queryname(head, name);
//print(head);
break;
}
case 7:
{
printf("请输入电话号码来修改其他信息:");
scanf("%s", num);
updatenum(head, num);
print(head);
break;
}
case 8:
{
printf("请输入地址来修改其他信息:");
scanf("%s", addr);
updatenum(head, addr);
print(head);
break;
}
case 9:
{
printf("请输入名字来修改其他信息:");
scanf("%s", name);
updatenum(head, name);
print(head);
break;
}
case 10:
{
flag = 2;
break;
}
case 11:
{
txt(head);
break;
}
}
}
return 0;
}
void txt(Node* head)
{
FILE* file = fopen("tongxunlu.txt","w+");
if (NULL == file)
{
perror("1");
exit(1);
}
while (head->next != NULL)
{
fprintf(file, "name = %s addr = %s num = %s\n", head->next->name, head->next->addr, head->next->num);
head = head -> next;
}
fclose(file);
}
Status init(Node**head)//初始化头节点
{
Node*new = (Node*)malloc(sizeof(Node));
if (NULL == new)
{
return ERROR;
}
*head = new;
new -> next = NULL;
return OK;
}
Status inserthead(Node*head, ElementType *name, ElementType *addr, ElementType *num)//头插
{
Node * new = (Node*)malloc(sizeof(Node));
if (NULL == new)
{
return ERROR;
}
strcpy(new -> name, name);
strcpy(new -> addr, addr);
strcpy(new -> num, num);
if (head->next == NULL)
{
head->next = new;
new->next = NULL;
}
else
{
new->next = head->next;
head->next = new;
}
}
Status deletename(Node*head, ElementType *name)//删除联系人
{
while (head -> next != NULL)
{
if (strcmp(head -> next -> name, name) == 0)
{
Node*temp = head -> next;
head -> next = head -> next -> next;
free(temp);
temp = NULL;
}
else
{
head = head -> next;
}
}
}
Status deletenum(Node*head, ElementType *num)
{
while (head -> next != NULL)
{
if (strcmp(head -> next -> num, num) == 0)
{
Node*temp = head -> next;
head -> next = head -> next -> next;
free(temp);
temp = NULL;
}
else
{
head = head -> next;
}
}
}
Status deleteaddr(Node*head, ElementType *addr)
{
while (head -> next != NULL)
{
if (strcmp(head -> next -> addr, addr) == 0)
{
Node*temp = head -> next;
head -> next = head -> next -> next;
free(temp);
temp = NULL;
}
else
{
head = head -> next;
}
}
}
void queryname(Node*head, ElementType *name)//查询联系人
{
int index = 1;
int temp = 0;
while (head -> next != NULL)
{
if (strcmp(head -> next -> name, name) == 0)
{
printf("index = %d, name = %s\n", index, name);
temp++;
}
index++;
head = head -> next;
}
if (temp == 0)
{
printf("%s is not in this list\n", name);
}
}
void querynum(Node*head, ElementType *num)
{
int index = 1;
int temp = 0;
while (head -> next != NULL)
{
if (strcmp(head -> next -> num, num) == 0)
{
printf("index = %d, num = %s\n", index, num);
temp++;
}
index++;
head = head -> next;
}
if (temp == 0)
{
printf("%s is not in this list\n", num);
}
}
void queryaddr(Node*head, ElementType *addr)
{
int index = 1;
int temp = 0;
while (head -> next != NULL)
{
if (strcmp(head -> next -> addr, addr) == 0)
{
printf("index = %d, addr = %s\n", index, addr);
temp++;
}
index++;
head = head -> next;
}
if (temp == 0)
{
printf("%s is not in this list\n", addr);
}
}
Status updatename(Node*head, ElementType *name)
{
int index = 1;
int temp = 0;
while (head -> next != NULL)
{
if (strcmp(head -> next -> name, name) == 0)
{
printf("请输入你要修改的地址或电话号码:");
scanf("%s", head->next->addr);
scanf("%s", head->next->num);
// strcpy(head -> next -> num, num);
//strcpy(head -> next -> addr, addr);
temp++;
}
index++;
head = head -> next;
}
if (temp == 0)
{
printf("%s is not in this list\n", name);
}
}
Status updateaddr(Node*head, ElementType *addr)
{
int index = 0;
int temp = 0;
while (head -> next != NULL)
{
if (strcmp(head -> next -> addr, addr) == 0)
{
printf("请输入你要修改的名字或电话号码:");
scanf("%s", head->next->name);
scanf("%s", head->next->num);
//strcpy(head -> next -> num, num);
//strcpy(head -> next -> name, name);
temp++;
}
index++;
head = head -> next;
}
if (temp == 0)
{
printf("%s is not in this list\n", addr);
}
}
Status updatenum(Node*head, ElementType *num)//修改联系人
{
int index = 0;
int temp = 0;
while (head -> next != NULL)
{
if (strcmp(head -> next -> num, num) == 0)
{
printf("请输入你要修改的地址或名字:");
scanf("%s", head->next->addr);
scanf("%s", head->next->name);
// strcpy(head -> next -> addr, addr);
// strcpy(head -> next -> name, name);
temp++;
}
index++;
head = head -> next;
}
if (temp == 0)
{
printf("%s is not in this list\n", num);
}
}
void print(Node*head)//打印
{
Node *tmp = head->next;
if(tmp == NULL)
{
return ;
}
while (tmp != NULL)
{
printf("name = %s\n", tmp->name);
printf("addr = %s\n", tmp->addr);
printf("num = %s\n", tmp->num);
tmp = tmp->next;
}
}
Status name_sort(Node* head, ElementType* name)//排序
{
Node*temp = NULL;
ElementType* small;
if(head->next == NULL)
{
printf("Can't sort,Linklist is empty!\n");
return 0;
}
while (head->next != NULL)
{
temp = head->next;
while (temp->next != NULL)
{
if (strcmp(head->next->name, temp->next->name) < 0)
{
strcpy(small, temp->next->name);
strcpy(temp->next->name, head->next->name);
strcpy(head->next->name, small);
strcpy(small, head->next->addr);
strcpy(head->next->addr, temp->next->addr);
strcpy(temp->next->addr, small);
strcpy(small, temp->next->num);
strcpy(temp->next->num,head->next->num);
strcpy(head->next->num, small);
}
temp = temp->next;
}
head = head->next;
}
}