通讯录

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

/*typedef struct {
	char country[20];
	char province[20];
	char city[20];
	char district[20];
}address;*/

typedef struct{
	char num[11];
    char name[30];
    char sex[4];
    char phone[13];
    char address[20];
    int priority;
}data;

typedef struct node{
	data D;
	struct node *next;
}node,*Linklist;

Linklist CreateList()
{
	node *head=(node *)malloc(sizeof(node));
	if(head==NULL)
	{
		printf("Fail to allocate the memory\n");
		exit(-1);
	}
	head->next=NULL;
	return head;
}

node *input_all_infor(node *n)
{
	printf("Please input the contactor's name\n");
	gets(n->D.name);
	printf("Please input the contactor's sex\n");
	gets(n->D.sex);
	printf("Please input the contactor's phone number\n");
	gets(n->D.phone);
	printf("Please input the contactor's student number\n");
	gets(n->D.num);
	printf("Please input the contactor's address\n");
	gets(n->D.address);
	printf("Please input the contactor's priority\n");
	scanf("%d",&n->D.priority);
	getchar();
	return n;
}

void node_insert(Linklist head,node *n)
{
    if(head->next==NULL)
        head->next=n;
    else
    {
        node *p=head,*q=head->next;
        while(q!=NULL && n->D.priority > q->D.priority)
        {
        	p=q;
        	q=q->next;
		}
        n->next=p->next;
        p->next=n;
    }
}

void Add_contactor(Linklist head)
{
    node *n=(node *)malloc(sizeof(node));
    if(!n)
        exit(-1);
    n->next=NULL;
    n=input_all_infor(n);
    node_insert(head,n);
}

void display_node(node *a)
{
	printf("\nname:%s\n",a->D.name);
	printf("num:%s\n",a->D.num);
	printf("address:%s\n",a->D.address);
	printf("phone:%s\n",a->D.phone);
	printf("sex:%s\n",a->D.sex);
	printf("priority:%d\n",a->D.priority);
 } 

node* Modify_Search_contactor(Linklist head)
{
	int cnt=0;
	char c,choice;
	printf("Please select the way you want to search by :1.name 2.student number\n");
	c=getch();
	if(c=='1')
	{
		char name[30];
		printf("Please input the student's name\n");
		gets(name);
		node *p=head->next;
		while(p!=NULL)
		{
			if(!strcmp(name,p->D.name))
			{
				display_node(p);
				printf("Do you want to modify that node? y/n\n");
				choice=getch();
				if(choice=='y')
					break;
			}
			p=p->next;
		}
		return p;
	}
	else if(c=='2')
	{
		char num[11];
		printf("Please input the student's stu_num\n");
		gets(num);
		node *p=head->next;
		while(p!=NULL)
		{
			if(!strcmp(num,p->D.num))
			{
				display_node(p);
				printf("Do you want to modify that node? y/n\n");
				choice=getch();
				if(choice=='y')
					break;
			}
			p=p->next;
		}
		return p;
	}
	else
	{
		printf("Wrong input.Please input again\n");
		return NULL;
	}
}

void Search_contactor(Linklist head)
{
	int cnt=0;
	char c,choice;
	printf("Please select the way you want to search by :1.name 2.student number\n");
	c=getch();
	if(c=='1')
	{
		char name[30];
		printf("Please input the student's name\n");
		gets(name);
		node *p=head->next;
		while(p!=NULL)
		{
			if(!strcmp(name,p->D.name))
			{
				display_node(p);
				printf("\n");
			}
			p=p->next;
		}
	}
	else if(c=='2')
	{
		char num[11];
		printf("Please input the student's stu_num\n");
		gets(num);
		node *p=head->next;
		while(p!=NULL)
		{
			if(!strcmp(num,p->D.num))
			{
				display_node(p);
				printf("\n");
			}
			p=p->next;
		}
	}
	else
	{
		printf("Wrong input.Please input again\n");
	}
}

void modify_node(node *p)
{
	char choice;
	printf("If you want to modify the following information,please enter 'y' after that item's display,else please input 'n'\n");
	printf("%s\n",p->D.name);
	if(choice=='y')
		gets(p->D.name);
	printf("%s\n",p->D.sex);
	if(choice=='y')
		gets(p->D.sex);
	printf("%s\n",p->D.num);
	
	printf("address:%s\n",p->D.address);
	choice=getch();
	if(choice=='y')
		gets(p->D.address);
	printf("phone:%s\n",p->D.phone);
	choice=getch();
	if(choice=='y')
		gets(p->D.phone);
	printf("priority:%d\n",p->D.priority);
	choice=getch();
	if(choice=='y')
	{
		scanf("%d",&p->D.priority);
		getchar();
	}
}

void delete_node(Linklist head,node *p)
{
	node *q=head;
	while(q->next!=p)
		q=q->next;
	q->next=p->next;
	free(p);
}

void Exit(Linklist head)
{
	node *p=head->next;
	while(p!=NULL)
	{
		node *q=p->next;
		free(p);
		p=q;
	}
	free(head);
}

void display_linklist(Linklist head)
{
	node *p=head->next;
	while(p)
	{
		display_node(p);
		printf("\n");
		p=p->next;
	}
}

int main()
{
	node *head=CreateList();
	node *p;
	char select;
	while(1)
	{
		printf(" Please input your choice\n");
		printf(" 1.Add  a contactor\n 2.Search for a contactor\n 3.Modify a contactor's information\n 4.Delete a contactor's information\n 6.Output all contactors' informaTtion\n 5.Display all the contactors\n 6.Exit the system\n");
		select=getch();
		if(select<'1' || select>'5')
		{
			system("cls");
			printf("Wrong input,please input the selection again\n");
		}
		switch(select)
		{
			case '1':
				system("cls");
                Add_contactor(head);
				break;
			case '2':
				system("cls");
				Search_contactor(head);
				break;
			case '3':
				system("cls");
				if(head->next==NULL)
					printf("The linklist is  empty\n");
				else
				{
					p=Modify_Search_contactor(head);
					if(p==NULL)
						printf("No such student and failed to modify the student's information\n");
					else
						modify_node(p);
				}
				break;
			case '4':
				system("cls");
				if(head->next==NULL)
					printf("The linklist is empty\n");
				else
				{
					p=Modify_Search_contactor(head);
					if(p==NULL)
						printf("There is no such node\n");
					else
						delete_node(head,p);
				}
				break;
			case '5':
				system("cls");
				if(head->next==NULL)
					printf("The linklist is empty\n");
				else
					display_linklist(head);
				break;
			case '6':
				system("cls");
				Exit(head);
				return 0;
				break;
		}
	}
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值