这里我做了一个以双向循环链表为主体的通讯录程序,主要功能有初始化链表,以尾插的形式插入链表,以id,name,num为查询条件删除链表,以id,name,num为查询条件查询数据,以id,name,num为查询条件修改数据,用冒泡排序,以name为条件排序,用快速排序,以num为条件排序。
在主函数中,我利用输入值的不同,用switch语句设计了页面。
但是我的快速排序还有很多问题,希望各位大佬能够帮忙指出问题。
#include <stdio.h>
#include <stdlib.h>#include <string.h>
#define T 1
#define F -1
typedef char Type;
struct Address
{
struct Address* next;
struct Address* prior;
int id;
Type name[20];
Type num[12];
};
typedef struct Address* address;
int init(address *head);
int length(address head);
int insert_tail(address head, Type *name, Type *num);
int delete_num(address head, Type *num);
int delete_name(address head, Type *name);
int delete_id(address head, int id);
int update_id(address head, int id, Type *name, Type *num);
int update_num(address head, Type *name, Type *old_num, Type *new_num);
int update_name(address head, Type *old_name, Type *new_name, Type *num);
int query_id(address head, int id);
int query_num(address head, Type *num);
int query_name(address head, Type *name);
void rank_name(address head);
void rank_num(address head, int begin, int end, int sum);
void print(address head);
int main()
{
int i;
int sum;
int ret;
Type name[20];
Type num[12];
Type new_name[20];
Type new_num[12];
Type old_name[20];
Type old_num[12];
address head = NULL;
ret = init(&head);
if (NULL == head)
{
return F;
}
printf("/****************************insert, delete, update, query, rank********************************/\n");
int control = 1;
int delete;
int update;
int query;
int rank;
while (control != 0)
{
printf("if you want to end scanf 0, insert scanf 1, delete scanf 2, query scanf 3, update scanf 4, rank scanf 5\n");
printf("please scanf : ");
scanf("%d", &control);
switch (control)
{
case 1 : printf("you want to insert how many telephone number : ");
scanf("%d", &sum);
for (i = 0; i < sum; i++)
{
printf("[%d] the name, number : ", i + 1);
scanf("%s %s", name, num);
insert_tail(head, name, num);
}
print(head);
break;
case 2 : printf("you want to choose delete by id 1, num, 2, name 3 : ");
scanf("%d", &delete);
switch (delete)
{
case 1 : printf("delete by id, please choose the id(1 ~ sum) : ");
int id;
scanf("%d", &id);
delete_id(head, id);
print(head);
break;
case 2 : printf("delete by num, please choose the num : ");
scanf("%s", num);
delete_num(head, num);
print(head);
break;
case 3 : printf("delete by name, please choose the name : ");
scanf("%s", name);
delete_name(head, name);
print(head);
break;
default : printf("no !\n");
break;
}
break;
case 3 : printf("you want to choose query by id 1, num, 2, name 3 : ");
scanf("%d", &query);
switch (query)
{
case 1 : printf("query by id, please choose the id(1 ~ %d) : ", sum);
int id;
scanf("%d", &id);
query_id(head, id);
break;
case 2 : printf("query by num, please choose the num : ");
scanf("%s", num);
query_num(head, num);
break;
case 3 : printf("query by name, please choose the name : ");
scanf("%s", name);
query_name(head, name);
break;
default : printf("no !\n");
break;
}
break;
case 4 : printf("you want to choose update by id 1, num, 2, name 3 : ");
scanf("%d", &update);
switch (update)
{
case 1 : printf("update by id, please choose the id(1 ~ sum), name, num : ");
int id;
scanf("%d %s %s", &id, name, num);
update_id(head, id, name, num);
print(head);
break;
case 2 : printf("update by num, please choose the name, the old_num, the new_num : ");
scanf("%s %s %s", name, old_num, new_num);
update_num(head, name, old_num, new_num);
print(head);
break;
case 3 : printf("update by name, please choose the old_name, the new_name, the num : ");
scanf("%s %s %s", old_name, new_name, num);
update_name(head, old_name, new_name, num);
print(head);
break;
default : printf("no !\n");
break;
}
break;
case 5 : printf("you want to choose rank by num 1, name 2 : ");
scanf("%d", &rank);
switch (rank)
{
case 1 : printf("rank by num");
rank_num(head, 0, length(head) - 1, sum);
print(head);
break;
case 2 : printf("rank by name");
rank_name(head);
print(head);
break;
default : printf("no !\n");
break;
}
break;
default : printf("no !\n");
break;
}}
printf("************************************END****************************************************\n");
return 0;
}
int init(address *head)
{
address newnode = (address)malloc(sizeof(struct Address));
if (NULL == newnode)
{
return F;
}
newnode->prior = newnode;
newnode->next = newnode;
*head = newnode;
return T;
}
void print(address head)
{
int i = 1;
address temp = head;
printf("output :\n");
while (temp->next != head)
{
printf("[%d] :\n", i++);
printf("\tid = %d\t", temp->next->id);
printf("\tname = %s\t", temp->next->name);
printf("\tnum = %s\n", temp->next->num);
temp = temp->next;
}
}
int insert_tail(address head, Type* name, Type* num)
{
address newnode = (address)malloc(sizeof(struct Address));
if (NULL == newnode)
{
return F;
}
static int id = 1;
strcpy(newnode->name, name);
strcpy(newnode->num, num);
newnode->id = id;
id++;
newnode->next = head->prior->next;
head->prior->next = newnode;
newnode->prior = head->prior;
head->prior = newnode;
return T;
}
void rank_name(address head)
{
int i;
int j;
int len = length(head);
address temp = head;
char *num = (char *)malloc(sizeof(char) * 20);
char *name = (char *)malloc(sizeof(char) * 20);
for (i = 0; i < len - 1; i++)
{
head = temp;
for (j = i; j < len; j++)
{
if (0 < strcmp(temp->next->name, head->next->name))
{
strcpy(name, head->next->name);
strcpy(head->next->name, temp->next->name);
strcpy(temp->next->name, name);
strcpy(num, head->next->num);
strcpy(head->next->num, temp->next->num);
strcpy(temp->next->num, num);
}
head = head->next;
}
temp = temp->next;
}
}
void rank_num(address head, int begin, int end, int sum)
{
int index;
address temp1 = head;
char *name[sum];
char *num[sum];
for (index = 0; index < length(head); index++)
{
name[index] = head->next->name;
num[index] = head->next->num;
head = head->next;
}
if (begin < end)
{
int i = begin + 1;
int j = end;
char *temp;
while (i < j)
{
if (strcmp(num[i], num[begin]) > 0)
{
temp = num[i];
num[i] = num[j];
num[i] = temp;
temp = name[i];
name[i] = name[j];
name[j] = temp;
j--;
printf("%s %s\n", num[i], num[j]);
}
else
{
i++;
}
}
if (strcmp(num[i], num[begin]) >= 0)
{
i--;
}
temp = num[i];
num[i] = num[j];
num[i] = temp;
temp = name[i];
name[i] = name[j];
name[j] = temp;
for (index = 0; index < length(head); index++)
{
strcpy(temp1->next->name, name[index]);
strcpy(temp1->next->num, num[index]);
temp1 = temp1->next;
}
rank_num(temp1, begin, i, sum);
rank_num(temp1, j, end, sum);
}
}
int length(address head)
{
int cont = 0;
address temp = head;
while (temp->next != head)
{
temp = temp->next;
cont++;
}
return cont;
}
int delete_num(address head, Type *num)
{
int i;
int len = length(head);
printf("%d\n", len);
for (i = 0; i < len; i++)
{
if (strcmp(head->next->num, num) == 0)
{
address temp = head->next->next;
free(head->next);
head->next = temp;
temp->prior = head;
}
else
{
head = head->next;
}
}
return T;
}
int delete_name(address head, Type *name)
{
int i;
int len = length(head);
printf("%d\n", len);
for (i = 0; i < len; i++)
{
if (strcmp(head->next->name, name) == 0)
{
address temp = head->next->next;
free(head->next);
head->next = temp;
temp->prior = head;
}
else
{
head = head->next;
}
}
return T;
}
int delete_id(address head, int id)
{
int i;
if (id < 0 || id > length(head))
{
printf("out of range!\n");
return F;
}
for (i = 1; i < id; i++)
{
head = head->next;
}
address temp = head->next->next;
free(head->next);
head->next = temp;
temp->prior = head;
return T;
}
int update_id(address head, int id, Type *name, Type *num)
{
int i;
if (id < 0 || id > length(head))
{
printf("out of range!\n");
return F;
}
for (i = 1; i < id; i++)
{
head = head->next;
}
strcpy(head->next->name, name);
strcpy(head->next->num, num);
return T;
}
int update_num(address head, Type *name, Type *old_num, Type *new_num)
{
int i;
int len = length(head);
for (i = 0; i < len; i++)
{
if (0 == strcmp(head->next->num, old_num))
{
strcpy(head->next->num, new_num);
strcpy(head->next->name, name);
}
head = head->next;
}
return T;
}
int update_name(address head, Type *old_name, Type *new_name, Type *num)
{
int i;
int len = length(head);
for (i = 0; i < len; i++)
{
if (0 == strcmp(head->next->name, old_name))
{
strcpy(head->next->name, new_name);
strcpy(head->next->num, num);
}
head = head->next;
}
return T;
}
int query_id(address head, int id)
{
int i;
if (id < 0 || id > length(head))
{
printf("out of range!\n");
return F;
}
for (i = 1; i < id; i++)
{
head = head->next;
}
printf("\tid = %d\n", id);
printf("\tname = %s\n", head->next->name);
printf("\tnum = %s\n", head->next->num);
}
int query_num(address head, Type *num)
{
printf("in\n");
int i;
int len = length(head);
for (i = 0; i < len; i++)
{
if (0 == strcmp(head->next->num, num))
{
printf("\tid = %d\n", head->next->id);
printf("\tname = %s\n", head->next->name);
printf("\tnum = %s\n", head->next->num);
}
head = head->next;
}
return T;
}
int query_name(address head, Type *name)
{
int i;
int len = length(head);
for (i = 0; i < len; i++)
{
if (0 == strcmp(head->next->name, name))
{
printf("\tid = %d\n", head->next->id);
printf("\tname = %s\n", head->next->name);
printf("\tnum = %s\n", head->next->num);
}
head = head->next;
}
return T;
}