通讯录管理系统(C语言+内核链表)

本文通过C语言实现了基于内核链表的通讯录管理系统,包括添加、删除、查找、修改和显示联系人等功能。代码中使用了内核链表的插入、删除等操作,展示了如何在实际项目中应用数据结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

初学数据结构的练手项目,供大家参考,具体思路和学生管理系统大同小异,不过这次实现我是使用了双向链表。

本文通过C语言内核链表去写通讯录管理系统

内核链表有封装的链表处理函数,用起来非常方便,本文由脱产学习C语言的门外汉编写,如有更好见解欢迎斧正。

以下是内核链表的代码(备注以便日后翻找查看)

/*
 * Copyright (C) 2007 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License v2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H

#define LIST_POISON1  ((struct list_head *) 0x00100100)
#define LIST_POISON2  ((struct list_head *) 0x00200200)

/*
 * Simple doubly linked list implementation.
 *
 * Some of the internal functions ("__xxx") are useful when
 * manipulating whole lists rather than single entries, as
 * sometimes we already know the next/prev entries and we can
 * generate better code by using them directly rather than
 * using the generic single-entry routines.
 */

struct list_head {
	struct list_head *next, *prev;
};

#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \
	struct list_head name = LIST_HEAD_INIT(name)

static inline void INIT_LIST_HEAD(struct list_head *list)
{
	list->next = list;
	list->prev = list;
}

/*
 * Insert a new entry between two known consecutive entries.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
#ifndef CONFIG_DEBUG_LIST
static inline void __list_add(struct list_head *xnew,
			      struct list_head *prev,
			      struct list_head *next)
{
	next->prev = xnew;
	xnew->next = next;
	xnew->prev = prev;
	prev->next = xnew;
}
#else
extern void __list_add(struct list_head *xnew,
			      struct list_head *prev,
			      struct list_head *next);
#endif

/**
 * list_add - add a new entry
 * @new: new entry to be added
 * @head: list head to add it after
 *
 * Insert a new entry after the specified head.
 * This is good for implementing stacks.
 */
#ifndef CONFIG_DEBUG_LIST
static inline void list_add(struct list_head *xnew, struct list_head *head)
{
	__list_add(xnew, head, head->next);
}
#else
extern void list_add(struct list_head *xnew, struct list_head *head);
#endif


/**
 * list_add_tail - add a new entry
 * @new: new entry to be added
 * @head: list head to add it before
 *
 * Insert a new entry before the specified head.
 * This is useful for implementing queues.
 */
static inline void list_add_tail(struct list_head *xnew, struct list_head *head)
{
	__list_add(xnew, head->prev, head);
}

/*
 * Delete a list entry by making the prev/next entries
 * point to each other.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
	next->prev = prev;
	prev->next = next;
}

/**
 * list_del - deletes entry from list.
 * @entry: the element to delete from the list.
 * Note: list_empty on entry does not return true after this, the entry is
 * in an undefined state.
 */
#ifndef CONFIG_DEBUG_LIST
static inline void list_del(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);
	entry->next = LIST_POISON1;
	entry->prev = LIST_POISON2;
}
#else
extern void list_del(struct list_head *entry);
#endif

/**
 * list_replace - replace old entry by new one
 * @old : the element to be replaced
 * @new : the new element to insert
 * Note: if 'old' was empty, it will be overwritten.
 */
static inline void list_replace(struct list_head *old,
				struct list_head *xnew)
{
	xnew->next = old->next;
	xnew->next->prev = xnew;
	xnew->prev = old->prev;
	xnew->prev->next = xnew;
}

static inline void list_replace_init(struct list_head *old,
					struct list_head *xnew)
{
	list_replace(old, xnew);
	INIT_LIST_HEAD(old);
}
/**
 * list_del_init - deletes entry from list and reinitialize it.
 * @entry: the element to delete from the list.
 */
static inline void list_del_init(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);
	INIT_LIST_HEAD(entry);
}

/**
 * list_move - delete from one list and add as another's head
 * @list: the entry to move
 * @head: the head that will precede our entry
 */
static inline void list_move(struct list_head *list, struct list_head *head)
{
        __list_del(list->prev, list->next);
        list_add(list, head);
}

