通讯录完全版(增删改查排序)

/*************************************************************************
    > File Name: Address_list.c
    > Author: ma6174
    > Mail: ma6174@163.com 
    > Created Time: 2017年05月23日 星期二 11时19分25秒
 ************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define F -1
#define T 1

typedef struct Address addr;
typedef struct Node* node;
typedef int type;

struct Address
{
	type id;
	char num[15];
	char name[15];
	char address[100];
};

struct Node//单链表
{
	struct Node *next;
	addr data;
};

int init(node*);//初始化
int interface(void);//功能列表
type insert_tail(node);//从尾部插入
int insert_id(node, int*);//由id插入
int delete_id(node, int*, int);//根据id删除
int delete_name(node, int*);//根据name删除
int delete_num(node, int*);//根据num删除
int edit_id(node, int);//根据id改
int edit_name(node);//根据name改
int edit_num(node);//根据num改
int search_id(node, int);
int search_num(node);
int search_name(node);//查找根据id or name or num
int sort_name(node);
int sort_num(node);//根据name or num 排序
void print(node head);//打印
void swap(node, node);//交换,用于排序

int main()
{
	node head;
	int ret = 0;
	int n = 0;
	int n1 = 0;
	int d = 0;
	int e = 0;
	int i = 0;
	init(&head);

	for(;ret != 8;)
	{
		ret = interface();

		switch(ret)
		{
			case 1:
				printf("how many contacts you want to put in?\n");
				scanf("%d", &n);
				n1 = n;
				for(;n1 > 0; n1--)
				{
					insert_tail(head);
				}
				break;
			case 2:
				insert_id(head, &n);
				printf("%d", n);
				break;
			case 3:
				printf("You want to delete by 1.id or 2.name or 3.num\n");
				scanf("%d", &d);
				if(1 == d)
				{
					printf("which id you want to delete?\n");
					scanf("%d", &i);
					if(i > n || i < 0)
					{
						printf("wrong id!\n");
					}
					else
					{
						delete_id(head, &n, i);
					}
				}
				else if(2 == d)
				{
					delete_name(head, &n);
				}				
				else if(3 == d)
				{
					delete_num(head, &n);
				}
				else 
				{
					printf("Warning:wrong function!\n");
				}
				break;
			case 4:
				printf("You want to edit by 1.id or 2.name or 3.num\n");
				scanf("%d", &e);
				if(1 == e)
				{
					printf("which id you want to edit?\n");
					scanf("%d", &i);
					if(i > n || i < 0)
					{
						printf("wrong id!\n");
					}
					else
					{
						edit_id(head, i);
					}
				}
				else if(2 == e)
				{
					edit_name(head);
				}				
				else if(3 == e)
				{
					edit_num(head);
				}
				else 
				{
					printf("Warning:wrong function!\n");
				}
				break;
			case 5:
				printf("You want to search by 1.id or 2.name or 3.num\n");
				scanf("%d", &e);
				if(1 == e)
				{
					search_id(head, n);
				}
				else if(2 == e)
				{
					search_name(head);
				}				
				else if(3 == e)
				{
					search_num(head);
				}
				else 
				{
					printf("Warning:wrong function!\n");
				}
				break;
			case 6:
				printf("You want to sort by 1.name or 2.num\n");
				scanf("%d", &e);
				if(1 == e)
				{
					sort_name(head);
				}
				else if(2 == e)
				{
					sort_num(head);
				}
				else 
				{
					printf("Warning:wrong function!\n");
				}
				break;				
			case 7:
				print(head);
				break;
			case 8:
				break;
			default:
				printf("Warning:wrong function!\n");
				break;
		}
	}
	return 0;
}

int init(node* head)
{
	node newnode = (node)malloc(sizeof(struct Node));
	if(newnode == NULL)
	{
		return F;
	}
	newnode->next = NULL;
	newnode->data.id = 0;
	strcpy(newnode->data.name, "name") ;
	strcpy(newnode->data.num, "number") ;
	strcpy(newnode->data.address, "address");

	*head = newnode;
	return T;
}

int interface(void)
{
	int flag = F;

	printf("Welcome To Use Address_list System\n");
	printf("1.Add contacts in batch\n");
	printf("2.Add a contact\n");
	printf("3.Delete a contact\n");
	printf("4.Edit a contact\n");
	printf("5.Find a contact\n");
	printf("6.Sort\n");
	printf("7.Print Address_list\n");
	printf("8.QUIT\n");

	scanf("%d", &flag);

	return flag;
}

type insert_tail(node head)
{
	int i = 1;
	char s1[15];
	char s2[15];
	char s3[100];

	node newnode = (node)malloc(sizeof(struct Node));
	if(newnode == NULL)
	{
		return F;
	}
	newnode->next = NULL;
	while(head->next != NULL)
	{
		head = head->next;
		i++;
	}
	head->next = newnode;

	newnode->data.id = i;
	printf("id NO.%d\n", i);
	printf("please input name\n");
	scanf("%s", s1);
	printf("please input num\n");
	scanf("%s", s2);
	printf("please input address\n");
	scanf("%s", s3);
	strcpy(newnode->data.name, s1);
	strcpy(newnode->data.num, s2);
	strcpy(newnode->data.address, s3);
	return T;
}

int delete_id(node head, int* n, int i)
{
	node t = NULL;

	while(head->next->data.id != i)
	{
		head = head->next;
	}
	
	t = head->next->next;
	free(head->next);
	head->next = t;
	
	while(head->next != NULL)
	{
		head = head->next;
		head->data.id--;
	}
	
	--*n;
	return T;
}

int delete_name(node head, int* n)
{
	char s[15];

	printf("who you want to delete?\n");
	scanf("%s", s);
	while(head->next != NULL)
	{
		if(strcmp(head->next->data.name, s) == 0)
		{
			delete_id(head, n, head->next->data.id);
		}
		else
		{
			head = head->next;
		}
	}
	
	return T;
}

int delete_num(node head, int* n)
{
	char s[15];
	
	printf("which num you want to delete?\n");
	scanf("%s", s);
	while(head->next != NULL)
	{
		if(strcmp(head->next->data.num, s) == 0)
		{
			delete_id(head, n, head->next->data.id);
		}
		else
		{
			head = head->next;
		}
	}
	
	return T;
}

int edit_id(node head, int i)
{
	char sname[15];
	char snum[15];
	char saddr[15];

	while(head->data.id != i)
	{
		head = head->next;
	}
	
	printf("input new name\n");
	scanf("%s", sname);
	printf("input new num\n");
	scanf("%s", snum);
	printf("input new address\n");
	scanf("%s", saddr);
	strcpy(head->data.name, sname);
	strcpy(head->data.num, snum);
	strcpy(head->data.address, saddr);
	return T;
}

int edit_name(node head)
{
	char s[15];
	int c = 0;

	printf("who you want to edit?\n");
	scanf("%s", s);
	while(head->next != NULL)
	{
		if(strcmp(head->next->data.name, s) == 0)
		{
			c++;
			edit_id(head, head->next->data.id);
		}
		else
		{
			head = head->next;
		}
	}
	
	if(c == 0)
	{
		printf("Name not found!\n\n");
	}
	
	return T;
}

int edit_num(node head)
{
	char s[15];
	int c = 0;
	
	printf("which num you want to edit?\n");
	scanf("%s", s);
	while(head->next != NULL)
	{
		if(strcmp(head->next->data.num, s) == 0)
		{
			c++;
			edit_id(head, head->next->data.id);
		}
		else
		{
			head = head->next;
		}
	}
	
	if(c == 0)
	{
		printf("Num not found!\n\n");
	}
	return T;	
}

int search_id(node head, int n)
{
	int i = 0;
	
	printf("input the id you want to search\n");
	scanf("%d", &i);
	if(i < 1 || i > n)
	{
		printf("Wrong id!\n");
		return F;
	}
	while(head->next != NULL)
	{
		if(head->data.id == i)
		{
			printf("\n");
			printf("%d %15s %15s %15s\n\n", head->data.id, head->data.name, head->data.num, head->data.address);
		}
		head = head->next;
	}

	if(head->next == NULL)
	{
		printf("\n");
		printf("%d %15s %15s %15s\n\n", head->data.id, head->data.name, head->data.num, head->data.address);
	}
	return T;
}

int search_num(node head)
{
	char i[15];
	int c = 0;
	printf("input the num you want to search\n");
	scanf("%s", i);
	
	while(head->next != NULL)
	{
		if(strcmp(head->data.num, i) == 0)
		{
			c++;
			printf("\n");
			printf("%d %15s %15s %15s\n\n", head->data.id, head->data.name, head->data.num, head->data.address);
		}
		head = head->next;
	}
	
	if(c == 0)
	{
		printf("num not found!\n\n");
	}
	return T;	
}

int search_name(node head)
{
	char i[15];
	int c = 0;
	printf("input the name you want to search\n");
	scanf("%s", i);
	
	while(head->next != NULL)
	{
		if(strcmp(head->data.name, i) == 0)
		{
			c++;
			printf("\n");
			printf("%d %15s %15s %15s\n\n", head->data.id, head->data.name, head->data.num, head->data.address);
		}
		head = head->next;
	}
	
	if(c == 0)
	{
		printf("name not found!\n\n");
	}
	return T;	
}

int sort_name(node head)
{
	head = head->next;
	node f = head;
	node t = head;
	while(head->next != NULL)
	{
		t = head->next;
		while(t->next != NULL)
		{
			if(strcmp(head->data.name, t->data.name) == 1)
			{
				swap(head, t);
			}
			t = t->next;
		}
	    while(f->next != NULL)
		{
			if(strcmp(t->data.name, f->data.name) == -1)
			{
				swap(t, f);
				break;	
			}
			f = f->next;
		}
		f = head;
		head = head->next;
	}
}

int sort_num(node head)
{
	head = head->next;
	node f = head;
	node t = head;
	while(head->next != NULL)
	{
		t = head->next;
		while(t->next != NULL)
		{
			if(strcmp(head->data.num, t->data.num) == 1)
			{
				swap(head, t);
			}
			t = t->next;
		}
	    while(f->next != NULL)
		{
			if(strcmp(t->data.num, f->data.num) == -1)
			{
				swap(t, f);
				break;	
			}
			f = f->next;
		}
		f = head;
		head = head->next;
	}
}

void print(node head)
{
	while(head->next != NULL)
	{
    	printf("%d %15s %15s %15s\n", head->data.id, head->data.name, head->data.num, head->data.address);
		head = head->next;
	}
    printf("%d %15s %15s %15s\n", head->data.id, head->data.name, head->data.num, head->data.address);
	printf("\n\n\n");
}

int insert_id(node head, int* n)
{
	
	int i = 1;
	char s1[15];
	char s2[15];
	char s3[100];
	int f = 0;

	if(0 == *n)
	{
		printf("you can't add contact to a empty Address_list\n");
		printf("please choose 1\n");
		printf("\n");
		printf("\n");
		return F;
	}
	node newnode = (node)malloc(sizeof(struct Node));
	if(newnode == NULL)
	{
		return F;
	}
	
	for(;f == 0;)
	{
		printf("The id of this contact\n");
		scanf("%d", &i);
		if(i > *n + 1|| i < 1)
		{
			
			printf("illegal id!\n");
		}
		else
		{
			newnode->data.id = i;
			printf("please input name\n");
			scanf("%s", s1);
			printf("please input num\n");
			scanf("%s", s2);
			printf("please input address\n");
			scanf("%s", s3);
			strcpy(newnode->data.name, s1);
			strcpy(newnode->data.num, s2);
			strcpy(newnode->data.address, s3);
			f = 1;
			++*n;
		}
	}
	for(f = 1; f < i; f++)
	{
		head = head->next;
	}
	
	newnode->next = head->next;
	head->next = newnode;
	printf("%d\n", head->data.id);
	head = newnode;
	while(head->next != NULL)
	{
		head = head->next;
		head->data.id++;
	}
	return T;
}

void swap(node head,node t)
{
	char tname[15];
	char tnum[15];
	char taddr[15];
	strcpy(tnum, head->data.num);
	strcpy(head->data.num, t->data.num);
	strcpy(t->data.num, tnum);
	
	strcpy(tname, head->data.name);
	strcpy(head->data.name, t->data.name);
	strcpy(t->data.name, tname);
	
	strcpy(taddr, head->data.address);
	strcpy(head->data.address, t->data.address);
	strcpy(t->data.address, taddr);
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值