基本功能已实现,排序功能暂未完善 ,仍有问题
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 50
#define M 50
#define SIZE 3
#define FAILURE 10000
#define SUCCESS 10001
#define FALSE 10002
#define TRUE 10003
typedef int ElemType;
struct student
{
int id;
char name[N];
char tel[N];
struct student *next;
};
typedef struct student Node;
int person = 0;
void welcome()
{
system("clear");
printf("\n\n\n\n*******************************************************\n");
printf("********************欢迎进入通讯录系统**********************\n");
printf("************************************************************\n");
}
void menu()
{
printf("************************************************************\n");
printf("*******************1.添加新用户 **********************\n");
printf("*******************2.删除用户 **********************\n");
printf("*******************3.查看所有用户信息 **********************\n");
printf("*******************4.查找用户 **********************\n");
printf("*******************5.修改用户信息 **********************\n");
// printf("*******************6.排序 **********************\n");
printf("*******************7.退出***********************************\n");
printf("************************************************************\n");
printf("请输入选择:\n");
}
int Linkinit(Node **l)
{
*l = (Node *)malloc(sizeof(Node)*1);
if(NULL == *l)
{
return FAILURE;
}
(*l)->next = NULL;
return SUCCESS;
}
int adduser(Node *l, Node *q)
{
int k = 1;
Node *p = l;
/*if(NULL == l)
{
return FAILURE;
}
if(p->next == NULL)
{
p->next = q;
q->next = NULL;
return SUCCESS;
}
q->next = p->next;
p->next = q;
return SUCCESS;*/
if(p->next == NULL)
{
p->next = q;
q->next = NULL;
return SUCCESS;
}
while(p->next != NULL)
{
p = p->next;
}
p->next = q;
q->next = NULL;
return SUCCESS;
}
int ShowAll(Node *l)
{
if(NULL == l)
{
return FAILURE;
}
Node *q = l;
while(q->next)
{
q = q->next;
printf("name:%s id:%d tel:%s\n", q->name, q->id, q->tel);
}
return SUCCESS;
}
int search(Node *l)
{
char name[10] = {0};
scanf("%s",name);
if(NULL == l)
{
return FAILURE;
}
Node *p = l;
while(p != NULL)
{
if(strcmp(p->name,name) == 0)
{
printf("name:%s id:%d tel:%s\n", p->name, p->id, p->tel);
return SUCCESS;
}
p = p->next;
}
return FAILURE;
}
int deleteuser(Node *first)
{
if(NULL == first)
{
return FAILURE;
}
Node *tmp;
char name[10] = {0};
scanf("%s",name);
Node *n = first;
while(n != NULL)
{
if(strcmp(n->next->name, name) == 0)
{
tmp = n->next;
n->next = n->next->next;
free(tmp);
return SUCCESS;
}
n = n->next;
}
return FAILURE;
}
int modify(Node *head,ElemType e)
{
if(head == NULL)
{
return FAILURE;
}
Node *p = head;
Node *m = head->next;
while(m->id != e && m != NULL)
{
m = m->next;
}
if(m->id == e)
{
scanf("%s %d %s", m->name, &(m->id), m->tel);
return SUCCESS;
}
return FAILURE;
}
/*int sort(Node *first)
{
if(head == NULL)
{
return FAILURE;
}
int len = 0;
Node *p = first->next;
while(p != NULL)
{
len++;
p = p->next;
}
//p = first->next;
int i, j;
for(i = 0; i < len - 1; i++)
{
for(j = 0; j < len - i -1; j++)
{
if(strcmp(p->name, p->next->name)>0)
{
p->next = p->next->next;
p->next->next = p ;
}
p = p->next;
}
}
return SUCCESS;
}*/
int main()
{
Node *first = NULL;
int ret, i;
Linkinit(&first);
int choice;
welcome();
while(1)
{
menu();
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("please input name id tel:\n");
Node *q = (Node *)malloc(sizeof(Node)*1);
scanf("%s %d %s",q->name, &(q->id), q->tel);
ret = adduser(first, q);
if(ret == FAILURE)
{
printf("Insert Failure!\n");
}
else
{
printf("Insert Success\n");
}
break;
case 2:
printf("please input name you want to delete:\n");
ret = deleteuser(first);
if(ret == FAILURE)
{
printf("\nDelete Failure!\n");
}
else
{
printf("\nDelete Success!\n");
}
break;
case 3:
ret = ShowAll(first);
if(ret == FAILURE)
{
printf("\nTraverse Failure!\n");
}
else
{
printf("\nTraverse Success!\n");
}
break;
case 4:
printf("please input name you want to find:\n");
ret = search(first);
if(ret == FAILURE)
{
printf("\nSearch Failure\n");
}
else
{
printf("\nSearch Success\n");
}
break;
case 5:
printf("please input modify id:\n");
int num;
scanf("%d",&num);
ret = modify(first,num);
if(ret == FAILURE)
{
printf("\nModify Failure\n");
}
else
{
printf("\nModify Success\n");
}
break;
/*case 6:
ret = sort(first);
if(ret == FAILURE)
{
printf("\nsort Failure\n");
}
else
{
printf("\nsort Success\n");
}
break;*/
case 7:
exit(0);
break;
default:
printf("input error");
}
}
return 0;
}