/**
 * list_move_tail - delete from one list and add as another's tail
 * @list: the entry to move
 * @head: the head that will follow our entry
 */
static inline void list_move_tail(struct list_head *list,
				  struct list_head *head)
{
        __list_del(list->prev, list->next);
        list_add_tail(list, head);
}

/**
 * list_is_last - tests whether @list is the last entry in list @head
 * @list: the entry to test
 * @head: the head of the list
 */
static inline int list_is_last(const struct list_head *list,
				const struct list_head *head)
{
	return list->next == head;
}

/**
 * list_empty - tests whether a list is empty
 * @head: the list to test.
 */
static inline int list_empty(const struct list_head *head)
{
	return head->next == head;
}

/**
 * list_empty_careful - tests whether a list is empty and not being modified
 * @head: the list to test
 *
 * Description:
 * tests whether a list is empty _and_ checks that no other CPU might be
 * in the process of modifying either member (next or prev)
 *
 * NOTE: using list_empty_careful() without synchronization
 * can only be safe if the only activity that can happen
 * to the list entry is list_del_init(). Eg. it cannot be used
 * if another CPU could re-list_add() it.
 */
static inline int list_empty_careful(const struct list_head *head)
{
	struct list_head *next = head->next;
	return (next == head) && (next == head->prev);
}

static inline void __list_splice(const struct list_head *list,
				 struct list_head *prev,
				 struct list_head *next)
{
	struct list_head *first = list->next;
	struct list_head *last = list->prev;

	first->prev = prev;
	prev->next = first;

	last->next = next;
	next->prev = last;
}

/**
 * list_splice - join two lists
 * @list: the new list to add.
 * @head: the place to add it in the first list.
 */
static inline void list_splice(struct list_head *list, struct list_head *head)
{
	if (!list_empty(list))
		__list_splice(list, head, head->next);
}

/**
 * list_splice_tail - join two lists, each list being a queue
 * @list: the new list to add.
 * @head: the place to add it in the first list.
 */
static inline void list_splice_tail(struct list_head *list,
				struct list_head *head)
{
	if (!list_empty(list))
		__list_splice(list, head->prev, head);
}

/**
 * list_splice_init - join two lists and reinitialise the emptied list.
 * @list: the new list to add.
 * @head: the place to add it in the first list.
 *
 * The list at @list is reinitialised
 */
static inline void list_splice_init(struct list_head *list,
				    struct list_head *head)
{
	if (!list_empty(list)) {
		__list_splice(list, head, head->next);
		INIT_LIST_HEAD(list);
	}
}

/**
 * list_splice_tail_init - join two lists and reinitialise the emptied list
 * @list: the new list to add.
 * @head: the place to add it in the first list.
 *
 * Each of the lists is a queue.
 * The list at @list is reinitialised
 */
static inline void list_splice_tail_init(struct list_head *list,
					 struct list_head *head)
{
	if (!list_empty(list)) {
		__list_splice(list, head->prev, head);
		INIT_LIST_HEAD(list);
	}
}

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

#define container_of(ptr, type, member) ({ const typeof( ((type *)0)->member )*__mptr = (ptr);(type *)( (char *)__mptr - offsetof(type,member) );})

/**
 * list_entry - get the struct for this entry
 * @ptr:	the &struct list_head pointer.
 * @type:	the type of the struct this is embedded in.
 * @member:	the name of the list_struct within the struct.
 */
#define list_entry(ptr, type, member) \
	container_of(ptr, type, member)

/**
 * list_first_entry - get the first element from a list
 * @ptr:	the list head to take the element from.
 * @type:	the type of the struct this is embedded in.
 * @member:	the name of the list_struct within the struct.
 *
 * Note, that list is expected to be not empty.
 */
#define list_first_entry(ptr, type, member) \
	list_entry((ptr)->next, type, member)

/**
 * list_next_entry - get the next element from a list
 * @ptr:	the list head to take the element from.
 * @member:	the name of the list_struct within the struct.
 *
 * Note, that next is expected to be not null.
 */
#define list_next_entry(ptr, member) \
	list_entry((ptr)->member.next, typeof(*ptr), member)

/**
 * list_for_each	-	iterate over a list
 * @pos:	the &struct list_head to use as a loop cursor.
 * @head:	the head for your list.
 */
