C语言链表实现简易通讯录

时间:2022-05-09 14:36:35

本文实例为大家分享了C语言链表实现简易通讯录的具体代码,供大家参考,具体内容如下

链表实现通讯录功能:

1.添加–(输入 姓名,电话)
2.删除-- (输入人名,删除该人)
3.查询-- (直接打印所有联系人)
4.修改-- (输入人名,修改电话)

运行效果:

C语言链表实现简易通讯录

代码分主函数块 和 链表块:

Linklist.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef LINKLIST_H_INCLUDED
#define LINKLIST_H_INCLUDED
 
//链表节点
typedef struct Node
{
    char name[10];//名字
    char data[12];//电话
    struct Node* next;//上一个节点
    struct Node* prev;//下一个节点
}Node;
//链表
typedef struct LinkList
{
    Node* head;//链表头节点
    Node* tail;//链表尾节点
    int len;//链表长度
}LinkList;
//创造空节点
Node* creatNode();
//添加
void AddNode(LinkList* List,char* data,char* name);
//删除
int DeleteNode(LinkList* List,char* name);
//查询
void CheckList(LinkList* List);
//修改
int UpdataNode(LinkList* List,char* data,char* name);
 
#endif // LINKLIST_H_INCLUDED

Linklist.c

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include "LinkList.h"
#include <stdlib.h>
#include <string.h>
Node* creatNode()
{
    //申请空间
    Node* ptr=(Node* )malloc(sizeof(Node));
    //初始化
    memset(ptr->data,0,sizeof(ptr->data));
    memset(ptr->name,0,sizeof(ptr->name));
    ptr->next=NULL;
    ptr->prev=NULL;
    return ptr;
}
 
void AddNode(LinkList* List,char* data,char* name)
{
    Node* ptr=creatNode();
    if(List->len==0)//增加时链表还没有节点
    {
        strcpy(ptr->data,data);
        strcpy(ptr->name,name);
        List->head=ptr;
        List->tail=ptr;
        List->len++;
    }
    else
    {
        List->len++;
        strcpy(ptr->data,data);
        strcpy(ptr->name,name);
        ptr->prev=List->tail;
        List->tail->next=ptr;
        List->tail=ptr;
    }
}
 
int DeleteNode(LinkList* List,char* name)
{
    Node* ptr=List->head;
    int it=1;
    while(it<=List->len)
    {
        if(strcmp(ptr->name,name)==0)
        {
            if(it==List->len)//删除的是尾节点
            {
                ptr->prev->next=NULL;
                List->tail=ptr->prev;
                List->len--;
            }
            else if(it==1)//删除的是头节点
            {
                ptr->next->prev=NULL;
                List->head=ptr->next;
                List->len--;
            }
            else
            {
                ptr->next->prev=ptr->prev;
                ptr->prev->next=ptr->next;
                List->len--;
            }
            return 1;
        }
        ptr=ptr->next;
        it++;
    }
    return 0;
}
 
void CheckList(LinkList* List)
{
    Node* ptr=List->head;
    int it = 0;//遍历,类似迭代器
    while(it<List->len)
    {
        printf("%s\t %s\n",ptr->name,ptr->data);
        ptr=ptr->next;
        it++;
    }
}
 
int UpdataNode(LinkList* List,char* data,char* name)
{
    Node* ptr=List->head;
    int it=1;
    while(it<=List->len)
    {
        if(strcmp(ptr->name,name)==0)//找到名字,修改电话
        {
            strcpy(ptr->data,data);
            return 1;
        }
        ptr=ptr->next;
        it++;
    }
    return 0;
}

main.c

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include "LinkList.h"
int main()
{
    int choose;
    LinkList list1 = {NULL,NULL,0};
    //方便调试,先输入三个人
    AddNode(&list1,"15181512167","chen");
    AddNode(&list1,"12345678910","li");
    AddNode(&list1,"12456878216","zhang");
    while(1)
    {
        printf("1.添加 2.删除 3.查询 4.修改 0.退出\n");
        scanf("%d",&choose);
        switch(choose)
        {
        case 1:
        {
            char data[12];
            char name[10];
            printf("输入名字: ");
            scanf("%s",name);
            printf("输入电话: ");
            scanf("%s",data);
            AddNode(&list1,data,name);
            printf("----------------\n");
            break;
        }
        case 2:
        {
            char name1[10];
            CheckList(&list1);
            printf("选择删除的人: ");
            scanf("%s",name1);
            if(DeleteNode(&list1,name1))
            {
                printf("删除成功 \n");
            }
            else
            {
                printf("删除失败 \n");
            }
            printf("----------------\n");
            break;
        }
        case 3:
        {
            CheckList(&list1);
            printf("----------------\n");
            break;
        }
        case 4:
        {
            char name[10];
            char data[12];
            CheckList(&list1);
            printf("输入你要修改的人: ");
            scanf("%s",name);
            printf("输入修改后的电话号:");
            scanf("%s",data);
            if(UpdataNode(&list1,data,name))
            {
                printf("修改成功\n");
            }
            else
            {
                printf("修改失败\n");
            }
            printf("----------------\n");
            break;
        }
        case 0:
        {
            printf("----------------\n");
            exit(0);
        }
 
        }
    }
    return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/CY_612/article/details/113768424