链表之通讯录

来源:互联网 发布:大富豪源码论坛 编辑:程序博客网 时间:2024/05/21 22:25

 今天把链表复习完了,写了一下通讯录这个小程序。代码如下:


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


#define ok 0
#define error -1
#define malloc_error    -2
#define N             20
typedef struct node
{
char id;
char name[N];
char tel[N];
char address[N];
struct node *next;
}Node;
typedef Node* Pnode;
int i = 1;
void show()
{
printf("\n\n\n\n");
printf("\t\ta:insert\n");
printf("\t\tb:delete\n");
printf("\t\tc:alter\n");
printf("\t\td:search\n");
}
//添加联系人
int Insert_list(Pnode h)
{
if(h == NULL)
{
return error;
}
Pnode node = (Pnode)malloc(sizeof(Node)/sizeof(char));
if(node == NULL)
{
return malloc_error;
}
system("clear");
node->id = '0'+i;
i++;
printf("\n\n    please input the name:");
scanf("%s",node->name);

printf("\n    please input the tel:");
scanf("%s",node->tel);

int j = 0;
int len = 0;
while(node->tel[j++] != '\0')
{
len ++;
}
while(len != 11)
{
printf("\n    please input 11 number's tel,input again:");
scanf("%s",node->tel);
len = 0;
j = 0;
while(node->tel[j++] != '\0')
{
len++;
}
}

printf("\n    please input the address:");
scanf("%s",node->address);

node->next = NULL;

printf("\n    %s's id is %c,tel is %s,address is %s\n",node->name,node->id,node->tel,node->address);

node->next = h->next;
h->next = node;
printf("\n    insert success!\n");
return ok;
}
//删除联系人
int Delete_list(Pnode h)
{
if(h == NULL)
{
return error;
}
char name[20];
printf("\n    please input the name deleted:");
scanf("%s",name);

Pnode temp = h->next;
while(temp)
{

if(strcmp(name,temp->name) != 0)
{
temp = temp->next;
}
else
{
Pnode p = temp;
p->next = temp;
p->next = temp->next;
printf("\n    delete success\n");
return ok;
}
}

printf("\n    该用户不存在:\n");

free(temp);
temp = NULL;
return ok;
}
//修改
int Alter_lidt(Pnode h)
{
if(h == NULL)
{
return error;
}
char name[20];
printf("\n    please input the name altered:");
scanf("%s",name);

Pnode temp = h->next;
while(temp)
{
if(strcmp(name,temp->name)!=0)
{
temp = temp->next;
}
else
{
if(Insert_list(h) != ok)
{
return error;
}
return ok;
}
}
if(temp == NULL)
{
printf("\n    该用户不存在:\n");
}
return ok;
}
//查询
int search_list(Pnode h)
{
if(h == NULL)
{
return error;
}
char name[20];
printf("\n    please input the name who searched:");
scanf("%s",name);

Pnode temp = h->next;
while(temp)
{
if(strcmp(name,temp->name)!=0)
{
temp = temp->next;
}
else
{
printf("\n    %s's id is %c,tel is %s,address is %s\n",temp->name,temp->id,temp->tel,temp->address);
free(temp);
temp = NULL;
return ok;
}
}

printf("\n    please input the true name:");
scanf("%s",name);
temp = h;
while(temp->next)
{
if(strcmp(name,temp->name)!=0)
{
temp = temp->next;
}
else
{
printf("\n    search success!\n");
printf("\n    %s's id is %s,tel is %s,address is %s\n",temp->name,temp->id,temp->tel,temp->address);
free(temp);
temp = NULL;
return ok;
}
}


}


int main()
{
Pnode head_node = (Pnode)malloc(sizeof(Node)/sizeof(char));
if(head_node == NULL)
{
return malloc_error;
}

head_node->next = NULL;

char onodetion[5];

while(1)
{
show();
scanf("%s",onodetion);

switch(onodetion[0])
{
case 'a': //insert 
{
if(Insert_list(head_node) != ok)
{
return error;
}

break;
}
case 'b': //delete
{
if(Delete_list(head_node) != ok)
{
return error;
}
break;
}
case 'c': //alter
{
if(Alter_lidt(head_node) != ok)
{
return error;
}
break;
}
case 'd': //search
{
if(search_list(head_node) != ok)
{
return error;
}

break;
}
}

}

return 0;
}

原创粉丝点击