#define list_for_each(pos, head) \
	for (pos = (head)->next; pos != (head); \
        	pos = pos->next)

/**
 * __list_for_each	-	iterate over a list
 * @pos:	the &struct list_head to use as a loop cursor.
 * @head:	the head for your list.
 *
 * This variant differs from list_for_each() in that it's the
 * simplest possible list iteration code, no prefetching is done.
 * Use this for code that knows the list to be very short (empty
 * or 1 entry) most of the time.
 */
#define __list_for_each(pos, head) \
	for (pos = (head)->next; pos != (head); pos = pos->next)

/**
 * list_for_each_prev	-	iterate over a list backwards
 * @pos:	the &struct list_head to use as a loop cursor.
 * @head:	the head for your list.
 */
#define list_for_each_prev(pos, head) \
	for (pos = (head)->prev; pos != (head); \
        	pos = pos->prev)

/**
 * list_for_each_safe - iterate over a list safe against removal of list entry
 * @pos:	the &struct list_head to use as a loop cursor.
 * @n:		another &struct list_head to use as temporary storage
 * @head:	the head for your list.
 */
#define list_for_each_safe(pos, n, head) \
	for (pos = (head)->next, n = pos->next; pos != (head); \
		pos = n, n = pos->next)

/**
 * list_for_each_entry	-	iterate over list of given type
 * @pos:	the type * to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the list_struct within the struct.
 */
#define list_for_each_entry(pos, head, member)				\
	for (pos = list_entry((head)->next, typeof(*pos), member);	\
	     &pos->member != (head); 	\
	     pos = list_entry(pos->member.next, typeof(*pos), member))

/**
 * list_for_each_entry_reverse - iterate backwards over list of given type.
 * @pos:	the type * to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the list_struct within the struct.
 */
#define list_for_each_entry_reverse(pos, head, member)			\
	for (pos = list_entry((head)->prev, typeof(*pos), member);	\
	     &pos->member != (head); 	\
	     pos = list_entry(pos->member.prev, typeof(*pos), member))

/**
 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue
 * @pos:	the type * to use as a start point
 * @head:	the head of the list
 * @member:	the name of the list_struct within the struct.
 *
 * Prepares a pos entry for use as a start point in list_for_each_entry_continue.
 */
#define list_prepare_entry(pos, head, member) \
	((pos) ? : list_entry(head, typeof(*pos), member))

/**
 * list_for_each_entry_continue - continue iteration over list of given type
 * @pos:	the type * to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the list_struct within the struct.
 *
 * Continue to iterate over list of given type, continuing after
 * the current position.
 */
#define list_for_each_entry_continue(pos, head, member) 		\
	for (pos = list_entry(pos->member.next, typeof(*pos), member);	\
	     &pos->member != (head);	\
	     pos = list_entry(pos->member.next, typeof(*pos), member))

/**
 * list_for_each_entry_from - iterate over list of given type from the current point
 * @pos:	the type * to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the list_struct within the struct.
 *
 * Iterate over list of given type, continuing from current position.
 */
#define list_for_each_entry_from(pos, head, member) 			\
	for (; &pos->member != (head);	\
	     pos = list_entry(pos->member.next, typeof(*pos), member))

/**
 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 * @pos:	the type * to use as a loop cursor.
 * @n:		another type * to use as temporary storage
 * @head:	the head for your list.
 * @member:	the name of the list_struct within the struct.
 */
#define list_for_each_entry_safe(pos, n, head, member)			\
	for (pos = list_entry((head)->next, typeof(*pos), member),	\
		n = list_entry(pos->member.next, typeof(*pos), member);	\
	     &pos->member != (head); 					\
	     pos = n, n = list_entry(n->member.next, typeof(*n), member))

/**
 * list_for_each_entry_safe_continue
 * @pos:	the type * to use as a loop cursor.
 * @n:		another type * to use as temporary storage
 * @head:	the head for your list.
 * @member:	the name of the list_struct within the struct.
 *
 * Iterate over list of given type, continuing after current point,
 * safe against removal of list entry.
 */
