用c语言链表实现通讯录
来源:互联网 发布:知天命尽人事的网名 编辑:程序博客网 时间:2024/05/17 03:42
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define F -1
#define T 1
struct Address
{
char name[20];
char number[12];
char address[20];
struct Address* next;
};
typedef struct Address* node;
int init(node* head);
int creat_tail(node head);
int insert_index(node head);
int length(node head);
int query_name(node head);
int delete_address(node head);
void print(node head);
int main()
{
node head;
init(&head);
int x;
do
{
printf("0: exit\n");
printf("1: creat_tail\n");
printf("2: insert_index\n");
printf("3: query_name\n");
printf("4: delete_address\n");
printf("5: print\n");
printf("please select\n");
scanf("%d", &x);
switch(x)
{
case 0:
exit(0);
case 1:
creat_tail(head);
break;
case 2:
insert_index(head);
break;
case 3:
query_name(head);
break;
case 4:
delete_address(head);
break;
case 5:
print(head);
break;
default :
exit(0);
}
}
while (1);
return 0;
}
int delete_address(node head)
{
char address[20];
printf("please input the address you want to delete\n");
scanf("%s", address);
while (head->next != NULL)
{
if (strcmp(head->next->address, address) == 0)
{
node temp = head->next;
head->next = head->next->next;
free(temp);
}
else
{
head = head->next;
}
}
return T;
}
int query_name(node head)
{
char name[20];
int count = 0, index = 0;
printf("please input the name you want\n");
scanf("%s", name);
while (head->next != NULL)
{
if (strcmp(head->next->name, name) == 0)
{
count++;
printf("%d.name:%s number:%s address:%s\n", index + 1, head->next->name, head->next->number, head->next->address);
}
head = head->next;
index++;
}
if (count == 0)
{
printf("not found\n");
}
return T;
}
int length(node head)
{
int count = 0;
while (head->next != NULL)
{
head = head->next;
count++;
}
return count;
}
int insert_index(node head)
{
int index;
printf("please input the index you want to add\n");
scanf("%d", &index);
if (index < 0 || index >= length(head))
{
printf("out of range\n");
return F;
}
node newnode = (node)malloc(sizeof(struct Address));
if (NULL == newnode)
{
return F;
}
int i;
for (i = 0; i < index; i++)
{
head = head->next;
}
printf("please input the name, number, address\n");
printf("when you input 0 0 0 , exit\n");
scanf("%s %s %s", newnode->name, newnode->number, newnode->address);
if (strcmp(newnode->name, "0") != 0)
{
newnode->next = head->next;
head->next = newnode;
}
return T;
}
void print(node head)
{
int count = 0;
while (head->next != NULL)
{
count++;
printf("%d.name:%s number:%s address:%s\n", count, head->next->name, head->next->number, head->next->address);
head = head->next;
}
}
int creat_tail(node head)
{
do
{
node newnode = (node)malloc(sizeof(struct Address));
if (NULL == newnode)
{
return F;
}
printf("please input the name, number, address\n");
printf("when you input 0 0 0 , exit\n");
scanf("%s %s %s", newnode->name, newnode->number, newnode->address);
if (strcmp(newnode->name, "0") != 0)
{
newnode->next = NULL;
while (head->next != NULL)
{
head = head->next;
}
head->next = newnode;
}
else
{
break;
}
}
while (1);
return T;
}
int init(node* head)
{
node newnode = (node)malloc(sizeof(struct Address));
if (NULL == newnode)
{
return F;
}
strcpy(newnode->name, "0");
strcpy(newnode->number, "0");
strcpy(newnode->address, "0");
newnode->next = NULL;
(*head) = newnode;
return T;
}
- 用c语言链表实现通讯录
- C语言课程设计通讯录链表实现
- c语言链表实现通讯录
- 用C语言实现通讯录
- c语言实现----通讯录
- C语言实现通讯录
- C语言实现通讯录
- c语言实现通讯录
- C语言实现通讯录
- C语言通讯录(利用链表实现)
- 用C语言实现一个通讯录
- 用C语言实现简单通讯录
- 【c语言】实现一个通讯录
- c语言实现通讯录系统
- C语言实现简单通讯录
- C语言实现简易通讯录
- c语言实现简单通讯录
- 【简易通讯录】-----C语言实现
- tensorflow实现对图片的读取
- 正则语言引擎:一个简单LEX和YACC结合运用的实例
- spark 2.1 TaskResult
- postfix
- android18
- 用c语言链表实现通讯录
- 列出联通集
- android18
- 数据结构基本概念
- Android-mvp&mvvm分别实现列表加载-demo
- 常见的正则表达式
- PuerMock的搭建和优化
- 服务器返回头部允许跨域
- android19