该通讯录利用了双向循环链表, 通讯录能记录id, name, number. 它的功能有:输入数据(尾插法),根据id, name, number实现删除, 查找,更新数据,排序,输出数据的功能。 其中排序用了两种方法, 对name进行从小到大的冒泡排序法 ,和对number进行从小到大的快速排序法。当输入指令为end时,就结束对通讯录的访问和修改。
#include <stdio.h>
#include <string.h>#include <stdlib.h>
#define T 1
#define F -1
typedef int el;
typedef int bo;
typedef char ch;
typedef struct Address* address;
struct Address
{
el id;
ch name[10];
ch number[12];
address next;
address prior;
};
address unit_index(address head, int index);
bo init(address* head);
void rank_id(address head);
void rank_name(address head);
void rank_number(address head, int l, int r);
bo insert_tail(address head, ch *name, ch *number);
bo delete_id(address head, int id);
bo delete_name(address head, ch *name);
bo delete_number(address head, ch *number);
bo query_id(address head, el id);
void query_name(address head, ch *name);
void query_number(address head, ch *number);
void update_name(address head, ch *name, char *name1, char *number);
void update_number(address head, ch *number, char *name1, char *number1);
bo update_id(address head, el id, char *name1, char *number);
bo length(address head);
void print(address head);
bo main()
{
int id, k, j;
char name1[10];
char num[12];
char name2[10];
char num1[12];
char v[10];
int x = 1;
char* m = (char *)malloc(sizeof(char)*20);
char n[10];
address head = NULL;
init(&head);
printf("**************************************you have six orders:insert, delete, query, print, end, rank************************************\n");
while(1 == x)
{
printf("please input a order:");
scanf("%s", n);
strcpy(m, n);
if(strcmp(m, "rank") == 0)
{
printf("rank according to name or number?");
scanf("%s", v);
if(strcmp(v, "name") == 0)
{
rank_name(head);
print(head);
}
if(strcmp(v, "number") == 0)
{
rank_number(head, 0, length(head));
print(head);
}
}
if(strcmp(m, "insert") == 0)
{
printf("how many address do you want to insert?");
scanf("%d", &j);
for(k = 1; k <= j; k++)
{
printf("please input name and number:");
scanf("%s %s", name1, num);
insert_tail(head, name1, num);
}
}
if(strcmp(m, "print") == 0)
{
print(head);
}
if(strcmp(m, "delete") ==0)
{
printf("which do you want to delete id, name or number:");
scanf("%s", v);
if(strcmp(v, "id") == 0)
{
printf("please input id:");
scanf("%d", &id);
delete_id(head, id);
}
if(strcmp(v, "name") == 0)
{
printf("please input name:");
scanf("%s", name1);
delete_name(head, name1);
}
if(strcmp(v, "number") == 0)
{
printf("please input number:");
scanf("%s", num);
delete_number(head, num);
}
print(head);
}
if(strcmp(m, "query") ==0)
{
printf("which do you want to query id, name or number:");
scanf("%s", v);
if(strcmp(v, "id") == 0)
{
printf("please input id:");
scanf("%d", &id);
query_id(head, id);
}
if(strcmp(v, "name") == 0)
{
printf("please input name:");
scanf("%s", name1);
query_name(head, name1);
}
if(strcmp(v, "number") == 0)
{
printf("please input number:");
scanf("%s", num);
query_number(head, num);
}
}
if(strcmp(m, "update") ==0)
{
printf("which do you want to update id, name or number:");
scanf("%s", v);
if(strcmp(v, "id") == 0)
{
printf("please input id:");
scanf("%d", &id);
printf("update it to:");
scanf("%s %s", name1, num);
update_id(head, id, name1, num);
}
if(strcmp(v, "name") == 0)
{
printf("please input name:");
scanf("%s", name1);
printf("update it to:");
scanf("%s %s", name2, num1);
update_name(head, name1, name2, num1);
}
if(strcmp(v, "number") == 0)
{
printf("please input number:");
scanf("%s", num);
printf("update it to:");
scanf("%s %s", name2, num1);
update_number(head, num, name2, num1);
}
print(head);
}
if(strcmp(m, "end") == 0)
{
printf("**************************************************************thank you**************************************************************\n");
x = 0;
}
}
return 0;
}
void rank_number(address head, int l, int r)
{
int i = l, j = r, m;
char name[20];
char num[20];
strcpy(name, unit_index(head, l)->name);
strcpy(num, unit_index(head, l)->number);
while(i < j)
{
while(i < j && strcmp(unit_index(head, j)->number, num) >= 0)
{
j--;
}
if(i < j)
{
strcpy(unit_index(head, i)->name, unit_index(head, j)->name);
strcpy(unit_index(head, i)->number, unit_index(head, j)->number);
}
while(i < j && strcmp(unit_index(head, i)->number, num) <= 0)
{
i++;
}
if(i < j)
{
strcpy(unit_index(head, j)->name, unit_index(head, i)->name);
strcpy(unit_index(head, j)->number, unit_index(head, i)->number);
}
strcpy(unit_index(head, i)->number, num);
strcpy(unit_index(head, i)->name, name);
rank_number(head, l, i - 1);
rank_number(head, i + 1, r);
}
}
address unit_index(address head, int index)
{
int i;
address temp = head;
for(i = 0; i < index; i++)
{
temp = temp->next;
}
return(temp);
}
void rank_name(address head)
{
int i, j;
int len = length(head);
char *num = (char*)malloc(sizeof(char)*20);
char *nam = (char*)malloc(sizeof(char)*20);
address temp = head;
for(i = 0; i < len - 1; i++)
{
head = temp;
for(j = i; j < len; j++)
{
if(strcmp(temp->next->name, head->next->name) > 0)
{
strcpy(num, head->next->number);
strcpy(head->next->number, temp->next->number);
strcpy(temp->next->number, num);
strcpy(nam, head->next->name);
strcpy(head->next->name, temp->next->name);
strcpy(temp->next->name, nam);
}
head = head->next;
}
temp = temp->next;
}
}
void update_name(address head, ch *name, char *name1, char *number)
{
int i;
int len = length(head);
for(i = 0; i < len; i++)
{
if(strcmp(head->next->name, name) == 0)
{
strcpy(head->next->name, name1);
strcpy(head->next->number, number);
}
head = head->next;
}
}
bo update_id(address head, el id, char *name1, char *number)
{
int i;
if(id <= 0 || id > length(head))
{
return F;
}
for(i = 0; i < id-1; i++)
{
head = head->next;
}
strcpy(head->next->name, name1);
strcpy(head->next->number, number);
return T;
}
void update_number(address head, ch *number, char *name1, char *number1)
{
int i;
int len = length(head);
for(i = 0; i < len; i++)
{
if(strcmp(head->next->number, number) == 0)
{
strcpy(head->next->name, name1);
strcpy(head->next->number, number1);
}
head = head->next;
}
}
bo query_id(address head, el id)
{
int i;
if(id <= 0 || id > length(head))
{
return F;
}
for(i = 0; i < id - 1; i++)
{
head = head->next;
}
printf("%d. %s %s\n", head->next->id, head->next->name, head->next->number);
return T;
}
void query_name(address head, ch* name)
{
int i;
int len = length(head);
for(i = 0; i < len; i++)
{
if(strcmp(head->next->name, name) == 0)
{
printf("%d. %s %s\n", head->next->id, head->next->name, head->next->number);
}
head = head->next;
}
}
void query_number(address head, ch* number)
{
int i;
int len = length(head);
for(i = 0; i < len; i++)
{
if(strcmp(head->next->number, number) == 0)
{
printf("%d. %s %s\n", head->next->id, head->next->name, head->next->number);
}
head = head->next;
}
}
bo delete_id(address head, int id)
{
int i;
address temp1 = head;
if(id <= 0 || id > length(head))
{
return F;
}
for(i = 0; i < id - 1; i++)
{
head = head->next;
}
address temp = head->next->next;
free(head->next);
head->next = temp;
temp->prior = head;
rank_id(temp1);
return T;
}
bo delete_name(address head, ch *name)
{
int i;
address temp = head;
while(temp->next != head)
{
if(strcmp(temp->next->name, name) == 0)
{
address sen = temp->next->next;
free(temp->next);
temp->next = sen;
sen->prior = temp;
}
else
{
temp = temp->next;
}
}
rank_id(head);
}
bo delete_number(address head, ch *number)
{
int i;
address temp = head;
while(temp->next != head)
{
if(strcmp(temp->next->number, number) == 0)
{
address sen = temp->next->next;
free(temp->next);
temp->next = sen;
sen->prior = temp;
}
else
{
temp = temp->next;
}
}
rank_id(head);
}
bo length(address head)
{
int count = 0;
address temp = head;
while(temp->next != head)
{
count++;
temp = temp->next;
}
return count;
}
bo init(address* head)
{
address newnode = (address)malloc(sizeof(struct Address));
if(NULL == newnode)
{
return F;
}
newnode->next = newnode;
newnode->prior = newnode;
*head = newnode;
}
bo insert_tail(address head, ch *name, ch *number)
{
static int id = 1;
address temp = head;
address newnode = (address)malloc(sizeof(struct Address));
if(NULL == newnode)
{
return F;
}
strcpy(newnode->name, name);
strcpy(newnode->number, number);
newnode->id = id;
id++;
head->prior->next = newnode;
newnode->next = head;
newnode->prior = head->prior;
head->prior = newnode;
rank_id(head);
return T;
}
void rank_id(address head)
{
int i;
int count = 1;
address temp = head;
for(i = 0; i < length(head); i++)
{
temp->next->id = count;
count++;
temp = temp->next;
}
}
void print(address head)
{
address temp = head;
while(temp->next != head)
{
printf("%d. %s %s\n", temp->next->id, temp->next->name, temp->next->number);
temp = temp->next;
}
}