#define list_for_each_entry_safe_continue(pos, n, head, member) 		\
	for (pos = list_entry(pos->member.next, typeof(*pos), member), 		\
		n = list_entry(pos->member.next, typeof(*pos), member);		\
	     &pos->member != (head);						\
	     pos = n, n = list_entry(n->member.next, typeof(*n), member))

/**
 * list_for_each_entry_safe_from
 * @pos:	the type * to use as a loop cursor.
 * @n:		another type * to use as temporary storage
 * @head:	the head for your list.
 * @member:	the name of the list_struct within the struct.
 *
 * Iterate over list of given type from current point, safe against
 * removal of list entry.
 */
#define list_for_each_entry_safe_from(pos, n, head, member) 			\
	for (n = list_entry(pos->member.next, typeof(*pos), member);		\
	     &pos->member != (head);						\
	     pos = n, n = list_entry(n->member.next, typeof(*n), member))

/**
 * list_for_each_entry_safe_reverse
 * @pos:	the type * to use as a loop cursor.
 * @n:		another type * to use as temporary storage
 * @head:	the head for your list.
 * @member:	the name of the list_struct within the struct.
 *
 * Iterate backwards over list of given type, safe against removal
 * of list entry.
 */
#define list_for_each_entry_safe_reverse(pos, n, head, member)		\
	for (pos = list_entry((head)->prev, typeof(*pos), member),	\
		n = list_entry(pos->member.prev, typeof(*pos), member);	\
	     &pos->member != (head); 					\
	     pos = n, n = list_entry(n->member.prev, typeof(*n), member))

#endif

正文代码

本文使用的是双向链表,内核链表

#include <stdio.h>
#include <stdlib.h>
//内核链表头文件不能少
#include "list.h"
#include <string.h>
//定义一个联系人结构体
struct contacts
{
    //第一个成员是内核链表里的结构体
    struct list_head list;
    char name[1024];
    char tel[1024];
};
//菜单函数,打印菜单
void menu()
{
    printf("=====================================\n");
    printf("============1.增加联系人=============\n");
    printf("============2.删除联系人=============\n");
    printf("============3.查找联系人=============\n");
    printf("============4.修改联系人=============\n");
    printf("==========5.设置星标联系人===========\n");
    printf("============6.打印通讯录=============\n");
    printf("============0.退出通讯录=============\n");
    printf("=====================================\n");
}
// 插入新的联系人
void insert(struct list_head *head, char *name, char *tel)
{
    struct contacts *new = malloc(sizeof(struct contacts));
    
    strcpy(new->name, name);
    strcpy(new->tel, tel);
    // 内核链表函数尾插
    list_add_tail(&new->list, head);
}

// 查找联系人
void find(struct list_head *head)
{
    int f;
    struct list_head *pos = head->next;
    printf("    **********************    \n");
    printf("      选择以什么方式查找  \n");
    printf("********************************\n");
    printf("** 1、名字查找    2、电话查找 **\n");
    printf("********************************\n");
    //此处getchar()是防止输入数字以外的函数,其实也可以通过scanf的返回值判断输入的值是否为整形
    scanf("%d", &f);
    getchar();
    //判定加输出
    switch (f)
    {
    case 1:
    {
        char name[1024];
        printf("请输入要查找联系人的姓名:\n");
        scanf("%s", name);
        while (pos != head)
        {
            //对比字符串
            if (strcmp(((struct contacts *)pos)->name, name) == 0)
            {
                //pos是struct list_head*类型的,强转一下取地址,方便操作
                printf("%s\t%s\n", ((struct contacts *)pos)->name, ((struct contacts *)pos)->tel);
                return;
            }
            pos = pos->next;
        }
        printf("未找到联系人,请核实姓名!\n");
        break;
    }

    case 2:
    {
        char tel[1024];
        printf("请输入要查找联系人的电话:\n");
        scanf("%s", tel);
        while (pos != head)
        {
            if (strcmp(((struct contacts *)pos)->tel, tel) == 0)
            {
                printf("%s\t%s\n", ((struct contacts *)pos)->name, ((struct contacts *)pos)->tel);
                return;
            }
            pos = pos->next;
        }
        printf("未找到联系人,请核实电话!\n");
        break;
    }
    default:
        printf("请输入正确的查询方式!\n");
    }
    return;
}
// 遍历/打印联系人列表
void show(struct list_head *head)
{
    struct list_head *pos;

    printf("姓名\t电话\t");
    printf("\n");
    //内核函数带参宏定义,即相当于for(pos=head->next,pos != head,pos = pos->next)
    list_for_each(pos, head)
    {
        //强转,作用类似上一个查找函数的输出
        struct contacts *each = (struct contacts *)pos;
        printf("%s\t%s\t", each->name, each->tel);
        printf("\n");
    }
}

// 删除联系人
void del(struct list_head *head)
{
    struct list_head *pos = head->next;
    while (pos != head)
    {
        char name[1024];
        printf("请输入要删除联系人的姓名:\n");
        scanf("%s", name);
        while (pos != head)
        {
            if (strcmp(((struct contacts *)pos)->name, name) == 0)
            {
                list_del(pos);
                return;
            }
            pos = pos->next;
        }
        printf("未找到联系人,请核实姓名!\n");
    }
}

// 设置星标联系人
// 星标联系人是只始终在第一个的联系人
// 利用内核链表的list_move函数实现的
void star(struct list_head *head)
{
    struct list_head *pos = head->next;
    while (pos != head)
    {
        char name[1024];
        printf("请输入要设置星标联系人的姓名:\n");
        scanf("%s", name);
        while (pos != head)
        {
            if (strcmp(((struct contacts *)pos)->name, name) == 0)
            {
                //内核链表函数,作用是取出链表某个节点并将其头插
                list_move(pos, head);
                return;
            }
            pos = pos->next;
        }
        printf("未找到联系人,请核实姓名!\n");
    }
}

// 增加联系人
void add(struct list_head *head)
{
    struct list_head *new = malloc(sizeof(struct list_head));
    printf("请输入要增加的联系人的姓名电话:\n");
    scanf("%s%s", ((struct contacts *)new)->name, ((struct contacts *)new)->tel);
    //内核链表寒湖是尾插
    //为了更好突出星标联系人,增加联系人都用尾插
    list_add_tail(new, head);
}

// 修改联系人
void change(struct list_head *head)
{
    char name[1024];
    struct list_head *pos = head->next;
    printf("请输入要修改联系人的姓名:\n");
    scanf("%s", name);
    //遍历链表
    while (pos != head)
    {
        if (strcmp(((struct contacts *)pos)->name, name) == 0)
        {
            printf("    **********************    \n");
            printf("        请选择修改项目  \n");
            printf("****************************\n");
            printf("** 1、名字    2、电话 **\n");
            printf("****************************\n");
            int f;
            scanf("%d", &f);
            switch (f)
            {
            case 1:
            {
                printf("请输入修改后的联系人的姓名:\n");
                scanf("%s", ((struct contacts *)pos)->name);
            }
            break;
            case 2:
            {
                printf("请输入修改后的联系人的电话:\n");
                scanf("%s", ((struct contacts *)pos)->tel);
            }
            break;
            default:
                printf("请输入正确的查询方式!\n");
            }
            return;
        }
        pos = pos->next;
    }
    printf("未找到联系人,请核实姓名!\n");
    return;
}
int main()
{
    // 初始化头节点
    struct list_head *head = malloc(sizeof(struct list_head));
    INIT_LIST_HEAD(head);
    //简单插入一些节点
    insert(head, "亚当斯", "213-555-0144");
    insert(head, "安妮", "232-155-0121");
    insert(head, "安再峰", "745-505-6589");
    insert(head, "爱丽丝", "222-555-3782");
    insert(head, "布鲁斯", "313-555-7024");
    insert(head, "布雷迪", "213-555-0354");
    
    int choice;
    do
    {
        printf("      欢迎使用通讯录管理系统!\n");
        menu();
        scanf("%d", &choice);
        switch (choice)
        {
        case 0:
            break;
            ;
        case 1:
            add(head);
            break;
        case 2:
            del(head);
            break;
        case 3:
            find(head);
            break;
        case 4:
            change(head);
            break;
        case 5:
            star(head);
            break;
        case 6:
            show(head);
            break;
        default:
            printf("请输入正确的数字\n");
            break;
        }

    } while (choice);

    free(head);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值